How to See If Wi-Fi Is Connected on Android

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

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 to check if Wifi network has internet access?

I've found a solution that is quick and does not require using ping command or having to load a page.

The solution uses Volley, Android's HTTP library:

public static void isInternetAccessWorking(Context context, final InternetAccessListener listener) {

StringRequest stringRequest = new StringRequest(Request.Method.GET, "https://www.google.com",
new Response.Listener<String>() {

@Override
public void onResponse(String response) {
listener.hasInternet(true);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
listener.hasInternet(false);
}
});

Volley.newRequestQueue(context).add(stringRequest);
}

This method is not blocking: the activity or fragment calling isInternetAccessWorking() has to provide a parameter implementing InternetAccessListener that will receive the response

public interface InternetAccessListener {
void hasInternet(Boolean result);
}

Detect whether there is an Internet connection available on Android

The getActiveNetworkInfo() method of ConnectivityManager returns a NetworkInfo instance representing the first connected network interface it can find or null if none of the interfaces are connected. Checking if this method returns null should be enough to tell if an internet connection is available or not.

private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager != null ? connectivityManager.getActiveNetworkInfo() : null;
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

You will also need:

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

in your android manifest.

Edit:

Note that having an active network interface doesn't guarantee that a particular networked service is available. Network issues, server downtime, low signal, captive portals, content filters and the like can all prevent your app from reaching a server. For instance you can't tell for sure if your app can reach Twitter until you receive a valid response from the Twitter service.

Checking if internet connection over WiFi or mobile data available

First ask for this permission to the manifest.xml : android.permission.ACCESS_NETWORK_STATE

In android, we can determine the internet connection
status easily by using WIFIMANAGER.

private boolean LookForWifiOnAndConnected() {
WifiManager wifi_m = (WifiManager)
getSystemService(Context.WIFI_SERVICE);

if (wifi_m.isWifiEnabled()) { // if user opened wifi

WifiInfo wifi_i = wifi_m.getConnectionInfo();

if( wifi_i.getNetworkId() == -1 ){
return false; // Not connected to any wifi device
}
return true; // Connected to some wifi device
}
else {
return false; // user turned off wifi
}

}

// check if mobile data on or off

  boolean mobileDataEnabled = false; // Assume disabled
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
Class cmClass = Class.forName(cm.getClass().getName());
Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
method.setAccessible(true); // Make the method callable
// get the setting for "mobile data"
mobileDataEnabled = (Boolean)method.invoke(cm);
} catch (Exception e) {
// Some problem accessible private API
// TODO do whatever error handling you want here
}

How To Check if WiFi Enabled On Phone

Checking the state of WiFi can be done by obtaining an instance to the WiFi system service as below:

WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);

from this, the method isWifiEnabled() can be used to determine if WiFi is enabled. As below:

WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled()){
//TODO: Code to execute if wifi is enabled.
}


Related Topics



Leave a reply



Submit