Is there a way to get the B4A IP address of a device through code?
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
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
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
<?php
echo $_SERVER['REMOTE_ADDR'];
?>
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
Have you looked at the code I posted on that thread?
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
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?