[Solved?] Connecting to multiple BLE devices fails only the first time
Posted: Thu Nov 16, 2023 7:01 pm
ESP32S3, 16MB flash, ESP-IDF v4 with Arduino, Bluedroid
My dual-mode BLE code should read MAC addresses sent by a BLE client, then form its own 5 BLE client connections to the given MAC addresses, which are MetaMotionS IMU devices. It finds the devices successfully. However, when trying to connect, one of two things happens.
- If the device starts up and sees that the MAC addresses are already stored in its SPIFFS storage, it tries to immediately connect to the addresses, and this works correctly. It takes a while to connect but works consistently.
- However, when it's initially told to connect (that is, not on startup but after a while, after receiving the 5 commands with the MAC addresses and another command telling it to start, then the connection does not work.
Specifically, the connection to each successive device will take longer and longer, until by the 4th or 5th one (usually 5th), the BLEClient::connect call hangs for a minute or so, then prints an "unknown error" message (I dug into the code and found it's receiving ESP_GATTC_ERR 0x85), and the client never connects. (This also forcibly disconnects the client connected to the ESP itself, if it's still connected.) The task running BLE startup then hangs forever.
I checked, and the ESP32 is not running out of memory. The data streams from the MetaMotionS devices have not yet been started when the error occurs. The Bluedroid GATTC layer does not print any information, even when in Verbose mode. The function that starts the error process is bta_gattc_open_fail, in bta_gattc_act.c, but I can't trace it back further than that.
Does anybody know what might be causing this? I'm totally mystified why it would fail when initializing the connection after startup, but work fine when initializing the connection during startup. (I tried disabling every part of the code except the parts that handle connection, fully cancelling the other tasks with vTaskDelete(NULL), but it still occurs.)
My dual-mode BLE code should read MAC addresses sent by a BLE client, then form its own 5 BLE client connections to the given MAC addresses, which are MetaMotionS IMU devices. It finds the devices successfully. However, when trying to connect, one of two things happens.
- If the device starts up and sees that the MAC addresses are already stored in its SPIFFS storage, it tries to immediately connect to the addresses, and this works correctly. It takes a while to connect but works consistently.
- However, when it's initially told to connect (that is, not on startup but after a while, after receiving the 5 commands with the MAC addresses and another command telling it to start, then the connection does not work.
Specifically, the connection to each successive device will take longer and longer, until by the 4th or 5th one (usually 5th), the BLEClient::connect call hangs for a minute or so, then prints an "unknown error" message (I dug into the code and found it's receiving ESP_GATTC_ERR 0x85), and the client never connects. (This also forcibly disconnects the client connected to the ESP itself, if it's still connected.) The task running BLE startup then hangs forever.
I checked, and the ESP32 is not running out of memory. The data streams from the MetaMotionS devices have not yet been started when the error occurs. The Bluedroid GATTC layer does not print any information, even when in Verbose mode. The function that starts the error process is bta_gattc_open_fail, in bta_gattc_act.c, but I can't trace it back further than that.
Does anybody know what might be causing this? I'm totally mystified why it would fail when initializing the connection after startup, but work fine when initializing the connection during startup. (I tried disabling every part of the code except the parts that handle connection, fully cancelling the other tasks with vTaskDelete(NULL), but it still occurs.)