11

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 out that my device (and the emulator) never send a MulticastSocket.joinGroup request. I tried running the same Java code from my PC as a standalone application and it worked well. Could it be that the Android platform blocks IGMP join requests? Has anyone succeeded with Multicast on Android before?

My manifest file contains the following permission:

I am running my application on 2.1 (Both emulator & device).

Any ideas anyone?

Thanks,

Eyal
  • 131
  • 1
  • 1
  • 3

4 Answers4

9

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:

<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>


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.

Paul Gregoire
  • 9,715
  • 11
  • 67
  • 131
  • Url which provided is giving 404. The correct url is without `/` at the end of the url. http://codeisland.org/2012/udp-multicast-on-android – Mahendran Sakkarai Apr 01 '16 at 09:14
4

As it seems, there is no proper multicast support in the emulator.

Here's a bug report and related thread. It is being fixed for froyo.

Milan
  • 15,389
  • 20
  • 57
  • 65
3

You need to do something like this

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

Reference: http://developer.android.com/reference/android/net/wifi/WifiManager.MulticastLock.html

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Divyanshu
  • 31
  • 2
0

I read all 2.1 devices not supporting IGMP stack.

IGMP was missing on different HTC, Samsung, and Motorola devices of all android version from 2.1 up to 3.2.

Link in which i read http://www.programmingmobile.com/2012/01/multicast-and-android-big-headache.html

SoftwareGuy
  • 1,121
  • 2
  • 11
  • 23