javaandroidkotlinvpnandroid-connectivitymanager

Network Capabilities is showing true, when vpn connected but net disconnected


class CheckPresentConnection(private val context: Context) {

private lateinit var cm: ConnectivityManager

fun isNetworkAvailable(): Boolean{

    cm = context.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
    val capabilities = cm.getNetworkCapabilities(cm.activeNetwork)
    return (capabilities != null && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET))

}

}

when I am checking for network capabilities without any VPN, it is working fine but if I connect to a VPN and then check for capabilities then it is showing that it has capabilities but if VPN is connected but the network is not connected, the capability is still showing true.


Solution

  • You can check real network connection by pinging a site like google.com

    check these answers:

    Android check internet connection

    https://stackoverflow.com/a/22203871/16728174

    if you are using kotlin coroutines:

    https://stackoverflow.com/a/60957561/16728174

    if these info helped you ,please vote this answer.