3

Is there a reason why a Wifi network set on Android (Lollipop) wouldn't automatically reconnect after a router reset? The network is being set like this:

private boolean connectToNetwork(ScanResult scanResult, String password, WifiManager wifiManager)
{
    WifiConfiguration wifiConfig = new WifiConfiguration();
    String quotedSSID = "\"" + scanResult.SSID + "\"";
    wifiConfig.SSID = quotedSSID;
    wifiConfig.status = WifiConfiguration.Status.DISABLED;
    wifiConfig.priority = 40;

    // Dependent on the security type of the selected network
    // we set the security settings for the configuration
    SecurityType securityType = getSecurityType(scanResult);
    if (securityType == SecurityType.Open)
    {
        // No security
        wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        wifiConfig.allowedAuthAlgorithms.clear();
        wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
        wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    }
    else if (securityType == SecurityType.WPA)
    {
        //WPA/WPA2 Security
        wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
        wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        wifiConfig.preSharedKey = "\"".concat(password).concat("\"");
    }
    else if (securityType == SecurityType.WEP)
    {
        // WEP Security
        wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
        wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);

        if (getHexKey(password))
            wifiConfig.wepKeys[0] = password;
        else
            wifiConfig.wepKeys[0] = "\"".concat(password).concat("\"");
        wifiConfig.wepTxKeyIndex = 0;
    }

    // Finally we add the new configuration to the managed list of networks
    connectionReceiver = new ConnectionReceiver();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
    intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(connectionReceiver, intentFilter);
    int networkID = wifiManager.addNetwork(wifiConfig);
    if (networkID != -1)
    {
        if(wifiManager.enableNetwork(networkID, true))
        {
            wifiManager.saveConfiguration();
            return true;
        }
    }

    // Connection failed
    unregisterReceiver(connectionReceiver);
    connectionReceiver = null;

    return false;
}

Which works fine and connects to the network. The network will continue to work fine until I switch off the Wifi router. Then, after switching it back on and waiting for the device to reconnect, I get errors and can't access the internet. The following code returns true, so it looks like the device has reconnected to the network:

public boolean isNetworkAvailable()
{
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

The active network in the above if the correct Wifi network, but when trying to actually access anything, I get errors. For example, Chromium gives a bunch of:

E/chromium: [ERROR:socket_posix.cc(80)] CreatePlatformSocket() returned an error, errno=64: Machine is not on the network
W/chromium: [WARNING:net_errors_posix.cc(116)] Unknown error 64 mapped to net::ERR_FAILED

And Volley gives the same:

java.net.SocketException: socket failed: errno 64 (Machine is not on the network)
Andrew Porritt
  • 1,706
  • 14
  • 22

1 Answers1

4

Turns out the code was setting the process default network elsewhere:

connectivityManager.setProcessDefaultNetwork(net);

Removing this line fixed the issue.

Andrew Porritt
  • 1,706
  • 14
  • 22
  • You can absolutely use the above line. You need to unbind from the network when the state changes to disconnected with `connMgr.setProcessDefaultNetwork(null);` using a broadcast manager. Thanks to this post: https://stackoverflow.com/q/40462955/1876355 – Pierre Dec 18 '17 at 11:50