我尝试使用代码A设置WiFi,我发现有时wifiConfigurations为空
我想wifiManager.isWifiEnabled=aWiFiDef.status
是异步的。
代码A
fun setWiFi(aWiFiDef: WiFiDef): Unit{
val wifiManager =mContext.applicationContext.getSystemService(WIFI_SERVICE) as WifiManager
wifiManager.isWifiEnabled=!wifiManager.isWifiEnabled
wifiManager.isWifiEnabled=aWiFiDef.status //Maybe is asynchronous
val wifiConfigurations = wifiManager.configuredNetworks //Maybe is null
wifiConfigurations?.let{
for (wifiConfiguration in wifiConfigurations) {
if (wifiConfiguration.SSID == "\"" + aWiFiDef.name + "\"" ||wifiConfiguration.SSID == aWiFiDef.name ) {
wifiManager.enableNetwork(wifiConfiguration.networkId, true)
wifiManager.reconnect()
break;
}
}
}
}