How to find if Wi-Fi configuration is disabled in device manager policy?

643 views Asked by At

I wanted to know if there is a way to find out if wifi configuration is enabled/disabled via code. I can use the following code:

UserManager um = (UserManager) context
                .getSystemService(Context.USER_SERVICE);

        Bundle restrictions = um.getUserRestrictions();
        LogUtil.d(TAG, "restrictions bundle = " + restrictions.toString());
        if (restrictions
                .containsKey(UserManagerUtils.DISALLOW_CONFIG_WIFI)) {
            boolean isWiFiDisabled = restrictions
                    .getBoolean(UserManagerUtils.DISALLOW_CONFIG_WIFI);
            LogUtil.d(
                    TAG,
                    "restrictions DISALLOW_CONFIG_WIFI = "
                            + isWiFiDisabled );
        }

But i do not want to use this code.

Usually to find if the wi-fi configuration policy is disabled, we can go to wi-fi settings. If we can see a list of available and/or connected wi-fi networks, it means that the wi-fi configuration is enabled by our device owner. When the device owner disables wi-fi config, we cannot see list of available/connected networks.However in this case, if we are already connected to some network, then we can still have access in internet via that network.

Please let me know if there is any other way to find out the wi-fi configuration status.

1

There are 1 answers

3
Sunny Rathor On

Use the following to check if it's enabled or not

boolean wifiEnabled = wifiManager.isWifiEnabled();

but before that You need the following permissions in your manifest file:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
  <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>

Then you can use the following in your activity class for enabled/disabled:

WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); 
wifiManager.setWifiEnabled(true);
wifiManager.setWifiEnabled(false);