码迷,mamicode.com
首页 > 移动开发 > 详细

android开发连接wifi addNetwork 返回-1

时间:2015-06-09 17:04:52      阅读:461      评论:0      收藏:0      [点我收藏+]

标签:

如下代码:(注释掉的是会返回-1的代码,未注释的是能够正常连接wifi的代码)

//     public WifiConfiguration CreateWifiInfo(String SSID, String Password,int Type) {
//         WifiConfiguration config = new WifiConfiguration();
//         config.allowedAuthAlgorithms.clear();
//         config.allowedGroupCiphers.clear();
//         config.allowedKeyManagement.clear();
//         config.allowedPairwiseCiphers.clear();
//         config.allowedProtocols.clear();
////         config.SSID = "\"" + SSID + "\"";
//         config.SSID = SSID;
//
//         WifiConfiguration tempConfig = this.IsExsits(SSID);
//         if (tempConfig != null) {
//             mWifiManager.removeNetwork(tempConfig.networkId);
//         }
//
//         if (Type == 1) // WIFICIPHER_NOPASS
//         {
//             config.wepKeys[0] = "";
//             config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
//             config.wepTxKeyIndex = 0;
//         }
//         if (Type == 2) // WIFICIPHER_WEP
//         {
//             config.hiddenSSID = true;
////             config.wepKeys[0] = "\"" + Password + "\"";
//             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;
//         }
//         if (Type == 3) // WIFICIPHER_WPA
//         {
////             config.preSharedKey = "\"" + Password + "\"";
//             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.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
//             config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
//             config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
//             config.status = WifiConfiguration.Status.ENABLED;
//         }
//         return config;
//     }
    public WifiConfiguration CreateWifiInfo(String SSID, String Password,
            int Type) {
        WifiConfiguration config = new WifiConfiguration();
        config.allowedAuthAlgorithms.clear();
        config.allowedGroupCiphers.clear();
        config.allowedKeyManagement.clear();
        config.allowedPairwiseCiphers.clear();
        config.allowedProtocols.clear();
        config.SSID = "\"" + SSID + "\"";

        WifiConfiguration tempConfig = this.IsExsits(SSID);
        if (tempConfig != null) {
            mWifiManager.removeNetwork(tempConfig.networkId);
        }

        if (Type == 1) // WIFICIPHER_NOPASS
        {
            config.wepKeys[0] = "";
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            config.wepTxKeyIndex = 0;
        }
        if (Type == 2) // 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;
        }
        if (Type == 3) // 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.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            config.status = WifiConfiguration.Status.ENABLED;
        }
        return config;
    }

 

有知道为什么ssid和pwd需要加上双引号的大侠请在下面加个评论,感谢!

 

android开发连接wifi addNetwork 返回-1

标签:

原文地址:http://www.cnblogs.com/feijian/p/4563689.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!