android开发连接wifi addNetwork 返回-1

如下代码:(注释掉的是会返回-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需要加上双引号的大侠请在下面加个评论,感谢!

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。