2016-07-25 83 views
2

在Android(Lollipop)上設置的Wifi網絡在路由器重置後不會自動重新連接是否有原因?網絡設置是這樣的:當Wifi下降時,Android網絡不會重新連接

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網絡,但在嘗試實際訪問什麼的時候,我得到的錯誤。例如,鉻給出了一串:

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 

抽射給出相同:

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

回答

1

原來的代碼的其他地方被設置進程默認網絡:

connectivityManager.setProcessDefaultNetwork(net);

刪除此行解決了問題。

+0

你可以絕對使用上面的行。當狀態改變爲與'connMgr.setProcessDefaultNetwork(null);'使用廣播管理器斷開連接時,您需要從網絡解除綁定。感謝這篇文章:https://stackoverflow.com/q/40462955/1876355 – Pierre

相關問題