0

I want to detect that Is Wifi connected to a network? Doesn't matter whether it has Internet connection or not. But my following code returns Disconnected when my Wifi is connected to a network(no internet) and 3G data is enabled.

public static boolean isWifiConnected(ConnectivityManager mConnectivity){
        android.net.NetworkInfo info = mConnectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (info == null ) {
            Log.e("network type","null");
            return false;
        }
        return info.isConnected();
    }

Specification:

Android OS: 5.0.2
Moto g 1st generation
shantanu
  • 2,408
  • 2
  • 26
  • 56

1 Answers1

0

I want to detect that Is Wifi connected to a network? Doesn't matter whether it has Internet connection or not.

If you doesn´t have any internet connection is impossible to determinate of what kind of connection you are disconnected!

More info:

isConnected ()

Indicates whether network connectivity exists and it is possible to establish connections and pass data.

Always call this before attempting to perform data transactions.

Returns true if network connectivity exists, false otherwise.

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • In android 5.0 I noticed that if my wifi does't have internet connection(but connected to a network) then android automatically start 3G data connect(both wifi and 3G are connected simultaneously). – shantanu Jun 23 '15 at 18:53
  • @Elenasys I choose only TYPE_WIFI NetworkInfo (1st line). So basically I'm checking is my WIFI is connected or not. – shantanu Jun 23 '15 at 18:55
  • isConnected () : Returns true if network connectivity exists, false otherwise. – Jorgesys Jun 23 '15 at 18:55
  • Yes, I'm trying to indicate the same thing. My wifi is connected with my router but my router doesn't have internet connection. In this scenario returns false(state == DISCONNECTED). It's happening only in Android >= 5.0 – shantanu Jun 23 '15 at 18:56
  • 1
    @shantanu The behavior of this method must have changed with Android 5.0. I know that on 4.x, it will return true if the device is associated to a WiFi access point even if there is no connectivity, which is what it sounds like you are seeing as well. – Daniel Nugent Jun 23 '15 at 19:54
  • @DanielNugent Is there any way that I can determine if Wifi is connected or not in Android 5.0 when 3G data is enable? – shantanu Jun 24 '15 at 10:21