Wi-fi is on but No internet connection check Android

69 Views Asked by At

class NetworkConnectivityObserver( context: Context, ): ConnectivityObserver {

private val connectivityManager =
    context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
private val networkRequest = NetworkRequest.Builder()
    .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
    .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
    .build()

override fun observe(): Flow<ConnectivityObserver.Status> {
    return callbackFlow {
        val callback = object : ConnectivityManager.NetworkCallback() {
            override fun onAvailable(network: Network) {
                super.onAvailable(network)
                launch { send(ConnectivityObserver.Status.Available) }
            }

            override fun onLosing(network: Network, maxMsToLive: Int) {
                super.onLosing(network, maxMsToLive)
                launch { send(ConnectivityObserver.Status.Losing) }
            }

            override fun onLost(network: Network) {
                super.onLost(network)
                launch { send(ConnectivityObserver.Status.Lost) }
            }

            override fun onUnavailable() {
                super.onUnavailable()
                launch { send(ConnectivityObserver.Status.Unavailable) }
            }
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            connectivityManager.registerDefaultNetworkCallback(callback)
        }else{
            connectivityManager.registerNetworkCallback(networkRequest, callback)
        }
        awaitClose {
            connectivityManager.unregisterNetworkCallback(callback)
        }
    }.distinctUntilChanged()
}}

I use above code for check internet connection but there is only get response when internet On/Off.

I want solution or code for My device wi-fi is on but no internet connection available.

Give me solution for this if anyone have.

1

There are 1 best solutions below

2
fahim36 On

You can use this Method:

Constants.REACHABILITY_SERVER = "https://www.google.com"

fun hasInternetConnected(): Boolean {
        try {
            val connection = URL(Constants.REACHABILITY_SERVER).openConnection() as HttpURLConnection
            connection.setRequestProperty("User-Agent", "ConnectionTest")
            connection.setRequestProperty("Connection", "close")
            connection.connectTimeout = 1000 // configurable
            connection.connect()
            Logger.d(classTag, "hasInternetConnected: ${(connection.responseCode == 200)}")
            return (connection.responseCode == 200)
        } catch (e: IOException) {
            Logger.e(classTag, "Error checking internet connection", e)
        }
    } 
    Log.d(classTag, "hasInternetConnected: false")
    return false
}

The code checks for internet connectivity by attempting to establish a connection to a specified server (in this case, "https://www.google.com") and then checks the response code. If the response code is 200, it returns true, indicating a successful connection. If the response code is not 200 or an exception is caught, it returns false