Android Code Snippet Access Wifi Information

This code takes care of requesting the location permission which is needed on Android 8+:

B4X:
Sub GetWifiInfo As ResumableSub
    Dim p As Phone
    Dim WifiManager As JavaObject
    Dim WifiInfo As JavaObject
    WifiManager = WifiManager.InitializeContext.RunMethod("getSystemService", Array("wifi"))
    If p.SdkVersion >= 27 Then
        Dim rp As RuntimePermissions
        rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result = False Then Return WifiInfo
    End If
    WifiInfo = WifiManager.RunMethod("getConnectionInfo", Null)
    Return WifiInfo
End Sub
Manifest editor:
B4X:
AddPermission(android.permission.ACCESS_WIFI_STATE)
AddPermission(android.permission.ACCESS_FINE_LOCATION)
Depends on: Phone, JavaObject and RuntimePermissions

Usage example:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Wait For (GetWifiInfo) Complete (WifiInfo As JavaObject)
    If WifiInfo.IsInitialized Then
        Log(WifiInfo.RunMethod("getSSID", Null))
    End If
End Sub
Other WifiInfo methods: https://developer.android.com/reference/android/net/wifi/WifiInfo

Note that the information will not be available on Android 8+ if location services are disabled.
 
What that i get is :
<unknown ssid>

samsung Galaxy 01 / Android 10
 

peacemaker

Expert
Licensed User
Longtime User
<unknown ssid>

in Android10 on my Samsung - "<unknown ssid>" is returned only when the screen is off, when screen is active - ssid is returned correctly.
 
Last edited:

Kirk Grayson

Member
Licensed User
Yes. My location is turned on. My test devices are a Note 5 running Android 7 and a Note 9 running Android 10.
Turning off my WIFI or leaving my WIFI area on my Note 5 works how the way I want. But on my Note 9, nothing happens.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…