Code Bye

Android WIFI 连接不加密热点总是失败 addNetwork总是返回-1

忘大神能帮个忙  分全给了 

我连接加密的热点 就可以连接,连接不加密的热点不能连接。addNetwork这个操作总是返回-1 

代码如下:这是connect
  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;  
    }      

10分
哥们帮个忙呗,现在我做这个。也是遇到链接的问题。能否给个源码。。。谢谢了。
20分
顶起同求答案
20分
我的解决了,可以这样
  if(Type == WifiCipherType.WIFICIPHER_NOPASS)
	      {
	    	  config.hiddenSSID = true;  
//	        config.wepKeys[0] = "";
	        config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
//	        config.wepTxKeyIndex = 0;
	      }

把与wep有关的都注释掉就行了

我自己解决办法是这样的 

if (type == SECURITY_NONE) {            
            config.wepKeys[0] = “”” + “””;        
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);  
            config.wepTxKeyIndex = 0;   

还是谢谢各位了

我的也可以,但是我要加上一个 BSSID  就老是返回-1 求大神指点

  wifiCong.SSID = “””+wifi.SSID+”””;//”转义字符,代表” 
////  wifiCong.BSSID= “””+BSSID+”””; 
 wifiCong.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 

引用 4 楼 ERQYY123 的回复:

我自己解决办法是这样的 

if (type == SECURITY_NONE) {            
            config.wepKeys[0] = “”” + “””;        
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);  
            config.wepTxKeyIndex = 0;   

还是谢谢各位了

灰常感谢

加密类型为wpa2怎么写

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Android WIFI 连接不加密热点总是失败 addNetwork总是返回-1