博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android开发连接wifi addNetwork 返回-1
阅读量:7112 次
发布时间:2019-06-28

本文共 4836 字,大约阅读时间需要 16 分钟。

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

 

转载地址:http://vjghl.baihongyu.com/

你可能感兴趣的文章
(笔记)VC6插件安装--Unable to register this add-in because its DllRegisterServer returns an error...
查看>>
【.net 深呼吸】细说CodeDom(7):索引器
查看>>
monolog使用
查看>>
【AtCoder010】B - Boxes(差分)
查看>>
三种 Failover 之 Client-Side Connect time Failover、Client-Side TAF、Service-Side TAF
查看>>
ES 相似度算法设置(续)
查看>>
46:八进制到十进制
查看>>
JAVA4种线程池的使用
查看>>
MonkeyRunner 模块
查看>>
ASP.NET Core实现OAuth2.0的AuthorizationCode模式
查看>>
Atitit tomcat在linux服务器的启动与其他
查看>>
C++/Php/Python 语言执行shell命令
查看>>
Oracle表空间维护总结
查看>>
12C -- ORA-01017
查看>>
约瑟夫环问题
查看>>
Compile、Make和Build的区别(as make, build, clean, run)
查看>>
介绍三款串口监控工具:Device Monitoring Studio,portmon,Comspy
查看>>
Bulk Load-HBase数据导入最佳实践
查看>>
sqlServer的主键只能自增不能手动增加
查看>>
maven常用命令介绍
查看>>