Android Question Auto-discover other phone via Wi-Fi

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
I was not sure whether to post this in Chit-Chat or here, but it deals with B4A…

I have an application which is really little more than one of Erel's AsyncStreams tutorials, slightly modified. It sends text between two phones both running the same app.

I connect the phones by asking the other person for their Wi-Fi address, then entering it into a dialog box, and pressing a button to connect.

Once connected, we can send text between the two phones. This all works fine.

I would like to be able to Auto-Connect to the other phone.

Is there a simple way to do this? It's not worth doing anything complicated.

I would rather not do a Port Scan looking for other devices on the local network listening at a certain port. I do not want to set up a server where devices could check for other available devices.

Is there a way to send a message over the local network saying "I'm Available Here at W.X.Y.Z" – maybe as a UDP broadcast or something?

Any pointers appreciated.

Added Later:

I found an example of sending UDP packets:
https://www.b4x.com/android/forum/threads/network-library-v1-10-udp-supported.9099/page-2#post-76782

I made some minor modifications to this example to make the "Send UDP To Address" to be the broadcast address of my Wi-Fi LAN:
B4X:
  Public Sub SendUDP(toIP As String)
  Dim sa() As String = Regex.Split("\.", toIP)

  If (sa.Length <> 4) Then Return

  Dim Packet As UDPPacket
  Dim data() As Byte
  Dim bcIP As String = Utl.Sprintf("%s.%s.%s.%s", Array As String(sa(0), sa(1), sa(2), "255"))

  data = toIP.GetBytes("ASCII")
  Packet.Initialize(data, bcIP, UDPPort) 'IP and Port can be changed

  UDPSoc.Send(Packet)
End Sub

This works very well on my LAN. The other device running my text transfer app gets the IP of the first device running the app.

Now I'm wondering about in an office environment. My LAN uses the local addresses in the range 192.168.0.X, but I've noticed in an office local network IPs in the range 192.168.X.Y.

Right now for my broadcast address I just replace the last number of the IP with "255". This seems to broadcast to every IP on my LAN.

Would it be correct in the office environment to replace the last two numbers of the IP to get the broadcast address – ie. 192.168.255.255. And will this broadcast address also work on my LAN.

I could just "try it". Does anyone know if I will be creating havoc on the networks?

Thanks,
Barry.
 
Last edited:
Top