是否有用于更改WiFi状态的回调函数?

时间:2018-06-26 02:41:35

标签: android

我尝试使用代码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;
                }
            }
        }

    }

0 个答案:

没有答案