1. Black Friday Sale - 50% off

    B4A Standard for $29 instead of $59!
    Buy Now
    Dismiss Notice

Android Question Enable multicast

Discussion in 'Android Questions' started by Johnmcenroy, Jul 28, 2014.

  1. Johnmcenroy

    Johnmcenroy Active Member Licensed User

  2. RandomCoder

    RandomCoder Well-Known Member Licensed User

    DonManfred likes this.
  3. Johnmcenroy

    Johnmcenroy Active Member Licensed User

    Thanks for reply
    But I also need this:
    Code:
    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 ?
     
  4. RandomCoder

    RandomCoder Well-Known Member Licensed 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...
    Code:
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
    And the broadcast and receive sections of code...
    Code:
    #Region -------------------------[ UDP M_Search ]-------------------------
    Private Sub Initialise
        
    ' Initialise UDP socket on first available port
        udpSocket1.Initialize("UDP"08000)
        
    ' 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
     
    Erel likes this.
  5. Johnmcenroy

    Johnmcenroy Active Member Licensed User

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

    Johnmcenroy Active Member Licensed 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:

    Code:
    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: Jul 29, 2014
  7. Erel

    Erel Administrator Staff Member Licensed User

  8. RandomCoder

    RandomCoder Well-Known Member Licensed User

  9. Johnmcenroy

    Johnmcenroy Active Member Licensed User

    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:
    Code:
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
    Code:
    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: Jul 30, 2014
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice