Android Question UDP permissions?

Acuario

Active Member
Licensed User
Longtime User
Hi, I'm trying to use UDP in my app.

I can't send or receive UDP packets, nothing, confirmed by monitoring with WireShark that nothing is going out onto my network.

I'm trying with a very basic sample code:
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

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", 4444, 8000)
        IP = Server.GetMyWifiIP
    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, "")
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, 4444)
    UDPSocket1.Send(Packet)
    ToastMessageShow("Message send = " &EditData.Text, False)
End Sub

I added the
AddPermission(android.permission.CHANGE_WIFI_MULTICAST_STATE)
to the manifest file (as I found this may help) but it didn't.

I have android:targetSdkVersion="29" in the manifest.

Phone is a Xiaomi if this makes any difference.

Any idea why UDP isn't working?

Thanks.
 

drgottjr

Expert
Licensed User
Longtime User
it's all wrong, sorry. from b4a viewpoint socket <> udpsocket. b4a socket is tcp, not udp.
you want basic? attached find basic.
you didn't say where your "server" is, but to keep things simple, i made "server" and "client" on the same device. udpsocket handles sending and receiving. it is both server and client. (that's the way erel cooked it up.)
with small modifications, you can put 1 copy on 1 device and another copy on another device and have a little udp chat. they just have to know the other's ip address on the network. you should be able to figure it out. got it?
i've tested in on 1 device, on 2 devices and with a real updsocket server on the internet. works fine. don't mess it up
 

Attachments

  • acuario.zip
    8.4 KB · Views: 216
Upvote 0

Acuario

Active Member
Licensed User
Longtime User
Ok, I found the solution - I was using address 0.0.0.0 as a destination as the host address on my esp project (which works fine esp to esp) but it seems Android isn't so happy using this address. Also to note, ESP8266 is fine with 0.0.0.0 but ESP32 isn't and it seems it has to be a broadcast IP (such as below)

Using Erels code for GetBroadcastAddress and changing to that IP (in my case 192.168.0.255) and data now flows.
As a further test, setting the IP address to 255.255.255.255 also works.

Thanks for the help.
 
Last edited:
Upvote 0

teddybear

Well-Known Member
Licensed User
A specific ip need not to add multicast permission
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…