Wifi连接有密码的SSID是wifiManager.addNetwork返回为-1

           这些天发现以前原本写好的Wifi工具在连接Wifi的时候出问题了,甚至会直接崩溃,这对于app开发者来说是一个致命的伤害,反正我现在已经遍体鳞伤了。

        连接Wifi的流程:

               1、如果是没有密码的Wifi,直接连接即可;

               2、如果是有密码的Wifi,需要按Wifi密码加密的加密情况来连接;

        废话不多说,先上代码:

        在修改之前连接的代码:

           public WifiConfiguration CreateWifiInfo(ScanResult scan, String Password) {
		WifiConfiguration config = new WifiConfiguration();
		config.hiddenSSID = false;
		config.status = WifiConfiguration.Status.ENABLED;
		if (scan.capabilities.contains("WEP")) {
			config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
			config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
			config.SSID = "\"" + scan.SSID + "\"";
			config.wepTxKeyIndex = 0;
			config.wepKeys[0] = Password;
		} else if (scan.capabilities.contains("PSK")) {
			config.SSID = "\"" + scan.SSID + "\"";
			config.preSharedKey = "\"" + Password + "\"";
		} else if (scan.capabilities.contains("EAP")) {
			config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
			config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
			config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
			config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
			config.SSID = "\"" + scan.SSID + "\"";
			config.preSharedKey = "\"" + Password + "\"";
		} else {
			config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
			config.SSID = "\"" + scan.SSID + "\"";
			config.preSharedKey = null;
			config.wepKeys[0] = "\"" + "\"";
			config.wepTxKeyIndex = 0;
		}
		return config;
	   }
        在连接Wifi的时候,执行到这里,然后去addNetwork的时候,返回的netId为-1,之后,由于没有做netId做判断,程序就一直在这里执行,也不返回任何东西。添加判断之后,发现,有密码的Wifi还是连接不上。然后在网上找了资料关于addNetwork返回值为-1的解决方案,采纳建议之后,确实可以正常连接我Wifi。

           WifiConfiguration config = new WifiConfiguration();
		config.hiddenSSID = false;
		config.SSID=scan.SSID;
		config.status = WifiConfiguration.Status.ENABLED;
		/*if (scan.capabilities.contains("WEP")) {
			config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
			config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
			config.SSID = "\"" + scan.SSID + "\"";
			config.wepTxKeyIndex = 0;
			config.wepKeys[0] = Password;
		} else */if (scan.capabilities.contains("WPA")) {
			
			config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
			config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
			config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
			config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
			config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
			config.preSharedKey = "\"".concat(Password).concat("\"");
//			config.SSID = "\"" + scan.SSID + "\"";
			
//			config.preSharedKey = "\"" + Password + "\"";
		} else if (scan.capabilities.contains("WEP")) {
			
			config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
			config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
			config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
			config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
			config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
			config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
			config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);

	        if (getHexKey(Password)) config.wepKeys[0] = Password;
	        else config.wepKeys[0] = "\"".concat(Password).concat("\"");
	        config.wepTxKeyIndex = 0;
			
//			config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
//			config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
//			config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
//			config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
//			config.SSID = "\"" + scan.SSID + "\"";
//			config.preSharedKey = "\"" + Password + "\"";
		} else {
//			config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
//			config.SSID = "\"" + scan.SSID + "\"";
			config.preSharedKey = null;
			config.wepKeys[0] = "\"" + "\"";
			config.wepTxKeyIndex = 0;
			config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
			config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
			config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
			config.allowedAuthAlgorithms.clear();
			config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
			config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
		}
		return config;

        我仔细对比一下了两者代码间的区别,只不是资料里面的config信息比较全而已,其实也没什么区别。

        后来我想了想,是不是Wifi SSID的问题,在项目里面处理SSID是需要将SSID前面和后面的“\”都要去掉,那么scanResult的SSID是不是本身就有“\”呢?

        我测试了一下,发现也没有啊,会不会是显示的问题?于是,我直接把scanResult的SSID赋值给了config的SSID,这确实可以正常的连接Wifi

         public WifiConfiguration CreateWifiInfo(ScanResult scan, String Password) {
		WifiConfiguration config = new WifiConfiguration();
		config.hiddenSSID = false;
		config.SSID=scan.SSID;
		config.status = WifiConfiguration.Status.ENABLED;
		if (scan.capabilities.contains("WEP")) {
			config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
			config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
//			config.SSID = "\"" + scan.SSID + "\"";
			config.wepTxKeyIndex = 0;
			config.wepKeys[0] = Password;
		} else if (scan.capabilities.contains("PSK")) {
//			config.SSID = "\"" + scan.SSID + "\"";
			config.preSharedKey = "\"" + Password + "\"";
		} else if (scan.capabilities.contains("EAP")) {
			config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
			config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
			config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
			config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
//			config.SSID = "\"" + scan.SSID + "\"";
			config.preSharedKey = "\"" + Password + "\"";
		} else {
			config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
//			config.SSID = "\"" + scan.SSID + "\"";
			config.preSharedKey = null;
			config.wepKeys[0] = "\"" + "\"";
			config.wepTxKeyIndex = 0;
		}
		return config;
	}
        难道是SSID 搞的鬼?初步判断并不是config不全的问题。

        新手菜鸟,希望大家喜欢,如果对你有帮助别忘了,别忘了点赞哦。

你可能感兴趣的:(WiFi)