Search code examples
androidandroid-wifiwifimanager

Android WifiManager not enabling/disabling WiFi state


I have a mobile application that allows users to enable/disable WiFi on click of a button.

However I noticed today that my app is no longer able to change the WiFi status. It was working since before few weeks. I tried to debug it but the following method always returns false.

boolean result = wifiManager.setWifiEnabled(true);

I am testing it on Samsung Galaxy Note 9 and Android 10.


Solution

  • This API is no longer supported when targeting Android 10 or higher.

    Starting with Build.VERSION_CODES#Q, applications are not allowed to enable/disable Wi-Fi. Compatibility Note: For applications targeting Build.VERSION_CODES.Q or above, this API will always fail and return false. If apps are targeting an older SDK (Build.VERSION_CODES.P or below), they can continue to use this API.

    Instead, you should use the Settings.Panel API to present a system UI allowing users to enable or disable Wi-Fi.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
      startActivity(Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY))
    }