实现自动WiFi连接


1,想实现连接到指定的WiFi网络吗?

其实这个不是很难,可这个一直困扰了我很长时间,今天终于找到了原因,因为,我之前的配置就是错误的,所以,导致无法连接到指定的无线网络。

2,实现方法:

请参考以下的代码(特别是有密码与没有密码的部分,他们的配置都不尽相同,所以,一定得细心):

WifiConfiguration config = new WifiConfiguration(); config.allowedAuthAlgorithms.clear(); config.allowedGroupCiphers.clear(); config.allowedKeyManagement.clear(); config.allowedPairwiseCiphers.clear(); config.allowedProtocols.clear(); config.SSID = "\"" + SSID + "\""; if(Type == NOPASS) { config.wepKeys[0] = ""; config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); config.wepTxKeyIndex = 0; } if(Type == WEP) { config.preSharedKey = "\""+Password+"\""; config.hiddenSSID = true; 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 == 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.status = WifiConfiguration.Status.ENABLED; }

3,下面是实现的具体关键代码(仅用了没有密码的实现方式,其他的可以自己去实现):

List<ScanResult> sRet = mWifi.getScanResults(); for (int i=0; i<sRet.size(); i++) { ScanResult retS = sRet.get(i); txt.append("resT: " + retS.SSID +" " + retS.BSSID + "\n"); Log.d(tag, "resT: " + retS.SSID +" " + retS.BSSID); if (retS.SSID.equalsIgnoreCase("Shuttled")) { txt.append("Found: " + retS.SSID +" " + retS.BSSID + "\n"); WifiConfiguration wc = new WifiConfiguration(); wc.allowedAuthAlgorithms.clear(); wc.allowedGroupCiphers.clear(); wc.allowedKeyManagement.clear(); wc.allowedProtocols.clear(); wc.allowedPairwiseCiphers.clear(); wc.SSID = "\""+retS.SSID+"\""; wc.wepKeys[0]=""; wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); wc.wepTxKeyIndex = 0; int netID = mWifi.addNetwork(wc); // add network // txt.append("addNetwork: "+ Integer.toString(netID) +"\n"); // mWifi.saveConfiguration(); if(mWifi.enableNetwork(netID, true)) // enable network { txt.append("enableNetwork: true\n"); }

4,在StackOverflow上,有位高人,让通过广播来实现(通知扫描完成事件),下面是实现代码:

private BroadcastReceiver scanResultsReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context arg0, Intent intent) { String action = intent.getAction(); if (action.equals("android.net.wifi.SCAN_RESULTS")) { do connect????? } } /* 注册事件时,需要添加行为 */ IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("android.net.wifi.SCAN_RESULTS"); registerReceiver(scanResultsReceiver, intentFilter);
5,完成,将近快10月了,才知道怎么去弄,也许是关注度不够吧。唉~~~,加油。。。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值