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.