2

For my app, I need to make sure the user is connected to wifi before contact with the server. I have found two methods to do so, but I am not sure if one suffices.

First I am adding this:

WifiManager wifiManager = (WifiManager) getActivity().getApplicationContext()
            .getSystemService(WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()) {
        buildAlertNoWifi();
        showProgressDialog(false, "");
        return;
}

And then I am doing this:

ConnectivityManager cm = (ConnectivityManager) getActivity()
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null) { // connected to the internet
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
            // connected to wifi

        } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
            // connected to the mobile provider's data plan
            Toast.makeText(getContext(), "Make sure you connect to wifi.", Toast.LENGTH_LONG).show();
            return;
        }
    } else {
        Toast.makeText(getContext(), "Make sure you connect to wifi.", Toast.LENGTH_LONG).show();
        return;
    }

So I was wondering if wifiManager.isWifiEnabled() returns whether the device is connected to a wifi or just has wifi turned on. And if so, is it enough to use it alone?

riadrifai
  • 1,108
  • 2
  • 13
  • 26
  • Possible duplicate of [How do I see if Wi-Fi is connected on Android?](http://stackoverflow.com/questions/3841317/how-do-i-see-if-wi-fi-is-connected-on-android) – Ashwin Mothilal Mar 19 '17 at 16:02
  • A lot of the answers below suggest the use of now deprecated (as of API 29) methods such as `getActiveNetworkInfo()`, Android now suggests some new ways of dealing with network information, see my answer [here](https://stackoverflow.com/a/59250021/8170714). – Jack Dec 09 '19 at 13:38

3 Answers3

1

I believe WifiManager.isWifiEnabled() only checks if device's wifi is turned on. Please use NetworkInfo.isConnected() or NetworkInfo.isConnectedOrConnecting() to check if it's connected to any network.

Daniel
  • 26
  • 3
  • But `NetworkInfo.isConnected()` would return true even if the device is connected to mobile data right? So a safe way to do it is my 2nd way? – riadrifai Mar 19 '17 at 15:07
  • Yes, you should use it in combination with your check for connectivity type above. – Daniel Mar 19 '17 at 15:41
1

Best Practice

public boolean isWifiConnected() {
    NetworkInfo net = getActiveNetworkInfo();
    return (isConnected(net) && net.getType() == TYPE_WIFI);
}

private NetworkInfo getActiveNetworkInfo() {
    ConnectivityManager connManager = (ConnectivityManager) 
Application.getContext()
            .getSystemService(Application.CONNECTIVITY_SERVICE);
    return connManager.getActiveNetworkInfo();
}
Farid Haq
  • 3,728
  • 1
  • 21
  • 15
0

I believe this should work,

   public boolean isWifiConnected()
    {
        ConnectivityManager cm = (ConnectivityManager)this.mContext.getSystemService(Context.CONNECTIVITY_SERVICE);

        return (cm != null) && (cm.getActiveNetworkInfo() != null) &&
                (cm.getActiveNetworkInfo().getType() == 1);
    }
lakshman.pasala
  • 565
  • 6
  • 16