Android Question Get SSID for Android 10

jo1234

Active Member
Licensed User
Longtime User
Hi all,

I have searched the forum and there are several threads on how to get the SSID of the connected Wifi, however, I could not find one that works on Android 10.

Currently, I'm using the following code, which works for Android 8.1 and 9:
B4X:
    Dim R As Reflector
    Dim SSID As String

    R.Target = R.GetContext
    Try
        R.Target = R.RunMethod2("getSystemService", "connectivity", "java.lang.String")
        R.Target = R.RunMethod("getActiveNetworkInfo")
        R.Target = R.RunMethod("getExtraInfo")
    End Try

I have added to the manifest: AddPermission (android.permission.ACCESS_NETWORK_STATE) and request runtime permission ACCESS_COARSE_LOCATION.
The code works for most devices with Android 8 and 9, only few devices with Android 5, 7, 8.1, 9 give an error in line 8 (R.RunMethod("getExtraInfo")):
Non-fatal Exception: java.lang.NullPointerException
Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference


However, this code does not work on Android 10 (testing on an Huawei P30).

Does anyone have a reliable way of getting the SSID in Android 10?

Thanks a lot,
Johannes
 
Last edited:

drgottjr

Expert
Licensed User
Longtime User

Attachments

  • 29.jpg
    29.jpg
    54.8 KB · Views: 233
Upvote 0

jo1234

Active Member
Licensed User
Longtime User
Hi,

this seems to work on Android 10.
B4X:
    R.Target = R.GetContext
    Try
        R.Target = R.RunMethod2("getSystemService", "wifi", "java.lang.String")
        R.Target = R.RunMethod("getConnectionInfo")
        R.Target = R.RunMethod("getSSID")
      
        If R.Target <> Null Then
            SSID = R.Target
            SSID = SSID.SubString2(1, SSID.Length - 1)
            If SSID.Contains("unknown") Then SSID = "" 'From android 8.0 onwards we get SSID only if GPS is turned on, otherwise "<unknown ssid>".
            Return SSID
        Else
            Return ""
        End If
    Catch
        Return ""
    End Try

It requires:
1.) This permission in the manifest: AddPermission (android.permission.ACCESS_NETWORK_STATE)
2.) Runtime permission ACCESS_COARSE_LOCATION
3.) GPS on.
 
Upvote 0
Top