2017-09-28 78 views
0

在使用這對連接到一個開放的WiFi網絡(未在設備上還配置):的Android連接到打開無線網絡 - Nexus 5和Nexus 5X之間零散

public static void connectToWifiNetwork(Context context, final String ssid, String password) { 
    final WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); 
    wifiManager.disconnect(); 

    // Delete already available network 
    List<WifiConfiguration> list = wifiManager.getConfiguredNetworks(); 
    for (WifiConfiguration i : list) { 
     if(i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) { 
      Log.i(TAG, "Deleting configuration for " + ssid); 
      wifiManager.removeNetwork(i.networkId); 

      break; 
     } 
    } 

    WifiConfiguration conf = new WifiConfiguration(); 
    conf.SSID = "\"" + ssid + "\""; 
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 

    Log.d(TAG, "Added network " + ssid + " " + password); 
    final int addNetworkResult = wifiManager.addNetwork(conf); 

    new Thread(new Runnable() { 
     @Override 
     public void run() { 
      Log.d(TAG, "Attempting to connect to " + ssid + " with id " + addNetworkResult); 
      wifiManager.enableNetwork(addNetworkResult, true); 
     } 
    }).start(); 
} 

在Nexus 5與API 23 (6.0.1),添加的網絡有結果-1,不連接。 在具有API 26(8.0.0)的Nexus 5X上,添加的網絡具有結果2,連接正常。

我建立了目標API 25

我不知道這是否是對API級別或設備,但是我想有一個解決方案,以統治他們。

任何想法?

編輯: 與所有的配置也試圖在這個SO question

conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 
conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 
conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA); 
conf.allowedAuthAlgorithms.clear(); 
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); 
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 

沒有Nexus 5的正常工作。 注:我可以用this implementation.

回答

0

出於某種原因,連接到編程方式使用兩個設備WEP/WPA/WPA2,我不得不手動刪除Android的WiFi設置開放式網絡(即使連接通過它工作正常)。它現在連接,但不是每次。

它斷開與當前網絡的連接,嘗試連接到開放網絡,然後再次連接到以前的網絡。