I tested this code in Android 11
val networkRequest = NetworkRequest.Builder().apply {
addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
}.build()
connectivityManager.registerNetworkCallback(networkRequest, object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
super.onAvailable(network)
val wifiName = getCurrentlyConnectedName() ?: return
val wifiMac = getCurrentlyConnectedMac() ?: return
Timber.d("WifiStateManager onAvailable() called with: network = [$wifiName]")
}
override fun onLost(network: Network) {
super.onLost(network)
Timber.d("WifiStateManager onLost() called with: network = [$network]")
}
})
And noticed a strange behavior. When the app is in the background wifi ssid
is always <unknown>
(please note that Location permission is granted 100%). When the app is in the foreground everything is OK and I get the correct wifi ssid
.
Why this is happening? How to get wifi SSID even when the app is in the background on Android 11?