Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is CHANGE_WIFI_MULTICAST_STATE permission and use of WifiManager.MulticastLock required to receive multicast packets on Android?

I have a test app that I'm using to demonstrate whether or not multicast traffic is making it into an Android device. I'm seeing behavior that appears to contradict Google's documentation here and here which implies that two prerequisites need to be in place in order for an app to receive multicast traffic:

  1. Acquire the MulticastLock
  2. Add the CHANGE_WIFI_MULTICAST_STATE permission to the app's manifest

My app is able to receive multicast traffic without either of these items in place. Below is the code snippet that sets up the multicast socket for receiving data.

MulticastSocket multicastReceiveSocket = new MulticastSocket( 18200 );
multicastReceiveSocket.joinGroup( InetAddress.getByName( "232.232.232.232" ) );

byte[] buffer = new byte[ 65536 ];
DatagramPacket packet = new DatagramPacket( buffer, buffer.length );
multicastReceiveSocket.receive( packet );

Should this be happening? Should I be able to receive multicast packets without either of the 2 prerequisites in place? I noticed that the MulticastLock documentation says:

Normally the Wifi stack filters out packets not explicitly addressed to this device

Does this mean that the behavior could be different from one device to another? I'm testing my app on a Samsung Galaxy Note4 (model SM-N910T) running Android version 4.4.4. Any clarification on this issue would be greatly appreciated.

like image 425
unbrokenrabbit Avatar asked Oct 31 '22 02:10

unbrokenrabbit


1 Answers

WifiManager.MulticastLock when acquired, would modify the state of Wifi interface on the device (gobally), to allow all apps to receive multicast traffic. your app will need CHANGE_WIFI_MULTICAST_STATE permission if your app wants to acquire this lock.

In your case, if multicast traffic is received without acquiring this lock, it must mean that either

(1) some other app has acquired this lock, due to which, the interface is now parsing/filtering and delivering multicast packets also.

or

(2) manufacturer of the device ignored this android api while developing the device, and interface always delivers all multicast traffic.

The description of the API does not force manufacturers to only allow multicast traffic when an app has acquired this lock.

Regardless, apps must acquire this lock if they want to reliably receive multicast traffic.

like image 125
GPS Avatar answered Nov 09 '22 12:11

GPS