Not receiving UDP Broadcast packets on Android [solved]


#1

I have working JUCE code that receives UDP Broadcast packets on the Desktop, but it does not seem to work on an Android device. I am able to communicate with the device with a unicast packet, so I know basic communications is working.

Does anyone have any experience with this? Several discussions of Android/UDP broadcast reception indicated having to use a MulticastLock, but those are references to Java related code, not C. And per those posts, I hoped that possibly just adding <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"> to the manifest would do the trick, but no such luck.

@Don_Turner I know you are an audio guy, but maybe you know someone who could help me out?


#2

Do you create the DatagramSocket object with “enableBroadcasting = true”? I use it for recognising different devices in a network and works well on Android, iOS, Linux etc.

Example code:

DatagramSocket socket(true);

String message = "broadcast!";
if (socket.waitUntilReady(false, 1000))
    socket.write("255.255.255.255", 7766, message.getCharPointer(), message.length());

#3

@MBO, thanks for replying. I am pretty sure I had the enableBroadcasting flag set to true. I’ll double check when I circle back to the issue. Your example shows sending a broadcast message, but I am needing to receive a broadcast message. Do you have an example of your receive code?


#4

Yes. I have another thread for this task, so it looks like:

UDPReceiver::UDPReceiver(): Thread("UDPReceiver")
{
	socket.setEnablePortReuse(true);
}

UDPReceiver::~UDPReceiver()
{
	stopThread(1000);
}

void UDPReceiver::run()
{
	if (!socket.bindToPort(7766))
		return;

	while (!threadShouldExit())
		if (socket.waitUntilReady(true, 0))
		{
			char buf[1000];
			int cnt = socket.read(buf, 1000, false);

			// do something...
		}
}

#5

@MBO, thanks again. my code matches what you have shown here, but it’s not working. I don’t know if it relates to the Android OS version (7.1.1, patched May 1st 2018), or the broadcast mask, which for ArtNet is just the last octet (ie. 192.168.1.255), where in your send broadcast example you are doing all four (255.255.255.255). Bottom line, you have verified that JUCE does the right thing for Android, and it should be working, so I’ll need to scratch my head for a bit on this.


#6

Actually, the broadcast address shouldn’t be the issue, I just verified the devices configuration should allow for this:

wlan0     Link encap:UNSPEC
          inet addr:192.168.1.237  Bcast:192.168.1.255  Mask:255.255.255.0

Dang, I hate it when things that should work do not work…


#7

Whew! Got it working. I have a sender and a receiver using the same port, so I had to do setEnablePortReuse on both of them.


#8

ok… um… kind of solved… the reception of the broadcast packet is very intermittent… :frowning:


#9

Some time ago I had a similar issue, so I would suggest running a receiver application on different computers/devices to verify where is the problem. Another thing is that UDP messages are not guaranteed to arrive…


#10

This same code works like a champ on multiple (Windows) desktop machines on this network. Next week my new Mac is coming in, so I will be verifying/testing on OSX, and also iOS.

An occasional dropped packet would be one thing, but the device is missing 5-100+ packets coming in at about ~8 second intervals…


#11

Maybe try on another Android device if possible?
You can try to turn off Bluetooth on this Android device and check if it helps - on some devices it can be a problem, especially with Bluetooth/wifi combos.

Check also if you have dropouts with the global (255.255.255.255) broadcast.