I'm working on a Xamarin app for Android 11 that makes a lot of GATT connections, and looking for suggestions on the best way to improve reliability or reduce the frequency of errors.
A quick overview of what the app does:
- Run frequent scans to find potential GATT targets
- Scan for 12sec, stop for a short period, scan for 12sec, repeat...
- This is odd, but helps us avoid having the Android OS downgrade our scans to opportunistic
- On an interval, attempt up to 5 simultaneous GATT connections
- For each connection: discover services, get the service we want, read a characteristic, and finally disconnect & close the GATT client as quickly as we can
The overall flow works decently, except we seem to encounter a higher rate of errors than we'd expect, and end up having to retry connections.
------------------
The most frequent error we see is the Status code 133 (GATT_ERROR) during onConnectionStateChange.
We call ConnectGatt(context, false, gattCallback, BluetoothTransports.Le); to initiate the connection. MS Docs
If we see the 133 response, we immediately call Close() on the client, and wait ~1sec before trying a fresh connection to that device.
I understand this is a generic, catch-all error code, but our app seems to hit it far more often than nRF Connect. I've tried using nRF Sniffer for BLE to find any other details of what might be going wrong, but the only thing that stands out is a malformed SCAN_REQ packet:
9551 -42 dBm Apr 18, 2023 10:02:49.422582000 LE 1M LE LL 0 SCAN_REQ[Malformed Packet] 73:73:61:4d:01:79 0x3
0000 05 17 00 03 76 a3 02 0a 00 25 2a 00 00 22 2f 1e
0010 40 d6 be 89 8e c3 04 79 01 4d 61 73 73 48
I noticed this happened near the timeframe of 2 different status 133 responses that our app received, but SCAN_REQ doesn't seem like the right message type to affect GATT connections. The other packets within 2 seconds of the error just look like normal ADV_IND & SCAN_REQ/RSP.
I'm pretty new to using the BLE Sniffer tool, so any advice on how best to use it to dig deeper would be appreciated.
------------------
We also see issues with:
- Service discovery sometimes takes an excessive amount of time (we call Close() on the client after 6 seconds)
- Service discovery sometimes returns the wrong number of services (i.e. we get 9, but nRF Connect mobile shows 10)
Our process for service discovery is:
- After getting a connection established, we:
- Request connection priority of High
- wait 600ms
- call DiscoverServices()