I modified the Android Bluetooth chat example a bit and notice strange delays in the arrival of messages. My setup:
- One side (A) sends 10 messages per second.
- The other side (B) then receives the messages an measures the time difference between them.
They arrive at regular intervals: For each sample of 100 messages, the largest measured time difference is always below 150 ms. So far, so good.
Now B also sends a single message. On some devices, after a few seconds, some messages from A will be received by B with a large delay. There will be nothing for 500-1000 ms, then the delayed messages will arrive quickly behind each other. Out of 100 messages, there will be 3-6 such delays.
Now if B also sends messages every now and then (once per second), this problem does not occur. Also, it will not happen if B never sends a message. I tested a number of tablets, and not all show this problem. I notice it with Samsung and Huawei tablets with Android 8 or 9, but I think it does not happen with Nexus 7 or Sony tablets with Android 5 and 6.
It looks to me a bit like the Bluetooth driver will go to sleep every now and then, but only once the device has sent a message. Did anyone else notice this behavior, and did you find a better solution to avoid those delays?
(My app is the Android Bluetooth chat example, just modified to continuously send messages at a certain interval at the push of a button.)