本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《
阿里云开发者社区用户服务协议
》和
《
阿里云开发者社区知识产权保护指引
》。如果您发现本社区中有涉嫌抄袭的内容,填写
侵权投诉表单
进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
public static boolean Connect(String SSID, String Password, int Type) {
WifiConfiguration wifiConfig = createWifiInfo(SSID, Password, Type);
if (wifiConfig == null) {
Log.v(TAG,"wifiConfig == null");
return false;
int netID = settingWifiAdmin.wifiManager.addNetwork(wifiConfig);
Log.v(TAG,"netID = "+netID);//连不加密AP总是返回-1
boolean bRet = settingWifiAdmin.wifiManager.enableNetwork(netID, true);
return bRet;
* 配置要连接的WIFI热点信息
* @param SSID
* @param password
* @param type 加密类型
* @return
public static WifiConfiguration createWifiInfo(String SSID, String password, int type) {
Log.e(TAG, "SSID = " + SSID + "## Password = " + password + "## Type = " + type);
WifiConfiguration config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = """ + SSID + """;
//增加热点时候 如果已经存在SSID 则将SSID先删除以防止重复SSID出现
WifiConfiguration tempConfig = wifiAdmin.IsExsits(SSID);
if (tempConfig != null) {
settingWifiAdmin.wifiManager.removeNetwork(tempConfig.networkId);
// 分为三种情况:没有密码 用wep加密 用wpa加密
if (type == SECURITY_NONE) { // WIFICIPHER_NOPASS
config.wepKeys[0] = "";
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
} else if (type == SECURITY_WEP) { // 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 == SECURITY_PSK) { // 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;
解决办法:
if(Type == WifiCipherType.WIFICIPHER_NOPASS)
{
config.hiddenSSID = true;
// config.wepKeys[0] = "";
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
// config.wepTxKeyIndex = 0;
}
把与wep有关的都注释掉就行了