Khushbu Shah
Khushbu Shah

Reputation: 1683

Android disconnects automatically from WiFi without internet below Android 10

Working on an android application in which I need to connect WiFi device programatically which does not have internet. Here is a code:

    private void connectToWiFi(final String ssid, String password) {

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {

            WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
            final ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkRequest.Builder request = new NetworkRequest.Builder();
            request.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
            request.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); // Internet not required

            ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {

                @Override
                public void onAvailable(Network network) {

                    String networkSSID = getNetworkSsid();

                    if (networkSSID.equals(ssid)) {
                        connectivityManager.bindProcessToNetwork(network);
                    }
                }

                @Override
                public void onUnavailable() {
                    super.onUnavailable();
                }

                @Override
                public void onLost(@NonNull Network network) {
                    super.onLost(network);
                }
            };
            connectivityManager.registerNetworkCallback(request.build(), networkCallback);

            WifiConfiguration config = new WifiConfiguration();
            config.SSID = String.format("\"%s\"", ssid);

            int netId = -1;
            List<WifiConfiguration> apList = wifiManager.getConfiguredNetworks();

            for (WifiConfiguration i : apList) {

                if (i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {
                    netId = i.networkId;
                }
            }

            // Add network in Saves network list if it is not available in list
            if (netId == -1) {

                if (TextUtils.isEmpty(password)) {
                    Log.d(TAG, "====== Connect to open network");
                    config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                } else {
                    Log.d(TAG, "====== Connect to secure network");
                    config.preSharedKey = String.format("\"%s\"", password);
                }

                netId = wifiManager.addNetwork(config);
            }

            Log.d(TAG, "Connect to Network : " + netId);
            wifiManager.enableNetwork(netId, true);

        } else {

            // For Android 10 and above
            // WifiNetworkSpecifier code
        }
    }

Permissions used :

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Above code works fine for the networks that offer internet connection. But it fails in most of the cases for the networks without internet connection.

When attempting to connect to a WiFi configuration which does not have internet access, we need to bind our app's process to that network, or configure our HTTP client to use that network's socket factory, otherwise the system will disconnect from this WiFi network.

As per this guideline , bindProcessToNetwork is already applied. But system is disconnecting from the network regardless of the use of bindToProcess() / routing traffic over the socket factory. It is not allowing to remain connected with the WiFi which don't have internet. It is really surprising, Android has never thought for IoT device use cases before restricting to network connection.

When attempting to connect to a WiFi network added by my app or system settings app or any other app, the system logs reveal:

UID xxxxx does not have permission to update configuration

Same error is not occurring when app tries to connect device which has internet even if that network is added by some other app.

So the app is allowed to connect to the previous configuration, albeit with 'insufficient permissions', and traffic is momentarily routed over this network. But after a few seconds, the network is disconnected, and the system attempts to re-associate with some other internet-enabled network.

Testing on Moto G5 Plus, Android 8.1.0, but I believe this is a platform bug, not device specific. And mostly this bug is introduced from Android 7 or something because it was working previously.

I have also reported issue here. Also provided sample app in this issue.

Is there any solution available for this issue ? Is there any option of paid support from Android ?

Thanks in advance.

Upvotes: 13

Views: 9333

Answers (3)

Always Learning
Always Learning

Reputation: 2743

Replace:

  • connectivityManager.registerNetworkCallback link

With

  • connectivityManager.requestNetwork link

Relevant docs on requestNetwork from the link above:

As long as this request is outstanding, the platform will try to maintain the best network matching this request, while always attempting to match the request to a better network if possible.

Note the above docs are not included for registerNetworkCallback.

The Android Connectivity stack will tear down unused networks. This is tracked by the number of requests a network has by requestNetwork. Requests filed as part of registerNetworkCallback are NOT counted therefore as far as the Connectivity stack is concerned, the Wi-Fi network you are requesting is not being requested and can therefore be torn down.

This doesn't apply for the default network, however only networks with internet capability are eligible to be the default which does not apply in your case.

Upvotes: 0

JexSrs
JexSrs

Reputation: 155

If you are targeting to use your code in specific networks then you can go to yours access point interface and simply set the DHCP settings to static and give the devices an appropriate IP.

More info can be found here

Now if you are targeting networks that does you cannot access their DHCP settings, based on that link you can tell your device to stay connected to the network no matter if it has internet connection.

NetworkRequest.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    builder = new NetworkRequest.Builder();
    //set the transport type do WIFI
    builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
    connectivityManager.requestNetwork(builder.build(), new
                                 ConnectivityManager.NetworkCallback() {
        @Override
        public void onAvailable(Network network) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (Build.VERSION.RELEASE.equalsIgnoreCase("6.0")) {
                    if (!Settings.System.canWrite(mActivity)) {
                        Intent goToSettings = new  Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
                        goToSettings.setData(Uri.parse("package:" + 
                        mActivity.getPackageName()));
                        mActivity.startActivity(goToSettings);
                    }
                }
                connectivityManager.bindProcessToNetwork(null);
                if (mSsid.contains("my_iot_device-xxxxxxxxx"))
                    connectivityManager.bindProcessToNetwork(network);
                else {
                    //This method was deprecated in API level 23
                    ConnectivityManager.setProcessDefaultNetwork(null);
                    if (mSsid.contains("my_iot_device-xxxxxxxxx"))
                        ConnectivityManager.setProcessDefaultNetwork(network);
                }
                connectivityManager.unregisterNetworkCallback(this);
            }
        }
    });
}

Also sometimes it is good to keep your wifi radio awake, so you gonna need to hold it using WiFiLock.

//Create a wifi manager instance first
WifiManager mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
//and then create your wifiLock
WifiManager.WifiLock mWifiLock = mWifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, "wifiLock");
if(!mWifiLock.isHeld())
        mWifiLock.acquire();

Later if you want to realese it

if(mWifiLock.isHeld())
        mWifiLock.release();

This code helped me to stay connected to my network even if it not has internet access.

Upvotes: 0

Nilesh Adhikari
Nilesh Adhikari

Reputation: 38

The problem you mentioned might not be because of the code you provided at Issuetracker. Once i fixed the problem in my device (Android 9), i am unable to replicate the error even after a complete undo. I even tried device reboot, app reinstallation or even factory reset. Try this.

  1. In Setting --> Apps & Permissions, the test app requests for a Location permission. Give it the permisson.

  2. Java code can be

    private void connectToWiFi(final String ssid, String password) {
    
    if (connectivityManager != null) {
    
        try {
            connectivityManager.bindProcessToNetwork(null);
            connectivityManager.unregisterNetworkCallback(networkCallback);
        } catch (Exception e) {
            Log.d(TAG, "Connectivity Manager is already unregistered");
        }
    }
    
    NetworkRequest.Builder request = new NetworkRequest.Builder();
    request.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
    if(isOnline()==false){
        request.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
    }
    
    networkCallback = new ConnectivityManager.NetworkCallback() {
    
        @Override
        public void onAvailable(Network network) {
    
            String networkSSID = getNetworkSsid();
            Log.e(TAG, "Network is available - " + networkSSID);
    
            if (networkSSID.equals(ssid)) {
                if(isOnline()==false){
                    Log.e(TAG, "bindProcessToNetwork");
                    connectivityManager.bindProcessToNetwork(network);
                }
            }
        }
    
        @Override
        public void onUnavailable() {
            super.onUnavailable();
            Log.e(TAG, "Network is Unavailable");
        }
    
        @Override
        public void onLost(@NonNull Network network) {
            super.onLost(network);
            Log.e(TAG, "Lost Network Connection");
        }
    };
    
    connectivityManager.registerNetworkCallback(request.build(), networkCallback);
    
    if (!wifiManager.isWifiEnabled()) {
        wifiManager.setWifiEnabled(true);
    }
    
    WifiConfiguration config = new WifiConfiguration();
    config.SSID = String.format("\"%s\"", ssid);
    config.status = WifiConfiguration.Status.ENABLED;
    
    int netId = -1;
    List<WifiConfiguration> apList = wifiManager.getConfiguredNetworks();
    Log.d(TAG, "List Size : " + apList.size());
    
    for (WifiConfiguration i : apList) {
    
        if (i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {
            netId = i.networkId;
        }
    }
    
    // Add network in Saves network list if it is not available in list
    if (netId == -1) {
    
        if (TextUtils.isEmpty(password)) {
            Log.e(TAG, "====== Connect to open network");
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        } else {
            Log.e(TAG, "====== Connect to secure network");
            config.preSharedKey = String.format("\"%s\"", password);
        }
    
        netId = wifiManager.addNetwork(config);
    }
    
    Log.d(TAG, "Connect to network : " + netId);
    wifiManager.enableNetwork(netId, true);
    
    }
    
    public boolean isOnline() {
    Runtime runtime = Runtime.getRuntime();
    try {
        Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
        int     exitValue = ipProcess.waitFor();
        return (exitValue == 0);
    }
    catch (IOException e)          { e.printStackTrace(); }
    catch (InterruptedException e) { e.printStackTrace(); }
    
    return false;
    }
    

Upvotes: 0

Related Questions