android-WifiManager.getConfiguredNetworks始终返回空列表
作者:互联网
我正在尝试连接到开放的wifi网络.当我打开我的应用程序时,应打开wifi并连接到如下定义的网络.问题是WifiManager.getConfiguredNetworks总是向我返回一个空列表.我也尝试过使用锁,但没有成功.
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秒钟-一切正常,但是还有更好的选择吗?
解决方法:
您无法继续
CMD wifiManager.getConfiguredNetworks()
直到完全启用WiFi状态.要启用WiFi,需要一些时间.因此,您需要延迟一些时间.
标签:android,wifi,android-wifi 来源: https://codeday.me/bug/20191013/1908136.html