Sometimes it's a bug
TL;DR — while troubleshooting BLE connection issues, don't forget chip manufacturers sometimes have bugs in their BLE firmware. It's easy to forget and can cause unnecessary headaches.
Recently, I was helping someone troubleshoot random disconnects between their device and mobile app.
They followed the right steps:
- Capture the traffic via a Sniffer
- Enable device HCI logging
- Verify custom firmware was behaving as expected
- Compare app behavior to running the same commands in a 3rd party app (Light Blue, nRF Connect)
- Ensure the app Bluetooth stack is adhering to best practices (e.g. following Apple's Connection Guidelines and doing BLE operations in serial on Android)
From the analysis, it was clear that the peripheral was the culprit—it disconnected for some unknown reason with error code 0x13.
(0x13 indicates the peripheral initiated the disconnect. See the full list of error codes here).
Then...
The chip manufacturer's BLE firmware stack was upgraded.
After this, the issue was resolved.
Note: I prefer to understand what is going on. Sometimes the manufacturer clues you in, but sometimes not.
Just a friendly reminder to also make sure you are on the latest firmware for your chipset - and don't assume it is entirely bug free.