Android Question is there anyway to set a static IP for the cell Phone?

ALEX BISSARO PITTA

Member
Licensed User
I'm trying to send and receive packets from arduino and B4A and vice versa.

Is there anyway to set a static IP for the cell Phone?
I have three acess points at my home and all the time it connects to a different AP it changes the IP and it is not possible to send or receive packets.
 

moster67

Expert
Licensed User
Longtime User
Most routers/access points have a web-interface and there should be an option where a fixed/static ip-address can be assigned based on the device name of your device or the MAC-number. You might need to do this procedure with all access-points.
 
Last edited:
Upvote 0

nwhitfield

Active Member
Licensed User
Longtime User
Are you really sure you need static IP addresses? If you do, then setting up the DHCP in your router to assign one is usually straightforward and you can get the WiFi MAC address of your phone easily (on mine it's via Settings, About Phone, Status). However, if you do this, you're setting things up in your apps based on your current network.

So, say you have a router that, like my Draytek, is dishing out local addresses in the 192.168 range, and you set up one of the apps with the hardcoded address of your phone. Then you upgrade to a different router - my Airport Extreme dishes out 10.0. addresses. So you either have to add a UI in your app to allow changing that address, or perhaps a config file, or something. And if you wanted to use the app somewhere else, on someone else's network, you'd also run into problems.

Except in a few cases, the problem probably isn't that you don't have a fixed IP address. It's simply that you don't know what the IP address you need to connect to actually is. What about using UDP discovery instead? In one of my apps, it can work as a server or client. In client mode, there's a text field called TCP Hostname; you can enter something in it, but if you tap the label, it starts listening for UDP packets on a port I picked for my app. 'discovery' is a UDPSocket, dport is an Int with the port name

B4X:
Sub hostIPlabel_Click
   ToastMessageShow("Scanning for Estim servers",False)
   discovery.Initialize("discovery",dport,8192)
End Sub

Sub discovery_PacketArrived (Packet As UDPPacket)
   Try
     Dim bc As ByteConverter
     Dim data(Packet.Length) As Byte
     bc.ArrayCopy(Packet.Data,Packet.Offset,data,0,Packet.Length)
     Dim ds As String = bc.StringFromBytes(data,"UTF8")
     If ds <> tcpHostname.Text Then
       tcpHostname.Text = ds
       discovery.Close
     End If
   Catch
     Log(LastException)
   End Try
End Sub

In the server code (this is B4A), you need a timer, and a UDP port, and of course your server TCP port

B4X:
advertisingUDP.initialize("advertise",advertPort,8192)
advertisingTimer.initialise("at",5000)
advertisingTimer.enabled = true

Then add a sub (this runs every five seconds for me)

B4X:
sub at_Tick

dim lanaddr as string = GetBroadcastAddress
if lanaddr <> "" then 
  dim up as UDPPacket
  up.initialise(serverTCP.GetMyWiFiIP.GetBytes("UTF8"),lanaddr, advertPort)
  advertisingUDP.send(up)
end if
end sub

You can refine this further - for example, only advertise for a certain time after startup, or until a connection is made.

On the face of if, this might seem more complex - but it does make your system independent of the network hardware and settings. Essentially, all that's happening is a regular broadcast on a port - pick a high number; for my app, I spelled out the first five letters on a numeric keypad and used that; check it's not already allocated - of a packet with the IP address of the server.

When the listening code hears the broadcast, it grabs the IP address, and uses that to set up the real connection.
 
Upvote 0

wes58

Active Member
Licensed User
Longtime User
So, say you have a router that, like my Draytek, is dishing out local addresses in the 192.168 range, and you set up one of the apps with the hardcoded address of your phone. Then you upgrade to a different router - my Airport Extreme dishes out 10.0. addresses. So you either have to add a UI in your app to allow changing that address, or perhaps a config file, or something. And if you wanted to use the app somewhere else, on someone else's network, you'd also run into problems.
Why would you have to hardcode an IP address in the app? You can assign static IP address on the phone when you configure WiFi connection on the phone.
 
Upvote 0

nwhitfield

Active Member
Licensed User
Longtime User
Because, from the description, it seems as if one of the reasons why the a fixed IP address is required on the phone is so that the app at the other end (which I gather is using B4R) knows what device to connect to. It was the app there that I was referring to, not the phone, needing some way to know the address of the phone that it's trying to connect with.
 
Upvote 0

rtek1000

Active Member
Licensed User
Longtime User
Upvote 0
Top