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
Tried the values here and it is useless
https://developer.android.com/reference/android/net/DhcpInfo
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
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