Problems with hivemq in the background on Android

We’ve had problems with running the hivemq client in the background on android in a foreground service. Our service is running as a location foreground service, and it reliably wakes up and does stuff when we get a location or a work manager timer goes off. However, sometimes when we talk to the hivemq client, hivemq reports that it is connected, but it isn’t actually communicating with the server to send or receive messages. We also see that keepalive pingreqs fail to send while the app isn’t in the foreground or the device plugged in. With the device plugged in, pings seem to flow out as expected even with the app in the background. When looking into the hivemq code, it looks like it is using an executor to schedule ping tasks, which doesn’t appear to work when the app is in the background.

While trying to debug/work around this issue, we’ve found that if the client gets into the disconnected-but-reporting-connected state, it can be resolved by telling it to disconnect from the server and reconnect, either from a watchdog in a work manager, or manually calling disconnect in the debugger.

We’ve also run into a problem with some NAT routers having a short lived expiration (5 minutes) and dropping our tcp connection to the server, which is likely impacting some of these findings. Since hivemq isn’t sending pings, we can’t keep these sessions alive by shortening our keepalive.

The pings not being sent out were verified in wireshark and breakpoints in the debugger.

I’m not sure if this is one issue or multiple issues, so I’m presenting what I’m seeing in the hopes that someone on the hivemq team can look into this.

Thanks!