Android Question Android 8.1 problem to get connected SSID

welu1805

Active Member
Licensed User
Longtime User
Hi all,

with this code I want to get the connected SSID name under Android 8.1. This should be my router because the app is installed over B4A bridge.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim Wifi As JavaObject
    Dim context As JavaObject
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_COARSE_LOCATION)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    
    context.InitializeContext
    Wifi = context.RunMethod("getSystemService", Array("wifi"))

    Dim SSID, state As String
    Dim jo As JavaObject
    
    jo = Wifi.RunMethodJO("getConnectionInfo", Null)
    state = jo.RunMethod("getSupplicantState", Null)
    
  If state = "COMPLETED" Then
        SSID = jo.RunMethod("getSSID", Null)
        Msgbox(state, SSID)
    Else
        Msgbox("Not COMPLETED", "state")

  End If

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)

End Sub

The msgbox shows:

<unknown ssid>
COMPLETED

In the Documentation for Wifimanager is:
An app must hold ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission in order to get valid results.

I granted the permission ACCESS_COARSE_LOCATION.

Has anyone an idea?
Lutz
 

DonManfred

Expert
Licensed User
Longtime User
i don´t see you using the result of the permission-request. Are you sure the permission returns TRUE in your request?

getSSID

Missing location
As of now, permission itself is not enough, location services have to be enabled to use WifiManager.getScanResults and WifiManager.getConnectionInfo.
Location is enabled?
 
Last edited:
Upvote 0

welu1805

Active Member
Licensed User
Longtime User
At first I resetted the permission for "Location" in my tablet. Then I run this app. It appears a dialog where I can set the permission for location. At the second start of this app the dialog desn't appera that means the permission was setted. In the tablets setting I see that too.
 
Upvote 0

welu1805

Active Member
Licensed User
Longtime User
I found a solution with help from Google:

B4X:
Sub GetWIFIName As String
    Dim R As Reflector
    Dim SSID As String
   
    ' Add this permission to the manifest: AddPermission (android.permission.ACCESS_NETWORK_STATE)
   
    R.Target = R.GetContext
    Try
        R.Target = R.RunMethod2("getSystemService", "connectivity", "java.lang.String")
        R.Target = R.RunMethod("getActiveNetworkInfo")
        R.Target = R.RunMethod("getExtraInfo")
       
        SSID = R.Target
        SSID = SSID.SubString2(1, SSID.Length - 1)
       
        Return SSID
    Catch
        Return "none"
    End Try
End Sub

It shows me the correct name of my router in Android 5, 6, 7 and 8.1

I have no device with Android 9. If someone has A 9, please test the code and let us know if it works.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…