Android Question Ethernet IP address without relying on the phone state change event

sorex

Expert
Licensed User
Longtime User

zed

Well-Known Member
Licensed User
IP::
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    
    Private myIP As ServerSocket '--> Network library
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    
    
    myIP.Initialize(0, "")
    Log(myIP.GetMyIP) ' my ip local or not
    '
    If myIP.GetMyIP = "127.0.0.1" Then  '
        log("You are not connected")
    End If

End Sub
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
unfortunately the socket method acts weird.

when connected to wifi the .GetMyIP and .GetMyWifiIP return the same IP address

when you connect the lan adapter it switches to lan mode but it doesn't update the .GetMyIP value and both are still the same.

if you close the app and open it then you get the real LAN IP with .GetMyIP and .GetMyWifiIP gives 127.0.0.1

if you don't connect anything the lan ip is some bogus 10.9.184.68 address and wifi is 127.0.0.1

this kinda works but I'll need to modify it so that it also works right on known ethernet/wifi change events or use another sub for those.

B4X:
Sub getIP
    Dim serverSocket As ServerSocket
    Dim ip As String
    serverSocket.Initialize(8888, "")
    serverSocket.Close

    CallSub2(Main,"writeLog","NETW> IP:" & serverSocket.GetMyIP)
    CallSub2(Main,"writeLog","NETW> WifiIP: " & serverSocket.GetMyWifiIP)

    If serverSocket.GetMyIP.StartsWith("192.168.") Then
            lanConnected=True
            lanIP=serverSocket.GetMyIP
    Else
            lanConnected=False
            lanIP="0.0.0.0"
    End If

    If serverSocket.GetMyWifiIP.StartsWith("192.168.") Then
            wifiConnected=True
            wifiIP=serverSocket.GetMyWifiIP
    Else
            wifiConnected=False
            wifiIP="0.0.0.0"
    End If

    CallSub2(Main,"writeLog",$"NETW> LAN: ${lanIP} WIFI: ${wifiIP}"$)
End Sub
 
Upvote 0
Top