Android 11 wifi SSID is unknown in background even when lcoation permissions is granted

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 notices 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?

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum