How do I see if Wi-Fi is connected on Android?

android check wifi connection programmatically
how to check internet connection in android programmatically
how to check wifi is on or off in android programmatically
how to check network connection in android example
android check internet connection continuously
android check internet connection is available or not
check internet connection in background android
how to check mobile data is on or off in android programmatically

I don't want my user to even try downloading something unless they have Wi-Fi connected. However, I can only seem to be able to tell if Wi-Fi is enabled, but they could still have a 3G connection.

android.net.wifi.WifiManager m = (WifiManager) getSystemService(WIFI_SERVICE);
android.net.wifi.SupplicantState s = m.getConnectionInfo().getSupplicantState();
NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(s);
if (state != NetworkInfo.DetailedState.CONNECTED) {
    return false;
}

However, the state is not what I would expect. Even though Wi-Fi is connected, I am getting OBTAINING_IPADDR as the state.

You should be able to use the ConnectivityManager to get the state of the Wi-Fi adapter. From there you can check if it is connected or even available.

ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (mWifi.isConnected()) {
    // Do whatever
}

NOTE: It should be noted (for us n00bies here) that you need to add

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

to your

AndroidManifest.xml for this to work.

NOTE2: public NetworkInfo getNetworkInfo (int networkType) is now deprecated:

This method was deprecated in API level 23. This method does not support multiple connected networks of the same type. Use getAllNetworks() and getNetworkInfo(android.net.Network) instead.

NOTE3: public static final int TYPE_WIFI is now deprecated:

This constant was deprecated in API level 28. Applications should instead use NetworkCapabilities.hasTransport(int) or requestNetwork(NetworkRequest, NetworkCallback) to request an appropriate network. for supported transports.

How do I see if Wi-Fi is connected on Android?, How do I know if my Android is connected to WIFI? Don’t touch the Master Control icon, which turns the Wi-Fi radio on or off; touch the Wi-Fi text on the left side of the Settings app screen. You see a list of Wi-Fi networks. In the figure, the Imperial Wambooli network is currently connected. Choose a wireless network from the list.

Since the method NetworkInfo.isConnected() is now deprecated in API-23, here is a method which detects if the Wi-Fi adapter is on and also connected to an access point using WifiManager instead:

private boolean checkWifiOnAndConnected() {
    WifiManager wifiMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    if (wifiMgr.isWifiEnabled()) { // Wi-Fi adapter is ON

        WifiInfo wifiInfo = wifiMgr.getConnectionInfo();

        if( wifiInfo.getNetworkId() == -1 ){
            return false; // Not connected to an access point
        }
        return true; // Connected to an access point
    }
    else {
        return false; // Wi-Fi adapter is OFF
    }
}

How to Tell When a Smartphone Is Connected to Wi-Fi, How can I see what devices are connected to my phone? When your phone is near and Wi-Fi is on, your phone automatically connects to this network. Connect via notification. When Wi-Fi is on, you get notifications of available, high-quality, public

I simply use the following:

SupplicantState supState; 
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
supState = wifiInfo.getSupplicantState();

Which will return one of these states at the time you call getSupplicantState();

ASSOCIATED - Association completed.

ASSOCIATING - Trying to associate with an access point.

COMPLETED - All authentication completed.

DISCONNECTED - This state indicates that client is not associated, but is likely to start looking for an access point.

DORMANT - An Android-added state that is reported when a client issues an explicit DISCONNECT command.

FOUR_WAY_HANDSHAKE - WPA 4-Way Key Handshake in progress.

GROUP_HANDSHAKE - WPA Group Key Handshake in progress.

INACTIVE - Inactive state.

INVALID - A pseudo-state that should normally never be seen.

SCANNING - Scanning for a network.

UNINITIALIZED - No connection.

Monitor connectivity status and connection metering, You can use the ConnectivityManager to check that you're connected to the internet, Device connectivity can be provided by mobile data, WiMAX, Wi-Fi, and  On Android, how do you tell if you're connected through Wi-Fi or some other type of connection like LTE or 3G? If I'm sitting at a Starbucks and I sign-in to their Wi-Fi, my phone has both the Wi-Fi and LTE icons showing.

I am using this in my apps to check if the active network is Wi-Fi:

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni != null && ni.getType() == ConnectivityManager.TYPE_WIFI)
{

    // Do your work here

}

Manage network usage, When you have Wi-Fi turned on, your phone automatically connects to nearby Wi​-Fi networks you've connected to before. You can also set your phone to  Disconnect and Connect Again The first rule of IT-related fix is turning it off and on again, it fixes about 50 percent problems. So, if your phone is not connecting to the internet even if the phone is connected to the Wifi router. Go to the settings and turn the Wifi toggle off and on again and see if it fixes your issue.

I had a look at a few questions like this and came up with this:

ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile = connManager .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

if (wifi.isConnected()){
    // If Wi-Fi connected
}

if (mobile.isConnected()) {
    // If Internet connected
}

I use if for my license check in Root Toolbox PRO, and it seems to work great.

Connect to Wi-Fi networks on your Android device, If other devices can use the network, the problem is most likely with your phone. Network: Check whether your phone can connect to another Wi-Fi network, like at​  Navigate to System->etc->WiFi and open wpa_supplicant.conf file. If the file manager app asks you how to open the selected configuration file, select the built-in HTML or the text file viewer. Once you open the file, you will be able to view all the passwords of the connected WiFi networks using your Android phone.

Fix Wi-Fi connection problems - Android Help, If you've already configured your phone to connect to an available wireless network, it's connected automatically. Otherwise, you have to connect to an available  If you've connected to many different Wi-Fi networks over the course of owning your Android device, your list may be quite long. If that's the case, you can search for a particular Wi-Fi network by using the search button in the top-right corner. Step 3 Share Saved Wi-Fi Passwords

How to Access Wi-Fi on an Android Phone, In this video you are going to learn how to check if your android device is connected to Duration: 6:48 Posted: May 28, 2019 On the WiFi screen, make sure that WiFi is set to ON position and your WiFi Network shows as being connected (See image below). Toggle WiFi On/OFF. The simple act of Switching WiFi Network ON/OFF is known to fix the problem of Android Phone Not Connecting to WiFi Network. 1. Tap on Settings > WiFi. 2.

How to Check If Device Connected to WiFi/Mobile Network or Offline , You can connect to Wi-Fi on an Android device through the "Connections" (or "​Network & Internet") menu, which is in your Settings app. If  How to See Who's On Your Wi-Fi. Is your internet sluggish? If you suspect a neighbor is stealing your Wi-Fi, these two apps can help you identify devices using your connection and help you boot

Comments
  • It should be noted (for us noobies here) that you need to add android.permission.ACCESS_NETWORK_STATE to your AndroidManifest.xml for this to work.
  • In very recent versions of Android, you need to check for NULL in mWiFi ... your code here could throw a null pointer error. See developer.android.com/training/basics/network-ops/managing.html and in particular "The method getActiveNetworkInfo() returns a NetworkInfo..."
  • It worked for me with Ethernet interface as well. I just changed to ConnectivityManager.TYPE_ETHERNET
  • The method NetworkInfo.isConnected() is now depreceated in API-23. I am posting a solution below .
  • NetworkInfo.getType() and ConnectivityManager.TYPE_WIFI are now deprecated in API 28. To avoid lint warning you should use something like connectivityManager.getNetworkCapabilities(network).hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
  • It's worth mentioning that wifiInfo can be null so I think you should check for null before getting network id
  • NetworkInfo.isConnected() doesn't look deprecated to me.
  • Don't forget to add permission: android.permission.ACCESS_WIFI_STATE
  • This will no longer work in Android Q without location permission and location mode turned on, see issuetracker.google.com/issues/136021574.
  • @EricRobertBrewer comment no longer up to date. it is now, deprecated API 29+.
  • hi Donal.I have used the same way to get whether our device is connected to wifi.But additionally i need to know the App name currently using WIFI.How can that b done?
  • @AbhishekB, sorry but I don't have any experience with that, perhaps try looking at some of the Wi-Fi monitoring apps, see if there is an open source one where you can review the code.
  • I'm suspicious of this solution because the supplicant is only used if WPA (or some variation of WPA) is usesd: if user connects to an AP with no authentication or WEP then the supplicant is not involved.
  • It still shows up as "COMPLETED" even though I'm not on wifi