I have BLE central device, written using legacy SDK. I also have some sensors, also written using the same SDK.
Quite often, there's a connection problem:
<debug> ble_scan: Scanning parameters have been changed successfully
<debug> ble_scan: Filter set on address 0x
<debug> ble_scan: 00 C3 70 5A 1A F9 |..pZ..
<debug> ble_scan: Scanning
<debug> ble_scan: Connecting
<debug> ble_scan: Connection status: 0
<info> app: NRF_BLE_SCAN_EVT_FILTER_MATCH
<debug> nrf_ble_gatt: Requesting to update ATT MTU to 247 bytes on connection 0x0.
<debug> nrf_ble_gatt: Updating data length to 251 on connection 0x0.
<info> app: NRF_BLE_SCAN_EVT_CONNECTED 00:C3:70:5A:1A:F9
<info> app: BLE_GAP_EVT_CONNECTED
<info> app: Connected as a central.
<info> app: Discovering GATT database...
<debug> nrf_ble_gq: Purging request queue with id: 0
<debug> nrf_ble_gq: Registering connection handle: 0x0000
<debug> ble_db_disc: Starting discovery of service with UUID 0x3EC0 on connection handle 0x0.
<debug> nrf_ble_gq: Adding item to the request queue
<debug> nrf_ble_gq: GATTC Primary Services Discovery Request
<debug> nrf_ble_gq: SD is currently busy. The GATT request procedure will be attempted again later.
<debug> nrf_ble_gq: Processing the request queue...
<debug> nrf_ble_gq: GATTC Primary Service Discovery Request
<debug> nrf_ble_gq: SD is currently busy. The GATT request procedure will be attempted again la
ter.
<info> app: BLE_GAP_EVT_DISCONNECTED
<debug> app: HCI status code 0x3E Connection Failed to be Established
After scan, I set filter for sensor MAC address, then BLE_GAP_EVT_CONNECTED event is received, and in this event ble_db_discovery_start() is called. Discovery module tries to queue some requests, they are postponed due to SoftDevice being busy, but as far as I can see, that's typical behavior, because it's the same on successful connection.
And then, almost immediately, BLE_GAP_EVT_DISCONNECTED event is received and connection is lost.
Reading disconnect reason from event data gives HCI status code 0x3E, and that's quite generic "Connection Failed to be Established" (last status code, that's a hint of catch-all case).
And it happens sometimes, can't see any correlation with anything. Sometimes it's a long series of failures - for example, can't connect for 10 minutes, trying in a loop. Then I'm making a small break (stopping some client software than commands the BLE central), and then it suddenly works. And then it can work for some long time. Sometimes resetting BLE central helps, and sometimes these disconnects happen out of reset. No pattern. Looks a bit timing related, but really, hard to correlate with anything else.
BLE peripheral (my sensors) don't show anything in debug log, when these failed connection attempts happen. There's no CONNECT event there, it fails earlier than CONNECT event.
Initially I thought that there's something wrong with my peripherals, because connecting from nRF Connect running on the Android phone also exhibits some erratic connection behavior - sometimes I have to retry connecting 2 or 3 times, before it connects. When it fails, it fails quite fast - usually less than a second, and nRFConnect log shows Error 133 (0x85) GATT ERROR, nothing more. No sign of incoming connection in my sensor log, so it fails before CONNECT event. But second or third attempt, and it connects. Completely different pattern of failure than my BLE central, which sometimes can't connect for a long time.
BTW, what's going on with nRF Connect, with these connection failures?
So I thought that it's something wrong with my sensor. I have some ready-made sensors from some other manufacturer, even made with nrf51 (my devices use mrf52832 and 52840). I adapted the code to connect to these sensors, but my BLE central has exactly the same trouble connecting, so no, I don't think it's sensor firmware related.
Here is similar case: https://devzone.nordicsemi.com/f/nordic-q-a/65434/ble_db_discovery_start-return-0x11 - not resolved.
Any thoghts? Help, please. How to debug this case?