Get Device IP Address Through Code

citywest

Member
Licensed User
Longtime User
Hi Bill,

I use something like this:

B4X:
Sub CheckConnectionStatus
    Dim InterCon As ServerSocket
    InterCon.Initialize(0, "")
    If InterCon.GetMyIP = "127.0.0.1" Then 
       Return ""
    Else
      Return InterCon.GetMyIP
    End If
End Sub

Cheers,

Mark S.
 
Last edited:
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
How do I get my internet IP (inside local net)
if using 3G, then the above code is ok
but if I am using wireless then I am getting my local net ip
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
Thanks, I wrote this,
I think it is ugly code, but do the job

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    http.Initialize("http",Me)
    http.Download("http://www.findmobileip.com/")
   
End Sub

B4X:
Sub JobDone (Job As HttpJob)
    Dim Str As String
    Dim idx As Int
       
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        Select Job.JobName
            Case "http"
                Str=Job.GetString
               
        End Select
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
   
    idx=Str.IndexOf("blue")
    Str=Str.SubString2(idx+7,idx+25)
    idx=Str.IndexOf("<")
    Str=Str.SubString2(0,idx)
    Log(Str)
   
End Sub
 
Upvote 0

Wobbi

New Member
Licensed User
Longtime User
You can also get the public IP via visiting the free Stun server(ref. RFC3489), just as:
B4X:
Dim Packet As UDPPacket
  Dim data(28) As Byte

    data(0) = 0x00
    data(1) = 0x01 ' message type=binding request
    data(2) = 0x00
    data(3) = 0x08 ' message length
    Dim k As Int
    For k = 0 To 15
        data(4+k) = Rnd(0x00,0xff) ' stun id = 16 random byte
    Next
   
    data(20) = 0x00
    data(21) = 0x03 'CHANGE_REQUEST type
    data(22) = 0x00
    data(23) = 0x04 'length
    data(24) = 0x00
    data(25) = 0x00
    data(26) = 0x00
    data(27) = 0x00 'data
   
    'using free stun server, example:
    'stun1.voiceeclipse.net
    'stun.sipgate.net
  'stun.voxalot.com
    Packet.Initialize(data, "stun.sipgate.net",3478)    'IP and Port can be changed
  UDP_Socket.send(Packet)

...

Sub UDP_PacketArrived (Packet As UDPPacket)
    Dim  ip As String
        Dim k As Int
      
        If (Packet.Data(0) = 0x01) AND (Packet.Data(1) = 0x01) Then
            k = Packet.Data(21)
            k = Bit.OR(Bit.ShiftLeft(Packet.Data(20),8),k)
            If k = 0x01 Then
            'MAPPED ADDRESS
            ip = Bit.AND(0xff,Packet.Data(28))&"." & Bit.AND(0xff,Packet.Data(29)) _
            & "." &Bit.AND(0xff,Packet.Data(30))&"."&Bit.AND(0xff,Packet.Data(31))
            Label2.Text = "Public IP: " &ip
            End If
        End If

       
End Sub

I have a test App for implement, please check the attachment(B4A 3.80)
 

Attachments

  • check_IP.zip
    7.9 KB · Views: 429
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
Have you looked at the code I posted on that thread?

I have tested the sample(B4A 3.8) and got errors like these :

java.lang.NullPointerException
at gsf.GetIP.main._hc_responseerror(main.java:316)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
at anywheresoftware.b4a.BA$3.run(BA.java:319)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException
 
Upvote 0

ac9ts

Active Member
Licensed User
Longtime User
B4X:
Sub CheckNetConnections

    ' This sub will determine the WiFi and network connection.
   
    Dim Server As ServerSocket 
    Dim WiFi_IP As String
    Dim Network_IP As String
   
    Server.Initialize(21341,"Net")
    Network_IP = Server.GetMyIP
    WiFi_IP = Server.GetMyWifiIP
    Server.Close

    If WiFi_Only AND WiFi_IP = "127.0.0.1" Then 
        ToastMessageShow("No WiFi connection", True)
    End If
   
    If WiFi_IP = "127.0.0.1" AND Network_IP = "127.0.0.1" AND Not(WiFi_Only) Then
        ToastMessageShow("No Internet connection", True)
    End If
   
End Sub
 
Upvote 0

cd37ycs

Member
Licensed User
Longtime User
B4X:
Sub CheckNetConnections

    ' This sub will determine the WiFi and network connection.
  
    Dim Server As ServerSocket
    Dim WiFi_IP As String
    Dim Network_IP As String
  
    Server.Initialize(21341,"Net")
    Network_IP = Server.GetMyIP
    WiFi_IP = Server.GetMyWifiIP
    Server.Close

    If WiFi_Only AND WiFi_IP = "127.0.0.1" Then
        ToastMessageShow("No WiFi connection", True)
    End If
  
    If WiFi_IP = "127.0.0.1" AND Network_IP = "127.0.0.1" AND Not(WiFi_Only) Then
        ToastMessageShow("No Internet connection", True)
    End If
  
End Sub

what's WiFi_Only?
 
Upvote 0

ac9ts

Active Member
Licensed User
Longtime User
what's WiFi_Only?

It is a flag I use in the app that the code was pulled from. If the user only wants to download through WiFi (WiFi_Only = True) and there is no WiFi connection (
WiFi_IP = "127.0.0.1"), then the message is displayed. The full code did some other stuff when the network conditions were not met for downloading content but I stripped it out for simplicity in the response above.
 
Upvote 0
Top