如何以编程方式扫描、查找并连接到开放的 WiFi AP?
我正在开发一个应用程序,它应该自动并定期启用 wifi、扫描网络、过滤掉开放的网络(不需要通行证)、创建 wifiConfiguration 对象并使用它连接到该网络。
我阅读了 android 开发人员的 wifiConfiguration API。基于它,我编写了一个代码示例,它确实启用了 wifi,扫描网络并将结果保存在一个列表中,如下所示:
11-25 16:05:44.191: I/WIFISCAN(12955): List of networks: [SSID: airlive_w, BSSID: 00:4f:62:2c:96:18, capabilities: [WPA-PSK-CCMP][WPA2-PSK-CCMP][WPS], level: -71, frequency: 2432, SSID: Mikynet, BSSID: 00:1e:2a:ed:62:4e, capabilities: [WPA2-PSK-CCMP], level: -72, frequency: 2427, SSID: TP-LINK_Vectra, BSSID: 74:ea:3a:ab:eb:b0, capabilities: [WPA2-PSK-TKIP+CCMP][WPS], level: -73, frequency: 2462, SSID: Nasza Siec- ryby, BSSID: d8:5d:4c:df:60:74, capabilities: [WPA2-PSK-TKIP+CCMP-preauth], level: -88, frequency: 2437, SSID: lanzarote, BSSID: 00:27:19:f7:05:9c, capabilities: [WPA-PSK-TKIP+CCMP][WPA2-PSK-TKIP+CCMP-preauth], level: -89, frequency: 2437, SSID: Alicja, BSSID: 94:44:52:a7:17:02, capabilities: [WPA-PSK-CCMP][WPA2-PSK-CCMP][WPS], level: -89, frequency: 2442, SSID: orangebox, BSSID: 00:1f:f3:f8:ea:0f, capabilities: [WPA2-PSK-CCMP], level: -89, frequency: 2462]
有了这个结果,我如何知道其中哪个是打开的(免费通过)? 如何为开放的 wifiConfiguration 创建 wifiConfiguration?
这是 WPA-PSK 的 wifiConfiguration 示例:
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wc = new WifiConfiguration();
// This is must be quoted according to the documentation
// http://developer.android.com/reference/android/net/wifi/WifiConfiguration.html#SSID
wc.SSID = "\"SSIDName\"";
wc.preSharedKey = "\"password\"";
wc.hiddenSSID = true;
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
int res = wifi.addNetwork(wc);
Log.d("WifiPreference", "add Network returned " + res );
boolean b = wifi.enableNetwork(res, true);
Log.d("WifiPreference", "enableNetwork returned " + b );
任何指导都将受到高度赞赏! :)
I am developing an application which should automarically and periodically enable wifi, scan for networks, filter out networks that are open (no pass needed), create a wifiConfiguration object and use it to connect to that network.
I read the wifiConfiguration API on android developers. And based on it I have written a sample of code which does enable the wifi, scans for networks and saves the result in a list which looks like that:
11-25 16:05:44.191: I/WIFISCAN(12955): List of networks: [SSID: airlive_w, BSSID: 00:4f:62:2c:96:18, capabilities: [WPA-PSK-CCMP][WPA2-PSK-CCMP][WPS], level: -71, frequency: 2432, SSID: Mikynet, BSSID: 00:1e:2a:ed:62:4e, capabilities: [WPA2-PSK-CCMP], level: -72, frequency: 2427, SSID: TP-LINK_Vectra, BSSID: 74:ea:3a:ab:eb:b0, capabilities: [WPA2-PSK-TKIP+CCMP][WPS], level: -73, frequency: 2462, SSID: Nasza Siec- ryby, BSSID: d8:5d:4c:df:60:74, capabilities: [WPA2-PSK-TKIP+CCMP-preauth], level: -88, frequency: 2437, SSID: lanzarote, BSSID: 00:27:19:f7:05:9c, capabilities: [WPA-PSK-TKIP+CCMP][WPA2-PSK-TKIP+CCMP-preauth], level: -89, frequency: 2437, SSID: Alicja, BSSID: 94:44:52:a7:17:02, capabilities: [WPA-PSK-CCMP][WPA2-PSK-CCMP][WPS], level: -89, frequency: 2442, SSID: orangebox, BSSID: 00:1f:f3:f8:ea:0f, capabilities: [WPA2-PSK-CCMP], level: -89, frequency: 2462]
Having that result how am I going to know which of them is open (pass free)?
And how do I create the wifiConfiguration for an open one?
Here is a sample wifiConfiguration for a WPA-PSK:
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wc = new WifiConfiguration();
// This is must be quoted according to the documentation
// http://developer.android.com/reference/android/net/wifi/WifiConfiguration.html#SSID
wc.SSID = "\"SSIDName\"";
wc.preSharedKey = "\"password\"";
wc.hiddenSSID = true;
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
int res = wifi.addNetwork(wc);
Log.d("WifiPreference", "add Network returned " + res );
boolean b = wifi.enableNetwork(res, true);
Log.d("WifiPreference", "enableNetwork returned " + b );
Any guidance will be highly apreciated! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,您可能需要尝试向 Wifi 服务添加开放配置。如果添加开放网络失败,则可能需要某种身份验证协议。
您还可以将其与
getConfiguredNetworks
方法结合使用,以查看附近的网络 a) 是否先前已连接且 b) 是否已打开。From what I can tell, you might have to try adding an open configuration to the Wifi service. If adding an open network fails, it probably requires some kind of auth protocol.
You could also use this in conjunction with the
getConfiguredNetworks
method to see if any nearby networks a) have been previously connected to and b) are open.