I have a few threads managing my BLE communications. I am finding that is one thread is in the middle of performing a .connect() process to a peripheral, my other threads are unable to update characteristics using .setValue() until the .connect() complete. The .connect() call often takes 3 seconds to run which is a fairly long time.
thread_1:
BLE.connect(); // This takes about 3 seconds to complete
thread_2:
ble_char.setValue() // This will halt until the connect() is complete in thread_1
Is this by design?
Is there a way to speed up .connect() or minimize the amount of time it locks the BLE stack?
It does appear to be a lock that’s causing those two calls to block each other. There wasn’t an obvious workaround to easily make connect less synchronous, unfortunately.
Thanks for getting back to me. What I have done (which has helped some) is to break the connect into four pieces and yield my thread at each step. It is not perfect, but it does help:
// Connect with the automatic flag set to false - connects faster
peer = BLE.connect(address, false);
// Yield
delay(10);
// Discovery
peer.discoverAllCharacteristics();
// Yield
delay(10);
// Get the characteristic - This appears to be the real bottleneck
peer.getCharacteristicByUUID(char, char_uuid);
// Yield
delay(10);
// Set the notification handler
char.onDataReceived(onNotification, NULL);
// Yield
delay(10);