3

Manifest (permissions declared and granted)

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

Main class

class MainActivity {
    val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val networkInfo = connectivityManager.activeNetwork
    val capabilities = connectivityManager.getNetworkCapabilities(networkInfo)
    if (capabilities?.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) == true) {
        val wifiInfo = capabilities.transportInfo as WifiInfo
        Log.d("DEBUG", "ssid: ${wifiInfo.ssid}")
    }

    val wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
    val connectionInfo = wifiManager.connectionInfo
    Log.d("DEBUG", "ssid (deprecated) ${connectionInfo.ssid}")
}

output

ssid: <unknown ssid>
ssid (deprecated): "bingo"

How to retrieve the SSID while targeting the latest API and use the latest, non deprecated functions?

mbmc
  • 5,024
  • 5
  • 25
  • 53
  • Have you seen: [On Oreo (8.1.0) not getting the correct Wifi SSID. It's showing though it is connected to a wifi with SSID](https://stackoverflow.com/a/54079640/295004) – Morrison Chang Nov 17 '21 at 06:05
  • 1
    @MorrisonChang did some tests, but this yields to the same results... i've also updated the post because the API 26 vs 31 bit was not correct – mbmc Nov 17 '21 at 16:03
  • @mbmc Have you got solution for this? For Android 12 API level 31, I am getting with connectionInfo.ssid – BSB Jan 10 '23 at 06:15

1 Answers1

0

You need to enable the device's location.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 21 '22 at 10:49