Search code examples
androidwifi

How to check Wi-Fi switch state 'ON' or 'OFF' even if not connected to a WiFi network on Android


I need to develop a feature that is required checking Wi-Fi settings state 'ON' or 'OFF' even if not connected to a WiFi network on Android! I need to display a pop up!

 public static boolean CheckWifiConnection(Context context) {
    ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService (Context.CONNECTIVITY_SERVICE);
    return conMgr.getActiveNetworkInfo() != null
            && conMgr.getActiveNetworkInfo().isAvailable();
}

For an example: Think I am in a bus, I have turned on the WiFi. It is not connected to a wifi network, because there is no WiFi router near by. So How can my app knows wifi is on or off.

enter image description here


Solution

  • For this question I could use WifiManager.

    public static boolean CheckWifiSwitch(Context context) {
        WifiManager wifiMgr = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        return wifiMgr.isWifiEnabled();
    }
    

    WifiManager