WifiManager getConnectionInfo() got deprecated in Android API level 31
Asked Answered
K

1

8

We uses Wifimanger getConnectionInfo() method to get SSID and BBSID of connected wifi. But starting with Andorid API level 31 , Android has deprecated the getConnectionInfo() method.

The solution that they have provided to use getTransportInfo() required minimum Android level 29 which we cannot target , given our 20 percent user still on Android API level below 29.

Can someone help with the alternative method to get connected wifi.

https://developer.android.com/reference/android/net/wifi/WifiManager#getConnectionInfo()

Kemper answered 1/3, 2022 at 20:27 Comment(0)
D
4

You can use NetworkCallback() instead. You need to pass FLAG_INCLUDE_LOCATION_INFO to NetworkCallback(), otherwise you will get "unknown SSID" only.

Also, you need the ACCESS_NETWORK_STATE and ACCESS_FINE_LOCATION permissions to make it work.

val request = NetworkRequest.Builder()
    .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
    .build()
val networkCallback = object : ConnectivityManager.NetworkCallback(
    FLAG_INCLUDE_LOCATION_INFO) {
    override fun onCapabilitiesChanged(
        network: Network,
        networkCapabilities: NetworkCapabilities
    ) {
        super.onCapabilitiesChanged(network, networkCapabilities)
        val wifiInfo = networkCapabilities.transportInfo as WifiInfo
        val ssid = wifiInfo.ssid
    }
}
connManager.registerNetworkCallback(request, networkCallback)

Refer to here.

Dip answered 12/8, 2022 at 2:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.