How to receive Multicast packets on Android

后端 未结 4 1795
囚心锁ツ
囚心锁ツ 2021-01-04 19:25

I\'m trying to receive data from a multicast address, but the call to MulticastSocket.receive() blocks until a timeout takes place. I did some network sniffing and found ou

4条回答
  •  暖寄归人
    2021-01-04 19:39

    Lukas gives the best explanation and examples that I've seen on his blog: http://codeisland.org/2012/udp-multicast-on-android

    In summary:
    1. You need the permissions:

    
    
    
    
    


    2. You need a lock:

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


    3. You have to have a device that supports receiving multicast. Or you can follow his work around for rooted devices.

提交回复
热议问题