[Solved?] Connecting to multiple BLE devices fails only the first time

akolodner25
Posts: 27
Joined: Tue Nov 22, 2022 5:15 am

[Solved?] Connecting to multiple BLE devices fails only the first time

Postby akolodner25 » 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.)
Last edited by akolodner25 on Sat Nov 18, 2023 8:03 pm, edited 1 time in total.

akolodner25
Posts: 27
Joined: Tue Nov 22, 2022 5:15 am

Re: Connecting to multiple BLE devices fails only the first time

Postby akolodner25 » Sat Nov 18, 2023 8:02 pm

The issue turned out to be the BLE client which was connected to the ESP32 while it attempted to connect to the peripherals. Although the ESP32 is theoretically capable of supporting 6 simultaneous connections, for some reason things break when the client is connected during the connection to the peripherals, but work fine if the client is disconnected before connection to the peripherals, then reconnected afterwards.

Who is online

Users browsing this forum: Google [Bot], lbernstone and 13 guests