Android Question how udp receive multicast packets

monki

Active Member
Licensed User
Longtime User
I have several smart home devices (shelly rgbw2), these send their status via Upn to the multicast address 224.01.1.187 port 5683.
how can i receive the multicast packets on android tablets. is there an example?

Monki
 

monki

Active Member
Licensed User
Longtime User
@ Erel
I have tested AcquireMulticastLock with this code, no Mulkticast packets are received.
I have not see a referenz to the Multicast Group IP in your postet link.
This https://www.b4x.com/android/forum/threads/multicastsocket.73437/#post-466959 code works with B4J.


B4X:
#Region  Project Attributes
    #ApplicationLabel: Mcast 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

'i Ad this To Manifest
'AddPermission(android.permission.CHANGE_WIFI_MULTICAST_STATE)

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    Dim MulticastLock As JavaObject
    Dim udp As UDPSocket
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    AcquireMulticastLock
    udp.Initialize("udp",5683,4096)
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub udp_PacketArrived (Packet As UDPPacket)
    Log(Packet.HostAddress)
End Sub


Sub AcquireMulticastLock
    Try
        Dim ctxt As JavaObject
        ctxt.InitializeContext
        Dim WifiManager As JavaObject = ctxt.RunMethod("getSystemService", Array("wifi"))
        MulticastLock = WifiManager.RunMethodJO("createMulticastLock", Array("b4a-udp"))
        MulticastLock.RunMethod("acquire", Null)
    Catch
        Log(LastException)
    End Try
End Sub
 
Upvote 0

monki

Active Member
Licensed User
Longtime User
Is it possible to follow the code mentioned in the link above
Convert to Android.?
 
Upvote 0

monki

Active Member
Licensed User
Longtime User
I have Tried, the Compiler say

B4A Version: 11.20
Parse den Code. (0.00s)
Java Version: 11
Building folders structure. (0.01s)
Kompiliere den Code. (0.00s)
Kompiliere Layoutcode. (0.00s)
Organisiere Libraries. (0.00s)
(AndroidX SDK)
Ressourcen zusammenstellen (0.06s)
Ressourcen verknüpfen (0.22s)
Kompiliere generierten Java Code. Error
B4A line: 49
End Sub
src\b4a\example\main.java:433: error: cannot find symbol
udp.init(ba, EventName, 8192, socket);
^
symbol: variable ba
location: class main
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The B4A library is indeed different.

Try this:
B4XPages code:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private udp As UDPSocket
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    CreateMulticastSocket("udp", 1900, "239.255.255.250")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub

Sub CreateMulticastSocket (EventName As String, Port As Int, Group As String)
    Dim multi As JavaObject
    multi.InitializeNewInstance("java.net.MulticastSocket", Array(Port))
    Dim inet As JavaObject
    multi.RunMethod("joinGroup", Array(inet.InitializeStatic("java.net.InetAddress").RunMethod("getByName", Array(Group))))
    Dim jo As JavaObject = Me
    jo.RunMethod("SetMulticastSocket", Array(EventName, multi, udp))
End Sub

#if java
import java.net.*;
public void SetMulticastSocket (String EventName, MulticastSocket socket, anywheresoftware.b4a.objects.SocketWrapper.UDPSocket udp) {
   udp.init(getBA(), EventName, 8192, socket);
}
#end if

2. Use jNetwork from B4J, instead of B4A Network library (copy it to the additional libraries folder).
 
Upvote 0

monki

Active Member
Licensed User
Longtime User
Erel you are the best !!
I tested the code above and it works perfectly.
I transferred you € 20
Many Thanks
monki
 
Upvote 0

Paul_

Member
Licensed User
Erel you are the best !!
I tested the code above and it works perfectly.
I transferred you € 20
Many Thanks
monki

I am trying to monitor SSDP packets on port 1900 using the example code posted by Erel above, I am also using the B4J jNetwork lib as recommended.

But I am still get the same error as you did before (as shown below) ?

Monki can you post a simple app with working B4A multicast code ?

Thanks.

javac 1.8.0_261
src\b4a\ssdpcapture\main.java:1275: error: cannot find symbol
udp.init(getBA(), EventName, 8192, socket);
^
symbol: method getBA()
location: class main
 
Upvote 0

Paul_

Member
Licensed User
Erel you are the best !!
I tested the code above and it works perfectly.
I transferred you € 20
Many Thanks
monki

I have attached my simple example with the compile error.

This is a non-B4X pages app.

If Erel, monki or anyone else can help find the bug I would appreciate it, many thanks.

Using the jNetwork lib as suggested.

Paul
 

Attachments

  • UDP SSDP Capture Example.zip
    50.4 KB · Views: 201
Upvote 0

monki

Active Member
Licensed User
Longtime User
Hello Paul,
I have moved the relevant code into a class. the attached code works for me.
monki
 

Attachments

  • ssdpcap.zip
    21.5 KB · Views: 219
Upvote 0

Paul_

Member
Licensed User
Hello Paul,
I have moved the relevant code into a class. the attached code works for me.
monki

Hi monki,
Your app works 100%, but I'm not really sure what the difference is with the original code. It might be because the 'Me' keyword can only be used inside a class module?
Many thanks for your help ?
Paul
 
Upvote 0
Top