Missing WiFi MulticastLock on Android for certain devices

Hi everyone,

// EDITED THE TITLE since i found out what the problem is. Please see first answer.

NetworkServiceDiscovery::AvailableServicesList problem here.

I’m using this class to find a broadcasting service on the network. Works perfectly on iOS, Mac, Win. On Android though I get inconsistent behaviour.

AvailableServicesList runs a thread on which it tries to read from a port like so:

   if (socket.waitUntilReady (true, 200) == 1) {...} //socket is a DatagramSocket object...

On my android tablet running Android 9 this is no problem. On my android phone running Android 4.4 - no problem either. On my brand new Google Pixel 3XL running Android 10 this returns 0! Every time. Which means it’s timing out. Even cranking the timeout value up to 10 seconds doesn’t make a difference, the socket is never ready. Any ideas what could be happening here?

1 Like

On certain devices (like the Google Pixel 3XL) Juce is missing permissions to receive network broadcasts

See the first answer on this post:

What I had to do was to add a Custom Permission in the respective Projucer fields:

android.permission.CHANGE_WIFI_MULTICAST_STATE 

Then I imported these in JuceApp.java

import android.net.wifi.WifiManager;
import 	android.net.wifi.WifiManager.MulticastLock;

and added these four lines into the onCreate method in JuceApp.java

      WifiManager wifi = (WifiManager) getSystemService(getBaseContext().WIFI_SERVICE);
    MulticastLock multicastLock = wifi.createMulticastLock("multicastLock");
    multicastLock.setReferenceCounted(true);
    multicastLock.acquire();

What is missing from the stackoverflow post is, that I’m not clearing the lock again, since I don’t know how to do my own JNI functions (I’ve been on this for about two weeks now and it’s a mess). So this is obviously not the best solution, but it fixes the problem that on certain android devices Juce can’t receive broadcasts

I’ve added this in the following commit:

Can you see if it fixes the issue for you?

2 Likes

tested on the Google Pixel 3XL on which it didn’t work before and now it does work!

Thanks a lot, also this is perfect for another reason. I’ve been fighting with JNI for a couple of weeks now and this commit is an A1 example on how to do it, since I tried accomplishing this myself and did everything wrong as I can see now. This is amazing, thanks for implementing this.