Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internet check over android.net.NetworkCapabilities not working

I have a question about android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET. According to the JavaDocs this "indicates that this network should be able to reach the internet".

Unfortunately this seems not to work properly - or I do something wrong here. I outputed the value of capabilities for three different Wi-Fi networks:

  1. A Wi-Fi with internet access.

  2. A Wi-Fi without internet access (I manually disabled the internet here).

  3. A public hotspot (the "Telekom" hotspot)

In all three cases the capabilities value is [ Transports: WIFI Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VPN LinkUpBandwidth>=1048576Kbps LinkDnBandwidth>=1048576Kbps].

This is my code:

ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
boolean capability = capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET

The boolean value is always true when connected with some Wi-Fi.

If it is relevant, the device I've tested is running under Android 5.0.1 (API 21). I compiled the app to API level 23.

like image 638
Timo Bähr Avatar asked Apr 01 '16 14:04

Timo Bähr


People also ask

What is network Capabilities?

Definition. Network capabilities refer to short-term authorizations that a sender obtains from a receiver and stamps on its packets to the receiver.

How do you check Internet is on or off in android programmatically?

In android, we can determine the internet connection status easily by using getActiveNetworkInfo() method of ConnectivityManager object. Following is the code snippet of using the ConnectivityManager class to know whether the internet connection is available or not.

What is the role of network capabilities class in Android app?

This replaces the old ConnectivityManager#TYPE_MOBILE method of network selection. Rather than indicate a need for Wi-Fi because an application needs high bandwidth and risk obsolescence when a new, fast network appears (like LTE), the application should specify it needs high bandwidth.


2 Answers

For devices with API 23+ you can check flag NET_CAPABILITY_VALIDATED

@RequiresApi(api = Build.VERSION_CODES.M)
private static boolean hasInternetConnectionM(final Context context) {
    final ConnectivityManager connectivityManager = (ConnectivityManager)context.
            getSystemService(Context.CONNECTIVITY_SERVICE);

    final Network network = connectivityManager.getActiveNetwork();
    final NetworkCapabilities capabilities = connectivityManager
            .getNetworkCapabilities(network);

    return capabilities != null
            && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
}

I check it on different devices and it works right. Except Xiaomi Redmi 3S MIUI 8 - it returns NET_CAPABILITY_VALIDATED for Wi-Fi without internet access.

For API < 23 I use ping.

like image 176
EVIL SnaKe Avatar answered Sep 21 '22 17:09

EVIL SnaKe


Unfortunately, you can't rely on NET_CAPABILITY_INTERNET && NET_CAPABILITY_VALIDATED, you can just turn off the internet on your wifi router (by cable or programatically) and check - both of those parameters will be true even after a long delay.

like image 42
blinker Avatar answered Sep 22 '22 17:09

blinker