Get current Wifi SSID Android 10

3.3k Views Asked by At

I'm trying to get my current Wifi SSID on Android 10. The code from Android 9 or lower does not work anymore. Is there any sample for Android 10?

Regards Hacki

2

There are 2 best solutions below

1
On

Since API 26 (Android 8 Oreo) you need to obtain the user location permission in order to get the wifi name (SSID), which is why also in Android 9 (API 28), Android 10 (API 29) or Android 11 (API 30) and newer you may get <unknown name> as the SSID returned or 02:00:00:00:00:00.

For that, in AndroidManifest.xml:

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

I will provide some demo code written in Kotlin.

For declaring your permission request success code, we store it in a companion object (Kotlin way for storing constants) inside the class we are testing from (MainActivity in this case) or you may define a class explicitly for constants, which is actually a common practice.

class MainActivity : AppCompatActivity() {

    ...

    companion object {
        const val PERMISSION_CODE_ACCEPTED = 1
        const val PERMISSION_CODE_NOT_AVAILABLE = 0
    }
 
    ...
}

In your testing activity:

when(requestLocationPermission()){
    MainActivity.PERMISSION_CODE_ACCEPTED -> getWifiSSID()
}

For checking an requesting the permission:

fun requestLocationPermission(): Int {
        if (ContextCompat.checkSelfPermission(this,
                        android.Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                            android.Manifest.permission.ACCESS_FINE_LOCATION)) {
            } else {
                // request permission
                ActivityCompat.requestPermissions(this,
                        arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION),
                        MainActivity.PERMISSION_CODE_ACCEPTED)
            }
        } else {
            // already granted
            return MainActivity.PERMISSION_CODE_ACCEPTED
        }

        // not available
        return MainActivity.PERMISSION_CODE_NOT_AVAILABLE
}

For actually getting the SSID (wifi name):

fun getWifiSSID() {
        val mWifiManager: WifiManager = (this.getApplicationContext().getSystemService(Context.WIFI_SERVICE) as WifiManager)!!
        val info: WifiInfo = mWifiManager.getConnectionInfo()
    
        if (info.getSupplicantState() === SupplicantState.COMPLETED) {
            val ssid: String = info.getSSID()
            Log.d("wifi name", ssid)
        } else {
            Log.d("wifi name", "could not obtain the wifi name")
        }
}

Tested on the emulator on API 29 (Android 10).

2020-10-04 15:35:28.625 13013-13013/com.example.myapplication D/wifi name: "AndroidWifi"
0
On

Set his two permissions requests into your project manifest file:

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

Than you have to set Application permission manually by using Settings screen of Android.