次のコードを使用して、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;
}