Oreo Wifi Connectivity

前端 未结 4 967
無奈伤痛
無奈伤痛 2021-02-05 20:29

I am working on an IoT app in which there is an on boarding process where the user connects to an access point, which has not internet connectivity, configure the device and the

4条回答
  •  礼貌的吻别
    2021-02-05 20:51

    I am currently facing the same issue right now for the newer phone with Android 8.+. I am using WifiManager call bindProcessToNetwork here this will allows data traffic go through the wifi with no internet access,

    here how I connect my phone to the access point

    //Method to connect to WIFI Network
    public boolean connectTo(String networkSSID, String key) {
        WifiConfiguration config = new WifiConfiguration();
        WifiInfo info = wifi.getConnectionInfo(); //get WifiInfo
        int id = info.getNetworkId(); //get id of currently connected network
    
        config.SSID = "\"" + networkSSID + "\"";
        if (key.isEmpty()) {
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        }
        int netID = wifi.addNetwork(config);
    
        int tempConfigId = getExistingNetworkId(config.SSID);
    
        if (tempConfigId != -1) {
            netID = tempConfigId;
        }
    
        boolean disconnect = wifi.disconnect();
        wifi.disableNetwork(id); //disable current network
        boolean enabled = wifi.enableNetwork(netID, true);
        boolean connected = wifi.reconnect();
    
    if (((Build.VERSION.SDK_INT >= Build.VERSION_CODES.M))
                        || ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
                                && !(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M))) {
    
                    final ConnectivityManager manager = (ConnectivityManager) context
                            .getSystemService(Context.CONNECTIVITY_SERVICE);
                    NetworkRequest.Builder builder;
                    builder = new NetworkRequest.Builder();
                    //set the transport type do WIFI
                    builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
    
                    manager.requestNetwork(builder.build(), new ConnectivityManager.NetworkCallback() {
                        @Override
                        public void onAvailable(Network network) {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                manager.bindProcessToNetwork(network);
                            } else {
                                ConnectivityManager.setProcessDefaultNetwork(network);
                            }
                            try {
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            manager.unregisterNetworkCallback(this);
                        }
                    });
                }
    

    after connecting to the access point created by the device, it will constantly drop off from it.

提交回复
热议问题