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
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
advertisingUDP.initialize("advertise",advertPort,8192)
advertisingTimer.initialise("at",5000)
advertisingTimer.enabled = true
Then add a sub (this runs every five seconds for me)
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.