2

I try to get the internet status from a WiFi network. So I can raise a notification when the device doesn't have an internet connection through WiFi. So the way I found to do it now with a not deprecated method is by using ConnectinityManager.NetworkCallback (https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback)

I succeeded check when the WiFi is disconnected or connected by using .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)

And I succeded check when a device doesn't have Internet data with .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)

But what I want is a callback to detect if there is internet or not through the WiFi it's connected. So I tried to add both items above, but it seems it doesn't work that way. The test I made is connecting a device to a hotspot, then disable the data from the hotspot. But it doesn't trigger the callback.

Here is the code I used (thanks to ConnectivityManager.NetworkCallback() -> onAvailable(Network network) method is not triggered when device connects to a internal wifi network) that has a related issue:

import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
}

private val networkCallback = getNetworkCallBack()

private fun getNetworkCallBack(): ConnectivityManager.NetworkCallback {
    return object : ConnectivityManager.NetworkCallback() {
        override fun onAvailable(network: Network) {
            super.onAvailable(network)
            Toast.makeText(applicationContext, "Internet is on!", Toast.LENGTH_SHORT).show()
        }

        override fun onLost(network: Network) {
            super.onLost(network)
            Toast.makeText(applicationContext, "Internet turns off!", Toast.LENGTH_SHORT).show()
        }
    }
}

private fun getConnectivityManager() = applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

private fun getNetworkRequest(): NetworkRequest {
    return NetworkRequest.Builder()
            .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) // internet check works without this but need it for WiFi status
            .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
            .build()
}

override fun onResume() {
    super.onResume()
    getConnectivityManager().registerNetworkCallback(getNetworkRequest(), networkCallback)
}

override fun onPause() {
    super.onPause()
    getConnectivityManager().unregisterNetworkCallback(networkCallback)
}

}
Brian
  • 303
  • 4
  • 16

1 Answers1

2

To check for internet connectivity, you need to include NET_CAPABILITY_VALIDATED in your query.

public static final int NET_CAPABILITY_VALIDATED

Indicates that connectivity on this network was successfully validated. For example, for a network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully detected.

Therefore you could update your code like so:

private fun getNetworkRequest(): NetworkRequest {
    return NetworkRequest.Builder()
            .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) // internet check works without this but need it for WiFi status
            .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
            .addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
            .build()
}
Always Learning
  • 2,623
  • 3
  • 20
  • 39
  • Unfortunately I also tried this but that did not trigger the callback either. When I turn off data from my hotspot, nothing happen. It is only triggered when the wifi is turned off. – Brian Mar 24 '21 at 20:33
  • Interesting. Typically, internet + validated would give you the answer you are looking for. When you say wifi is "turned off", where are you turning it off at? On the hotspot device? Also, can you override `onCapabilitiesChanged` in `getNetworkCallBack()`? I'm hoping you'll see that event trigger for your use case. Thanks! – Always Learning Mar 24 '21 at 21:07
  • When I turn off the hotspot or the wifi on the device itself it's working for checking if there is wifi or not but this is not what I need of course. I overrided ```onCapabilitiesChanged ``` but I don't see any logs here when I disable data on my hotspot device, I see logs when I disable wifi though. – Brian Mar 25 '21 at 07:41
  • Thanks for the info Brian. In that case, when you say you "disable data from the hotspot", can you perhaps update your question with with the specifics of what exactly you are doing? Thanks. – Always Learning Mar 25 '21 at 16:28