Android Question Receiving UDP Broadcast Packets when phone suspended.


Licensed User
Building on the ‘Background Location Tracking’ example. I am trying to do something similar with UDP Sockets.

I need to receive UDP broadcast packets from several Huzzah32-ESP32 with an array of connected sensors every few seconds or so, log this data in a database and if specific trigger values are received, notify the user.

This all works as expected when the phone is active/alive. All Broadcast UDP packets are received, however when the screen times out or the user suspends the phone, all Broadcast UDP packets cease?

I know the service is still running, it is not being stopped or destroyed, I put a timer in the service to show a heartbeat in the log and this occurs without fail.

Then I tried, In the timer, to pole one of the devices, I send a UDP Packet to one of the ESP32 and receive a reply, even when the phone is suspended, ok… so Wi-Fi and UDP communications is still active and works when the phone is suspended, so its just not receiving unsolicited UDP broadcast.

But I don’t really want it working this way I just wanted to receive all the Broadcasts and log those regardless of phone active/sleep status i really don't want to have to pole all the boards for a status update.

Any Ideas on how to keep receiving UDP 'Broadcast' packets in the background when the phone is suspended?


Staff member
Licensed User
Make a test and try to send a UDP packet every second. Are the broadcast packets received?

Are you acquiring a multicast lock?

Sub AcquireMulticastLock
       Dim ctxt As JavaObject
       Dim WifiManager As JavaObject = ctxt.RunMethod("getSystemService", Array("wifi"))
       MulticastLock = WifiManager.RunMethodJO("createMulticastLock", Array("b4a-udp"))
       MulticastLock.RunMethod("acquire", Null)
   End Try
End Sub


Licensed User
Hi Erel,

Thanks for this. But sadly no... still the same.

As soon as the phone times out to suspend or the user suspends the phone receiving broadcast UDP Packets stops.

Having said that I'm using a samsung S9+ and going through the power, notifications and security settings it's a bit of a minefield...

So I'll pick up one of our client devices and try again



Licensed User
Hi Erel,

I am yes, other functions run as expected.
I'll have access to the other devices tomorrow so I'll test on those.

There is a good chance this is a Samsung issue.



Licensed User
Hi All,

It's only marginally better on another device. I'm receiving maybe 30% of the broadcast transmission.

Where I receive 99% if I pole the ESP32 for an immediate reply.

As much as I didn't want to do it this way is seems to be the more reliable.

Thanks anyhow ...