Android Question Problem to getSSID in Android 8

welu1805

Active Member
Licensed User
Longtime User
Hi all,

I tried to get the SSID of the connected network with this code:

B4X:
...
Context.InitializeContext
Wifi = Context.RunMethod("getSystemService", Array("wifi"))
...

' if available and suplicantState "COMPLETED" return SSID
' if available and suplicantState <> "COMPLETED" return "not completed"
' if wifi is not enabled, Return "unknown ssid"
Public Sub returnActSSID As String
    Dim SSID, state As String
    Dim jo As JavaObject
   
    jo = Wifi.RunMethodJO("getConnectionInfo", Null)
    SSID = jo.RunMethod("getSSID", Null)
    state = jo.RunMethod("getSupplicantState", Null)
   
    If SSID <> "unknown ssid" Then
        If state <> "COMPLETED" Then
            SSID = "not completed"
        End If
    End If
    Return SSID
End Sub

Although I have in the manifest the permissions ACCESS_WIFI_STATE and ACCESS_FINE_LOCATION and
requested runtime permission ACCESS_FINE_LOCATION in the app and set on location of the device manually, the returned SSID is "unknownssid".

However I tried another way:

B4X:
Type nwType (nwID As Int, ssid As String)

Sub GetConnectedNetwork As nwType
    Dim nwList As List
    Dim i As Int
    Dim res As nwType

    nwList = Wifi.RunMethodJO("getConfiguredNetworks", Null)
   
    res.nwID = -1
    res.ssid = "none"
    For i = 0 To nwList.Size - 1
        Dim config As JavaObject
        config = nwList.Get(i)
        If config.GetField("status") = 0 Then     ' status = 0 is the CURRENT connected network!
            res.nwID = config.GetField("networkId")
            res.ssid = config.GetField("SSID")
            Exit
        End If
    Next
   
    Return res
End Sub

res.ssid returns the correct SSID of the connected network.

This is the documentation in Android developer WifiManger:

getConfiguredNetworks
added in API level 1
public List<WifiConfiguration> getConfiguredNetworks ()
Return a list of all the networks configured for the current foreground user. Not all fields of WifiConfiguration are returned. Only the following fields are filled in:

  • networkId
  • SSID
  • BSSID
  • priority
  • allowedProtocols
  • allowedKeyManagement
  • allowedAuthAlgorithms
  • allowedPairwiseCiphers
  • allowedGroupCiphers
Returns
List<WifiConfiguration> a list of network configurations in the form of a list of WifiConfiguration objects.

I use in my code the field "status", which is NOT in the list above. But it seems to be filled, until Android 8 it works. Would it be filled in Android 9 too?

Thanks for answers
Lutz
 
Last edited:

welu1805

Active Member
Licensed User
Longtime User
In my code is before the call of Sub "returnActSSID"

B4X:
Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)

Because it doesn't work for me I tried the second solution.
 
Upvote 0

welu1805

Active Member
Licensed User
Longtime User
Some days ago I posted code to get the SSID with "getExtraInfo" (duplicate):

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

I found an article in the internet, that "getExtraInfo" would not work in Android 9. I can't check this because I have no device with A9.
Therefore I looked for another way to get SSID.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…