Android Question Enable multicast

RandomCoder

Well-Known Member
Licensed User
Longtime User
Upvote 0

Johnmcenroy

Active Member
Licensed User
Longtime User
You just need to add the permission in the manifest file. See code snippet link at the top of this thread.

Kind regards,
RandomCoder

Thanks for reply
But I also need this:
B4X:
WifiManager wifi = (WifiManager)getSystemService( Context.WIFI_SERVICE );
if(wifi != null){
    WifiManager.MulticastLock lock = wifi.createMulticastLock("Log_Tag");
    lock.acquire();
}
http://codeisland.org/2012/udp-multicast-on-android/
How it can be done ?
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
I'm not sure if you actually need to acquire and later release the multicast lock or if B4A or Android takes care of it for you. I've programmed a UPNP control point app and I've not needed to explicitly set the multicast lock but it seems to work.
Anyhow, if you really want to set the lock then maybe check out this post here on the Forum which uses the reflection library to access it.

And just in-case your interested, this is the line I add to my manifest file...
B4X:
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>

And the broadcast and receive sections of code...
B4X:
#Region -------------------------[ UDP M_Search ]-------------------------
Private Sub Initialise
    ' Initialise UDP socket on first available port
    udpSocket1.Initialize("UDP", 0, 8000)
    ' Initialse Map (Key=server uuid As String , Value=server details As UPNPserver)
    serverID.Initialize
    eotTimer.Initialize("EOT_Delay", 5000)
    serverResponse = ""
End Sub

'Send a multicast message to search for devices of device type defined by 'st'
'mx - Maximum wait time in seconds
'st - Search target as defined by the UPNP device architecture
'
'Example: <code>
'M_Search(3, "ssdp:all")</code>
Private Sub M_Search(mx As Int, st As String)
    ' Compile HTTP broadcast message
    Dim msg As String       
    msg = "M-SEARCH * HTTP/1.1~" & _
          "HOST: 239.255.255.250:1900~" & _
          "MAN: " & Chr(34) & "ssdp:discover" & Chr(34) & "~" & _
          "MX: " & mx & "~" & _
          "ST: " & st & "~~"
    ' Replace "~" with "\r\n" [NOTE:- CRLF only sends Chr(13) eg."\r"]
    msg = msg.Replace("~", Chr(13) & Chr(10))
    Debug.debugLog(msg) 'DEBUG
    ' Convert string to UTF8
    Dim data() As Byte
    data = msg.GetBytes("UTF8")
    ' Initialise packet and send
    Dim packet As UDPPacket
    packet.Initialize(data, "239.255.255.250", 1900)
    udpSocket1.Send(packet)
End Sub

Private Sub UDP_PacketArrived (Packet As UDPPacket)
    ' Restart 1s timer each time a new packet arrives
    EOT_Delay_Reset(1000)
    ' Convert received data to UTF8
    serverResponse = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
    Debug.debugLog(CRLF & serverResponse) 'DEBUG
    ' Extract device uuid and description file url
    Get_Details
End Sub
......
......

Regards,
RandomCoder
 
Upvote 0

Johnmcenroy

Active Member
Licensed User
Longtime User
I'm not sure if you actually need to acquire and later release the multicast lock or if B4A or Android takes care of it for you. I've programmed a UPNP control point app and I've not needed to explicitly set the multicast lock but it seems to work.
Anyhow, if you really want to set the lock then maybe check out this post here on the Forum which uses the reflection library to access it.

And just in-case your interested, this is the line I add to my manifest file...
B4X:
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>

And the broadcast and receive sections of code...
B4X:
#Region -------------------------[ UDP M_Search ]-------------------------
Private Sub Initialise
    ' Initialise UDP socket on first available port
    udpSocket1.Initialize("UDP", 0, 8000)
    ' Initialse Map (Key=server uuid As String , Value=server details As UPNPserver)
    serverID.Initialize
    eotTimer.Initialize("EOT_Delay", 5000)
    serverResponse = ""
End Sub

'Send a multicast message to search for devices of device type defined by 'st'
'mx - Maximum wait time in seconds
'st - Search target as defined by the UPNP device architecture
'
'Example: <code>
'M_Search(3, "ssdp:all")</code>
Private Sub M_Search(mx As Int, st As String)
    ' Compile HTTP broadcast message
    Dim msg As String   
    msg = "M-SEARCH * HTTP/1.1~" & _
          "HOST: 239.255.255.250:1900~" & _
          "MAN: " & Chr(34) & "ssdp:discover" & Chr(34) & "~" & _
          "MX: " & mx & "~" & _
          "ST: " & st & "~~"
    ' Replace "~" with "\r\n" [NOTE:- CRLF only sends Chr(13) eg."\r"]
    msg = msg.Replace("~", Chr(13) & Chr(10))
    Debug.debugLog(msg) 'DEBUG
    ' Convert string to UTF8
    Dim data() As Byte
    data = msg.GetBytes("UTF8")
    ' Initialise packet and send
    Dim packet As UDPPacket
    packet.Initialize(data, "239.255.255.250", 1900)
    udpSocket1.Send(packet)
End Sub

Private Sub UDP_PacketArrived (Packet As UDPPacket)
    ' Restart 1s timer each time a new packet arrives
    EOT_Delay_Reset(1000)
    ' Convert received data to UTF8
    serverResponse = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
    Debug.debugLog(CRLF & serverResponse) 'DEBUG
    ' Extract device uuid and description file url
    Get_Details
End Sub
......
......

Regards,
RandomCoder

Thanks for code. What I want is to recieve multicast iptv from address udp://@238.1.1.1:1234 in videoview for example.
 
Upvote 0

Johnmcenroy

Active Member
Licensed User
Longtime User
So I need not broadcast but multicast. I know there are problems in android of supporting multicast. I will try of course with simply add permission but as I understand for multicast I need this code:

B4X:
WifiManager wifi = (WifiManager)getSystemService( Context.WIFI_SERVICE );
if(wifi != null){
    WifiManager.MulticastLock lock = wifi.createMulticastLock("Log_Tag");
    lock.acquire();
}

http://codeisland.org/2012/udp-multicast-on-android/
Am I need a wrapper for such code or it can be done with reflection/javaobject ?

Thanks
John
 
Last edited:
Upvote 0

Johnmcenroy

Active Member
Licensed User
Longtime User
This is the same link as I've already provided above (but maybe my link wasn't as obvious?). What you want to do can be achieved with use of the reflection library.

Regards,
RandomCoder

Didn't saw your link. But saw Erel's link. Thanks to all answers. There are problems with recieve multicast iptv , none of my devices work with this. Only some android kernels can do this. All work only through UDP-to-HTTP proxy.

The code to disable multicast lock:
B4X:
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />

B4X:
ReflectorIGMP.Target = ReflectorIGMP.GetContext
    ReflectorIGMP.Target = ReflectorIGMP.RunMethod2("getSystemService", "wifi", "java.lang.String")
    ReflectorIGMP.Target = ReflectorIGMP.RunMethod2("createMulticastLock", "mylock", "java.lang.String")
    ReflectorIGMP.RunMethod2("setReferenceCounted", False, "java.lang.boolean") ' not really necessary but safer
    ReflectorIGMP.RunMethod("acquire")

So it is big problem of android devices to support multicast stream.

P.S. I have rather large resolution on rather small display. Now I see your link in post.

Regards
John
 
Last edited:
Upvote 0
Top