我试图连接到一个开放的WiFi网络。当我打开我的应用程序应该打开WiFi和连接到如下定义的网络。问题是, WifiManager.getConfiguredNetworks
总是返回我空列表。我已经使用锁太没有成功尝试。
I am trying to connect to an open wifi network. When I open my app it should turn on wifi and connect to the network defined as below. The problem is that WifiManager.getConfiguredNetworks
always returns me an empty list. I have tried using locks too without success.
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
conf.status = WifiConfiguration.Status.ENABLED;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.priority = Integer.MAX_VALUE;
WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
if(list.isEmpty())
{
Log.e("Connection Setup","Empty list returned");
}
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
Log.e("Connection Setup",i.SSID+" connrction attempted");
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
break;
}
}
我一直在尝试更多的东西 - 如果我做这个线程睡眠约10秒钟左右 - 一切工作正常 - 但有没有更好的选择
I have been trying more things - if I make this thread sleep for about 10 seconds or so - everything works fine - but is there a better alternative?
您不能继续
CMD wifiManager.getConfiguredNetworks()
,直到无线状态已启用完全。为使无线网络就需要一定的时间。所以,你需要推迟一段时间。
until the status of WiFi is enabled completely. For enabling the WiFi it need some time. So you need to delay some time.