问题
String networkSSID = "networkName";
String networkPassword = "networkPassword";
WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (!wm.isWifiEnabled()) {
if (wm.getWifiState() != WifiManager.WIFI_STATE_ENABLING) {
wm.setWifiEnabled(true);
}
}
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.allowedAuthAlgorithms.clear();
wifiConfig.allowedGroupCiphers.clear();
wifiConfig.allowedKeyManagement.clear();
wifiConfig.allowedPairwiseCiphers.clear();
wifiConfig.allowedProtocols.clear();
wifiConfig.SSID = "\"" + networkSSID + "\"";
wifiConfig.priority = 40;
wifiConfig.hiddenSSID = true;
wifiConfig.status = WifiConfiguration.Status.ENABLED;
wifiConfig.wepKeys[0] = "\"" + networkPassword + "\"";
wifiConfig.wepTxKeyIndex = 0;
wifiConfig.preSharedKey = "\"" + networkPassword + "\"";
wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
int netId = wm.addNetwork(wifiConfig);
wm.disconnect();
wm.enableNetwork(netId, false);
wm.reconnect();
This is the code that I write to connect to wireless for WEP with SSID and password, but when I debug the code, it shows ID for WifiConfiguration is -1. It leads to netID having the value of -1, and there is no connection to wireless. Please help! Thanks.
回答1:
I'm pretty sure the configuration is not correct or simply try to set also the BSSID
:
wc.BSSID = yourBSSID;
cause you've an hidden network. You can obtain it from a network scan where the SSID has no value.
来源:https://stackoverflow.com/questions/21937887/android-wificonfiguration-shows-1-for-id-how-can-i-fix-it-for-ssid-to-be-recog