I have been working on this for a few days after following this guide online. I am using target API 30 (building on a device using Android 11). Trying to get the Wifi SSID using networkCapabilities
this keeps returning null whereas the depreciated wifiManager.connectionInfo.ssid
returns the correct SSID. Any ideas what I am doing wrong? I know this will need to change for API 31 to include the FLAG_INCLUDE_LOCATION_INFO
but I believe this should be working for API 30. Any ideas?
import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import android.util.Log
import androidx.lifecycle.LiveData
private const val TAG = "NetworkStatus"
enum class NetworkStatus {
UNAVAILABLE,
CONNECTED_TO_XXXX,
CONNECTED_TO_OTHER,
CONNECTION_LOST
}
class NetworkStatusHelper(private val context: Context) : LiveData<Pair<NetworkStatus, String>>() {
private lateinit var connectivityManagerCallback: ConnectivityManager.NetworkCallback
var connectivityManager: ConnectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val wifiManager =
context.getSystemService(Context.WIFI_SERVICE) as WifiManager
private fun getConnectivityManagerCallback() = object : ConnectivityManager.NetworkCallback() {
override fun onUnavailable() {
super.onUnavailable()
announceStatus(Pair(NetworkStatus.UNAVAILABLE, ""))
}
override fun onAvailable(network: Network) {
super.onAvailable(network)
}
override fun onCapabilitiesChanged(
network: Network,
networkCapabilities: NetworkCapabilities
) {
super.onCapabilitiesChanged(network, networkCapabilities)
Log.d(TAG, "onCapabilitiesChanged: transport info = ${connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)?.transportInfo}")
try {
val wifiInfo = networkCapabilities.transportInfo as WifiInfo
Log.d(TAG, "onCapabilitiesChanged: wifi SSID ${wifiInfo}")
if (wifiInfo.ssid.contains("XXXX-")) {
announceStatus(Pair(NetworkStatus.CONNECTED_TO_XXXX, wifiInfo.ssid))
} else {
announceStatus(Pair(NetworkStatus.CONNECTED_TO_OTHER, wifiInfo.ssid))
}
}
catch (e: Exception) {
Log.d(TAG, "onCapabilitiesChanged: exception = $e")
}
try {
Log.d(TAG, "onCapabilitiesChanged: ${wifiManager.connectionInfo.ssid}")
} catch (e: Exception) {
Log.d(TAG, "onAvailable: exception = $e")
}
}
override fun onLost(network: Network) {
super.onLost(network)
announceStatus(Pair(NetworkStatus.CONNECTION_LOST, ""))
}
}
fun announceStatus(networkResposePair: Pair<NetworkStatus, String>) {
postValue(networkResposePair)
}
override fun onActive() {
super.onActive()
connectivityManagerCallback = getConnectivityManagerCallback()
val networkRequest = NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.build()
connectivityManager.registerNetworkCallback(networkRequest, connectivityManagerCallback)
}
override fun onInactive() {
super.onInactive()
connectivityManager.unregisterNetworkCallback(connectivityManagerCallback)
}
}
Permissions in Gradle:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
SDK versions
compileSdkVersion 31
buildToolsVersion "30.0.3"
minSdkVersion 30
targetSdkVersion 31