我正在开发一个应用程序,它应该自动和定期启用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]
有了这个结果我怎么知道哪些是开放的(通过免费)? 如何为开放的wifi创建wifi配置?
以下是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 );
任何指导都会非常苛刻! :)
答案 0 :(得分:2)
据我所知,您可能需要尝试向Wifi服务添加开放配置。如果添加开放网络失败,则可能需要某种身份验证协议。
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "some ssid from the list";
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
int networkId = wifi.addNetwork(wc);
if (networkId == -1) {
// probably not open
} else {
// likely open
}
您还可以将此功能与getConfiguredNetworks
方法结合使用,以查看附近的网络a)是否先前已连接过,并且b)是否已打开。