我正在使用以下代码在 android 中连接 WPA2(我可以连接 WEP 和 WPA)。但我只得到“扫描”状态。而且我无法连接到 WPA2 网络。你能告诉我我需要做哪些更改才能使此代码与 wpa2 WiFi 相关。
private boolean saveWepConfigAndEnableNetwork(String ssid, String pass) {
isAlreadyPresend = false;
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"" + ssid + "\""; // IMP! This should be in Quotes!!
wc = checkPreviousConfiguration(wc);
wc.hiddenSSID = true;
wc.status = WifiConfiguration.Status.DISABLED;
wc.priority = 40;
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
wc.preSharedKey = "\"" + pass + "\"";
wc.wepKeys[0] = "\"" + pass + "\""; // This is the WEP Password
wc.wepTxKeyIndex = 0;
boolean res1 = wifi.setWifiEnabled(true);
int res = 0;
if(isAlreadyPresend){
res = wifi.addNetwork(wc);
}else{
res = wifi.updateNetwork(wc);
}
Log.d("WifiPreference", "add Network returned " + res);
boolean es = wifi.saveConfiguration();
Log.d("WifiPreference", "saveConfiguration returned " + es);
boolean b = wifi.enableNetwork(res, true);
Log.d("WifiPreference", "enableNetwork returned " + b);
return b;
}
// Check if this SSID is already stored. If it is, return that
// configuration.
// If not, return the configuration being tested.
public WifiConfiguration checkPreviousConfiguration(WifiConfiguration wc) {
List<WifiConfiguration> configs = wifi.getConfiguredNetworks();
for (WifiConfiguration config : configs) {
if (config.SSID.equals(wc.SSID)){
isAlreadyPresend = true;
return config;
}
}
return wc;
}