当前位置: 代码迷 >> Android >> Android WIFI 连接不加密热点总是失败 addNetwork总是返回-1,该如何解决
  详细解决方案

Android WIFI 连接不加密热点总是失败 addNetwork总是返回-1,该如何解决

热度:517   发布时间:2016-04-28 06:07:20.0
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;  
  相关解决方案