I am geting wifiInfo
like that:
val wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
val wifiInfo = wifiManager.connectionInfo
Log.d("###", wifiInfo.ssid.toString())
But when app is in background wifiInfo
always return <unknown ssid>
.
How can i get it?
EDIT:
In MainActivity
I starting coroutine that prints connected wifi SSID every second:
job = MainScope().launch {
while(true) {
val wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
val wifiInfo = wifiManager.connectionInfo
Log.d("###", "SSID: ${wifiInfo.ssid.toString()}")
delay(1000)
}
}
Can see, that SSID stops printing properly after any seconds in background.
@AlexVs86 I tried your provided code.
Solution:
After API 29 ACCESS_COARSE_LOCATION
or ACCESS_FINE_LOCATION
permissions is not enough in manifest, you have to add ACCESS_BACKGROUND_LOCATION
for background location access.
Here, I tried permission:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
OutPut: After granting permission to Allow all the time it provides the information in background as well.
D/###: SSID: <unknown ssid>
D/###: SSID: <unknown ssid>
D/###: SSID: <unknown ssid>
D/###: SSID: <unknown ssid>
D/###: SSID: <unknown ssid>
D/###: SSID: <unknown ssid>
D/###: SSID: <unknown ssid>
D/###: SSID: "Home1_EXT"
D/###: SSID: "Home1_EXT"
D/###: SSID: "Home1_EXT"
D/###: SSID: "Home1_EXT"
D/###: SSID: "Home1_EXT"
D/###: SSID: "Home1_EXT"
D/###: SSID: "Home1_EXT"
D/###: SSID: "Home1_EXT"
D/###: SSID: "Home1_EXT"