Android Question Does anyone know an easy and reliable way to read multicast on B4A?

Dylan Meng

Member
Licensed User
Longtime User
Hi, I am designing an IoT product that required auto-discovery service.

I can fully ask a question to my device (Bonjour service), but I am not quite sure how to read multicast 224.0.0.251 [5353] with B4A.

(My device can only communicate via UDP)

Does anyone know an easy and reliable way to read multicast on Android?

Thanks for the help!
 

Dylan Meng

Member
Licensed User
Longtime User
Those work great for communication to my device and to send multicast 224.0.0.251 [5353]. However, I don't receive any data with UDP_PacketArrived for 224.0.0.251 [5353]. Just to make sure UDP_PacketArrived work great with a multicast address?
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Those work great for communication to my device and to send multicast 224.0.0.251 [5353]. However, I don't receive any data with UDP_PacketArrived for 224.0.0.251 [5353]. Just to make sure UDP_PacketArrived work great with a multicast address?
If you're sending multicast UDP from one device, all you need to do on the one you want to receive on is initialize your UDP Socket on the correct port, then handle any messages in the _PacketArrived(Packet As UDPPacket) callback.

Make sure your network allows multicast. You can use the code below to check what the multicast address is:

B4X:
'Returns the UDP broadcast address.
'Returns an empty string if not available.
Sub GetBroadcastAddress As String
    Dim niIterator As JavaObject
    niIterator = niIterator.InitializeStatic("java.net.NetworkInterface").RunMethod("getNetworkInterfaces", Null)
    Do While niIterator.RunMethod("hasMoreElements", Null)
        Dim ni As JavaObject = niIterator.RunMethod("nextElement", Null)
        If ni.RunMethod("isLoopback", Null) = False Then
            Dim addresses As List = ni.RunMethod("getInterfaceAddresses", Null)
            For Each ia As JavaObject In addresses
                Dim broadcast As Object = ia.RunMethod("getBroadcast", Null)
                If broadcast <> Null Then
                    Dim b As String = broadcast
                    Return b.SubString(1)
                End If
            Next
        End If
    Loop
    Return ""
End Sub

- Colin.
 
Last edited:
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
To clarify - are you wanting to multicast or broadcast? I took your post to mean broadcast, but if you really want to multicast then you should check that your router is capable of passing multicast packets as apparently a lot aren't.

- Colin.
 
Upvote 0

Dylan Meng

Member
Licensed User
Longtime User
Multicast, I already know that the data will be on 224.0.0.251 [5353] then I can discover the IP address of the IoT device by analyzing the answer found on 224.0.0.251.

Here more information:

- Android App
- IoT embedded device that understands Apple Bonjour / zero-configuration networking / mDNS

Android App - Send a "Question" via 224.0.0.251 [5353] (Working)
IoT device "Answer" to the "Question" again via 224.0.0.251 [5353] (Working - confirmed with Wireshark)
Android App need to read the "Answer" sent by the IoT device at 224.0.0.251 [5353] (Not working)

Quick Code example
B4X:
Dim UDPSocket1 As UDPSocket
Dim BonjourAutoDiscovery As UDPSocket

UDPSocket1.Initialize("UDP", 0, 8000)
BonjourAutoDiscovery.Initialize("BonjourAutoDiscovery",5353 , 8192)

Sub Bonjour '(Working)
    Dim Packet As UDPPacket
    Dim data() As Byte
    data = b.HexToBytes("000000000001000000000000085F73646C65645631045f746370056c6f63616c00000c0001")
    Packet.Initialize(data, "224.0.0.251", 5353)
    UDPSocket1.Send(Packet)
End Sub

Sub BonjourAutoDiscovery_PacketArrived (Packet As UDPPacket) '(Not working)
    Log(Packet.HostAddress)
    Dim msg As String
    msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
    msg = b.StringFromBytes(Packet.Data, "UTF8")
    Log(msg)
    Msgbox("Message received: " & msg, "")

    'Code to analyze the answer...

End Sub
 
Upvote 0

Dylan Meng

Member
Licensed User
Longtime User
ah maybe it because

B4X:
Sub Bonjour '(Working)
    Dim Packet As UDPPacket
    Dim data() As Byte
    data = b.HexToBytes("000000000001000000000000085F73646C65645631045f746370056c6f63616c00000c0001")
    Packet.Initialize(data, "224.0.0.251", 5353)


    'UDPSocket1.Send(Packet) 'Should be
    BonjourAutoDiscovery.Send(Packet) '?

End Sub

I will need to try that when I have my IoT device with me
 
Upvote 0
Top