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:
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.
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.