1.代码 addNetwork并没有把网络添加保存,也无法连接了
public static WifiConfiguration configWifiInfo(Context context, String SSID, String password, int type) {
WifiConfiguration config = null;
WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (mWifiManager != null) {
List<WifiConfiguration> existingConfigs = mWifiManager.getConfiguredNetworks();
for (WifiConfiguration existingConfig : existingConfigs) {
if (existingConfig == null) continue;
if (existingConfig.SSID.equals("\"" + SSID + "\"") /*&& existingConfig.preSharedKey.equals("\"" + password + "\"")*/) {
config = existingConfig;
break;
}
}
}
if (config == null) {
config = new WifiConfiguration();
}
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = "\"" + SSID + "\"";
// 分为三种情况:0没有密码1用wep加密2用wpa加密
if (type == 0) {// WIFICIPHER_NOPASSwifiCong.hiddenSSID = false;
// config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
Log.d("TAG1:", "configWifiInfo: 进入0 ");
config.wepKeys[0] = "";
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
} else if (type == 1) { // WIFICIPHER_WEP
config.hiddenSSID = true;
config.wepKeys[0] = "\"" + password + "\"";
config.allowedAuthAlgorithms
.set(WifiConfiguration.AuthAlgorithm.SHARED);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
config.allowedGroupCiphers
.set(WifiConfiguration.GroupCipher.WEP104);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
} else if (type == 2) { // WIFICIPHER_WPA
config.preSharedKey = "\"" + password + "\"";
config.hiddenSSID = true;
config.allowedAuthAlgorithms
.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.CCMP);
config.status = WifiConfiguration.Status.ENABLED;
}
return config;
}
WifiConfiguration configuration=WifiTest.configWifiInfo(getApplicationContext(),"ee929","",0);
int netId = configuration.networkId;
if (netId == -1) {
netId = mWifiManager.addNetwork(configuration);
}
mWifiManager.enableNetwork(netId, true);
这是干啥???
你传进来的就是
String SSID
,为何还要做隐式转换成String
?转换就转换吧,为啥要这么写?
这么写最后SSID是啥?
SSID = "\"" + "ee929" + "\"" = ""ee929""
???建议你在
int netId = configuration.networkId;
插入log,打印下你的
SSID
和password
,看看还是不是你要的结果。