Android Question I want to extract the IPv4 address of the router connected to my mobile

AlfaizDev

Well-Known Member
Licensed User
I want to extract the IPv4 address of the router connected to my mobile
I used the following codes and it only appears
Default Gateway
And also the Wi-Fi for my mobile
Just
B4X:
Sub GetGatewayIp As String
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim WifiManager As JavaObject = ctxt.RunMethod("getSystemService", Array(ctxt.GetField("WIFI_SERVICE")))
    Dim dhcp As JavaObject = WifiManager.RunMethod("getDhcpInfo", Null)
    Dim formatter As JavaObject
    Return formatter.InitializeStatic("android.text.format.Formatter").RunMethod("formatIpAddress", Array(dhcp.GetField("gateway")))
End Sub
Tried the values here and it is useless
https://developer.android.com/reference/android/net/DhcpInfo

B4X:
Sub GetPropInfo As String
    Dim i As Int
    Dim P As Phone
    Dim sb As StringBuilder
    Dim PropData() As String
    Dim sWifi, sModel, sSdk, sVersion, sIP, sMask, sGate, sD1, sD2 As String

    sb.Initialize
    P.Shell("getprop", Null, sb, Null)
    PropData = Regex.Split(CRLF,sb.ToString)

    For i = 0 To PropData.Length - 1

        ' What is the Wifi Interface called?
        If PropData(i).Contains("[wifi.interface]") Then
            sWifi = PropData(i)
            sWifi = sWifi.Replace("[","")
            sWifi = sWifi.Replace("]","")
            sWifi = sWifi.SubString(sWifi.IndexOf(" ")).Trim
            Exit
        End If
 
    Next

    If sWifi.Length > 0 Then

        ' Get the model, android version, sdk version, ip, subnet mask, gateway, dns1 and dns2
        For i = 0 To PropData.Length - 1

            If PropData(i).Contains("[ro.product.model]") Then
                sModel = PropData(i)
                sModel = sModel.Replace("[","")
                sModel = sModel.Replace("]","")
                sModel = sModel.SubString(sModel.IndexOf(" ")).Trim
            End If
     
            If PropData(i).Contains("[ro.build.version.sdk]") Then
                sSdk = PropData(i)
                sSdk = sSdk.Replace("[","")
                sSdk = sSdk.Replace("]","")
                sSdk = sSdk.SubString(sSdk.IndexOf(" ")).Trim
            End If
     
            If PropData(i).Contains("[ro.build.version.release]") Then
                sVersion = PropData(i)
                sVersion = sVersion.Replace("[","")
                sVersion = sVersion.Replace("]","")
                sVersion = sVersion.SubString(sVersion.IndexOf(" ")).Trim
            End If
     
            ' *******************************************************************
     
            If PropData(i).Contains("[dhcp." & sWifi & ".ipaddress]") Then
                sIP = PropData(i)
                sIP = sIP.Replace("[","")
                sIP = sIP.Replace("]","")
                sIP = sIP.SubString(sIP.IndexOf(" ")).Trim
            End If
     
            If PropData(i).Contains("[dhcp." & sWifi & ".mask]") Then
                sMask = PropData(i)
                sMask = sMask.Replace("[","")
                sMask = sMask.Replace("]","")
                sMask = sMask.SubString(sMask.IndexOf(" ")).Trim
            End If
     
            If PropData(i).Contains("[dhcp." & sWifi & ".gateway]") Then
                sGate = PropData(i)
                sGate = sGate.Replace("[","")
                sGate = sGate.Replace("]","")
                sGate = sGate.SubString(sGate.IndexOf(" ")).Trim
            End If
     
            If PropData(i).Contains("[dhcp." & sWifi & ".dns1]") Then
                sD1 = PropData(i)
                sD1 = sD1.Replace("[","")
                sD1 = sD1.Replace("]","")
                sD1 = sD1.SubString(sD1.IndexOf(" ")).Trim
            End If
     
            If PropData(i).Contains("[dhcp." & sWifi & ".dns2]") Then
                sD2 = PropData(i)
                sD2 = sD2.Replace("[","")
                sD2 = sD2.Replace("]","")
                sD2 = sD2.SubString(sD2.IndexOf(" ")).Trim
            End If

        Next
 
        Return  "Model: " & sModel & CRLF & _
            "Android v/ " & sVersion & CRLF & _
            "SDK v/ " & sSdk & CRLF & CRLF & _
            "Wifi Interface: " & sWifi & CRLF & _
            "IP Address: " & sIP & CRLF & _
            "Subnet Mask: " & sMask & CRLF & _
            "Default Gateway: " & sGate & CRLF & _
            "DNS #1: " & sD1 & CRLF & _
            "DNS #2: " & sD2 & CRLF

    Else
        Return "Wifi Interface not found."
    End If
End Sub
77.jpg
 

drgottjr

Expert
Licensed User
Longtime User
the code in your GetGatewayIp sub is ok and gives you the correct answer. you only asked for the gateway, so that's what you got. the documentation on the google ref page you mentioned is not useless. perhaps, to quote obi wan, those are not the values you're looking for.

if you're looking to see what's facing the outside from the inside (your mobile), you can't. only the router can. or somebody already on the outside, but all you're going to get from the outside is your external ip address. there are plenty of servers that will tell you your external ip address from your mobile.

technically, it would be possible to log in to your router (eg, with ssh) and find some information, but figuring out what your router wants to cough that information up isn't going to be easy. assuming your router has a web frontend, you might be able to scrape the page via okhttputils. if you can see the numbers on the page, you should be able to capture them for your app.

EDIT:

just a brief follow-up: if you had an ethernet adapter and otg adapter for your device, you could
plug it directly into a cable/satellite modem. you might get a little closer to learning what you
wanted to learn. once you add a router to the mix, you lose access to the modem, although,
technically, it is possible to (port)forward requests from the router to the modem, if you knew
which port the modem is listening on and its address on your provider's wan.
 
Last edited:
Upvote 0

AlfaizDev

Well-Known Member
Licensed User
thanks for the information
But I couldn't execute
Could you have approached me that
Abbreviated code
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
sorry, i don't know what you mean by "abbreviated code" ??

your sub GetGatewayIp runs fine. i tested it. refer to google's documentation, and you can add the other available fields (ns1, ns2, gateway, etc). you seem to understand what the class can do, so i don't understand what the problem is.

as for your sub GetPropInfo, it also runs fine. but those fields you're looking for aren't there. after your call to "sh" returns and you have sb.tostring, just insert a log call ( log(sb.tostring) ). you'll see what i'm talking about.

it seems to me that you want to run GetPropInfo first and then run GetGatewayIp (with the additional fields filled in, of course):
B4X:
    Log(formatter.InitializeStatic("android.text.format.Formatter").RunMethod("formatIpAddress", Array(dhcp.GetField("gateway"))))
    Log(formatter.InitializeStatic("android.text.format.Formatter").RunMethod("formatIpAddress", Array(dhcp.GetField("ipAddress"))))
    Log(formatter.InitializeStatic("android.text.format.Formatter").RunMethod("formatIpAddress", Array(dhcp.GetField("netmask"))))
    Log(formatter.InitializeStatic("android.text.format.Formatter").RunMethod("formatIpAddress", Array(dhcp.GetField("serverAddress"))))
    ' etc ...

is this what you mean?
 

Attachments

  • capture1.png
    capture1.png
    45.3 KB · Views: 240
Upvote 0

AlfaizDev

Well-Known Member
Licensed User
Yes, I tried all these codes beforehand, as in the Google document
But it does not appear to me except the one in the picture
The IPv4 address of the connected router is not shown
789.jpg
888.jpg
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
the address of the connected router is the serverAddress - the dhcp server. on a small home network, the gateway and the dhcp server are likely to be the same address. i don't understand what you're saying in 888.jpg. i don't know where it came from, i don't know what code you're using.

when you use the dhcp class, you're dealing with a dhcp server. it doesn't have to be the router, but that's usually the case (on a home network). i don't know what kind of environment you're operating in. dhcpinfo just tells you about dhcp. that's what you asked for. if you want to know the id addresses of every node on your network, you need arp -a. one of those addresses will be your router. that same node could also be your gateway and your dhcp server. they are all different things, but on a small network, they usually resolve to the same node.

if you're saying 192.168.1.2 is a node on your network, ok. but it's not your dhcp server. you'll have to ask 192.168.1.2 to identity itself. or you need access to the configuration file that sets the default gateway. i apologize for mistakenly assuming you were on a simple home network. if one of your boxes is actually running the network, and you have different devices serving as a gateway and a router and a firewall and a dhcp server, etc., you're not going to get very far talking to the dhcp server. its job is to assign temporary (dynamic) ip addresses to devices that ask it for one on your network.

in addition to arp, there is also ifcfg to tell you more about your network. different os's deal with these utilities in different ways. and android may not make some of them available with "sh". sorry i can't help.
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
@alfaiz678 I know that @drgottjr has already answered your question, but here is an example from my code base.

B4X:
** Activity (main) Resume **
** Service (starter) Destroy (ignored)**
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
My IP address = 192.168.0.115
Broadcast address = 192.168.0.255
Gateway Address 192.168.0.100

Probably not what you are looking for and probably will not help you at all, but anyway...
 

Attachments

  • IP.zip
    8.2 KB · Views: 285
Upvote 0
Top