20

ConnectivityManager.TYPE_WIFI is deprecated in Android P API 28. Also, NetworkInfo#getType and ConnectivityManager.TYPE_MOBILE's are also deprecated.

So, what are the alternatives for them? I understood that the we've to use the method from NetworkCapabilities class. However I'm not able to merge all the things in one place like how to do getType() in NetworkCapabilities class and how to add the WIFI and cellular data checks on it?

Please assist.

Amrut
  • 2,655
  • 3
  • 24
  • 44

4 Answers4

14

You can use below snippet to check if you have Wifi connection or Cellular:

ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
    Network network = connectivityManager.getActiveNetwork();
    NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
    return capabilities != null && (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR));
}
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
  • 1
    any alternative? – Nitish Sep 23 '19 at 07:54
  • I am already tried the above code then i made up optimization my code the result is crash. So i made up a question https://stackoverflow.com/q/59745326/1384360, i need your help, thank you – Subhanshuja Jan 15 '20 at 04:55
9

Use below method.. 19/06/2019

public boolean isconnectedToWifi(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager == null) {
        return false;
    }

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        Network network = connectivityManager.getActiveNetwork();
        NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
        if (capabilities == null) {
            return false;
        }
        return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI);
    } else {
        NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (networkInfo == null) {
            return false;
        }
        return networkInfo.isConnected();
    }
}
Syed Zeeshan
  • 490
  • 1
  • 7
  • 13
7

ConnectivityManager.TYPE_WIFI is Deprecated. You should use NetworkCapabilities.

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. Similarly if an application needs an unmetered network for a bulk transfer it can specify that rather than assuming all cellular based connections are metered and all Wi-Fi based connections are not.

Applications should instead use NetworkCapabilities.hasTransport(int) or requestNetwork(NetworkRequest, NetworkCallback) to request an appropriate network. for supported transports.

You can try this way

NetworkAgentInfo networkAgent;
int type = ConnectivityManager.TYPE_NONE;
if (networkAgent.networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
     type = ConnectivityManager.TYPE_MOBILE;
} else if (networkAgent.networkCapabilities.hasTransport(
     NetworkCapabilities.TRANSPORT_WIFI)) {
     type = ConnectivityManager.TYPE_WIFI;
}
ataravati
  • 8,891
  • 9
  • 57
  • 89
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

This is how I managed to check if WIFI is available or not without using deprecated libraries and adding support for marshamallow devices-

@ExperimentalCoroutinesApi
    suspend fun isWifiAvailable(): Boolean {
        val connectivityManager =
            ContextCompat.getSystemService(context, ConnectivityManager::class.java) ?: return false
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val networkCapabilities = connectivityManager.activeNetwork ?: return false
            val activeNetwork =
                connectivityManager.getNetworkCapabilities(networkCapabilities) ?: return false
            return activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
        } else {
            return connectivityManager.isNetworkAvailable(NetworkCapabilities.TRANSPORT_WIFI)
//            val activeNetwork = connectivityManager.`activeNetworkInfo`
//            return activeNetwork?.type == ConnectivityManager.TYPE_WIFI
        }
    }

    @ExperimentalCoroutinesApi
    private suspend fun ConnectivityManager.isNetworkAvailable(vararg transportType: Int): Boolean {
        return suspendCancellableCoroutine { continuation ->
            val builder = NetworkRequest.Builder()
            transportType.forEach {
                builder.addCapability(it)
            }
            val networkRequest = builder.build()
            val networkCallback = object : ConnectivityManager.NetworkCallback() {
                override fun onAvailable(network: Network) {
                    super.onAvailable(network)
                    continuation.resume(true, null)
                    unregisterNetworkCallback(this)
                }

                override fun onLost(network: Network) {
                    super.onLost(network)
                    continuation.resume(false, null)
                    unregisterNetworkCallback(this)
                }

                override fun onUnavailable() {
                    super.onUnavailable()
                    continuation.resume(false, null)
                    unregisterNetworkCallback(this)
                }
            }
            registerNetworkCallback(networkRequest, networkCallback)
        }
    }
Priya Sindkar
  • 368
  • 3
  • 11