Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking Wi-Fi enabled or not on Android

What would the code be for checking whether the Wi-Fi is enabled or not?

like image 460
inforg Avatar asked Jul 06 '11 08:07

inforg


People also ask

Why is Wi-Fi not working on Android?

If your Android phone isn't connecting to Wi-Fi, it may be that your router is acting up and not allowing any devices to connect to your wireless network. In this case, it's best to check your router's response using another Wi-Fi-enabled device. Try using another Android or any other device to connect to the network.


4 Answers

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled()) {
  // wifi is enabled
}

For details check here

like image 50
Rasel Avatar answered Oct 18 '22 23:10

Rasel


The above answers work fine. But don't forget to add the right permissions in the Manifest:

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

Hope it helps ..

like image 43
McLan Avatar answered Oct 19 '22 00:10

McLan


The top answer is correct, but not up to date because this code may leak memory on certain devices.

Therefore the better answer would be:

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled()) {
  // wifi is enabled
}

Permissions in app=>mainfests=>AndroidManifest.xml:

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

Reference: https://www.mysysadmintips.com/other/programming/759-the-wifi-service-must-be-looked-up-on-the-application-context

like image 17
KoKlA Avatar answered Oct 19 '22 01:10

KoKlA


public static boolean wifiState() {
    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    return wifiManager.isWifiEnabled();
}
like image 8
XXX Avatar answered Oct 18 '22 23:10

XXX