Android Question Network lib : UDP broadcast and wifi direct

freedom2000

Well-Known Member
Licensed User
Longtime User
Hi,

I have recently writen a very simple VB.Net program to link an android to a PC via UDP.
This prog works fine on a LAN wifi network.

But I do have issues when connecting my PC to my Nexus5 acting as a hotspot.

The issue is that I cannot broadcast UDP packet to the local network (the PC never sees anything neither a broadcast packet (255.255.255.255) nor a regular packet sent by the android to the client adress)

I can broadcast to the Android from the PC client, both with a broadcast adress or directly to the Host IP adress

It seems to be due to the fact that the IPadress on the android is the 'cellular' one and not the local IP on the LAN between the PC and the Android.

The code on the server side (android) is simple :

B4X:
Sub Process_Globals
    Dim UDPSocket1 As UDPSocket
    Dim Server As ServerSocket
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim ButtonSend As Button
    Dim EditData As EditText
    Dim EditDest As EditText
    Dim LabelIP As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
    Dim IP As String
    If FirstTime Then
        UDPSocket1.Initialize("UDP", 5000, 8000)
        IP = Server.GetMyIP
    End If
    Activity.LoadLayout("udp")
    LabelIP.Text = IP
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub UDP_PacketArrived (Packet As UDPPacket)
    Dim msg As String
 
    msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
    Msgbox("Message received: " & msg , "")
    Try
        Msgbox(" from host " & Packet.HostAddress, "")
    Catch
        Msgbox(" error from host ", "")
    End Try
End Sub
Sub ButtonSend_Click
    Dim Packet As UDPPacket
    Dim dest As String
    Dim data() As Byte
    data = EditData.text.GetBytes("UTF8")
    dest = EditDest.text
    Packet.Initialize(data, dest, 5000)
    UDPSocket1.Send(Packet)
    ToastMessageShow("Message send = " &EditData.Text, False)
End Sub

After googling a lot I found this link which proposes a way to avoid broadcasting issue. But it doesn't work on my side...

The explaination provided is rather convincing, but not really totally !!!
"A mobile device set up as a hotspot this way acts as a router and creates its own local network. It gives itself a local IP address and gives one to any devices that connect to it using DHCP.

However when using the network_send_broadcast function, it doesn't broadcast over the local network created in this way. I've tried it and it is instead trying to broadcast to all the IP addresses in the range of the address it uses for the mobile data connection (i.e. the external IP rather than the local IP).
"

After googling even more I found this other link explaining how to create a hotspot on the PC if you have an Intel Wifi chip... which I have !
[HOWTO] Wifi Direct - Use your laptop/desktop as a SoftAP for android (Reverse Tethering)

So I of course tested both the PC and the B4A codes on this configuration :
- client is the Android
- host (hotspot) is the PC

In this case the communication "point to point" is OK when providing the Ipadresses.
It doesn't work when just broadcasting UDP packets...
This solution is technically interesting but suppose that you have an INTEL wifi chip... which is of course not the case for everybody ...

Finally my question :)

Do you know a way to network between a PC linked to an android acting as a hotspot (using sockets at UDP or TCP level) but allowing a bidirectional communication ?

And I frankly do not understand why communication is OK from the client to the host but not from the host to the client...
 
Last edited:
Top