当Wifi丢弃时,Android网络无法重新连接

时间:2016-07-25 16:24:18

标签: android networking

在路由器重置后,Android(Lollipop)上的Wifi网络设置是否会自动重新连接?网络设置如下:

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;
}

哪种方法正常并连接到网络。在我关闭Wifi路由器之前,网络将继续正常工作。然后,在重新打开并等待设备重新连接后,我收到错误并无法访问互联网。以下代码返回true,因此看起来设备已重新连接到网络:

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

上面的活动网络如果是正确的Wifi网络,但在尝试实际访问任何东西时,我都会收到错误。例如,Chromium提供了一堆:

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

而Volley给出的相同:

java.net.SocketException: socket failed: errno 64 (Machine is not on the network)

1 个答案:

答案 0 :(得分:3)

原来代码是在其他地方设置流程默认网络:

connectivityManager.setProcessDefaultNetwork(net);

删除此行可修复此问题。