Android Question How can I find out the SSID and IP address of the connected router or access point

Lakhtin_V

Active Member
Licensed User
Longtime User
I found a code that allows you to find out the SSID of a connected router or access point
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

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
But I have no options to find out the IP address of the device that distributes Wifi
 

Lakhtin_V

Active Member
Licensed User
Longtime User
I almost solved the problem,
B4X:
Sub GetWifiInfo As ResumableSub
    Dim p As Phone, str As String
    Dim WifiManager As JavaObject
    Dim WifiInfo As JavaObject, WiFiDHCP As JavaObject, WiFiState 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)
    Log("WiFi status " & WifiInfo)
    WiFiState = WifiManager.RunMethod("getWifiState", Null)
    Log("WiFi status " & WiFiState)
[B]    WiFiDHCP = WifiManager.RunMethod("getDhcpInfo", Null)
    Log("WiFi DHCP status " & WiFiDHCP)[/B]
    Return WifiInfo
End Sub

WiFi status (WifiInfo) SSID: Gama, BSSID: 20:98:d8:18:28:fd, MAC: 84:98:66:86:0a:2b, Supplicant state: COMPLETED, RSSI: -39, Link speed: 72, Net ID: 5, Metered hint: false, mFrequency: 2437
WiFi status (Integer) 3
WiFi DHCP status (DhcpInfo) ipaddr 192.168.0.21 gateway 192.168.0.1 netmask 255.255.255.0 dns1 192.168.0.1 dns2 0.0.0.0 DHCP server 192.168.0.1 lease 28800 seconds

I see the IP address in the WiFiDHCP Java object, but I don’t know how to get the IP address of the server from the Java object convert to string
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
several different ways to do this. here's one way. i just inserted a couple statements into your GetWifiInfo sub and added an extra step:

B4X:
Sub GetWifiInfo
    Dim p As Phone, str As String
    Dim WifiManager As JavaObject
    Dim WifiInfo As JavaObject, WiFiDHCP As JavaObject, WiFiState 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
    End If
    WifiInfo = WifiManager.RunMethod("getConnectionInfo", Null)
    Dim rawipaddress As Int = WifiInfo.RunMethod("getIpAddress",Null)
   
    Log("WiFi status " & WifiInfo)
    WiFiState = WifiManager.RunMethod("getWifiState", Null)
    Log("WiFi status " & WiFiState)
    WiFiDHCP = WifiManager.RunMethod("getDhcpInfo", Null)
    Log("WiFi DHCP status " & WiFiDHCP)
    Dim gateway As Int = WiFiDHCP.GetField("gateway")
    Log("Gateway: " & i2ip(gateway))
End Sub

Sub i2ip(address As Int) As String
    Dim jo As JavaObject
    jo.InitializeContext
    Return(jo.RunMethod("i2ip2", Array(address)))
End Sub

#if Java
    public static String i2ip2(int ip) {
        return(java.lang.String.format("%d.%d.%d.%d",(ip & 0xff),(ip >> 8 & 0xff), (ip >> 16 & 0xff),(ip >> 24 & 0xff)));
    }
#end if

the WifiDHCP class documentation has the information you need. just ask it for the gateway's address. it returns an int which you need to convert to an inet address. you need a little help routine for that. there are many. i chose inline. there is a similar call in b4a. you can look for it.
 

Attachments

  • 1.png
    1.png
    68.4 KB · Views: 83
Last edited:
Upvote 0
Top