Working around BLE.connect(deviceAddress) is a blocking call

Given that BLE.connect(deviceAddress) is a blocking call, and I would like to make sure my main thread doesn't get held up by this, is it safe to run it in a second Thread?

Yes, you can call it from a worker thread.

Just be careful of general thread-safety issues when accessing it from multiple threads. It may be easier just to do all operations from the worker thread.

1 Like

You mean like scanning for the serviceId, getting characteristics after the connect, etc?

I would say anything that starts with BLE.

In one project I got in trouble, I have a worker thread that does all BLE scan, connection, etc. When I simply call from loop():

BLE.peerCentral().connected();

it may cause havoc, and this on DeviceOS 5.9.
So beware, for now things are sensitive around BLE.
PS: Particle is working on a solution to this, coming up in next DeviceOS potentially.

Easier and safer!

2 Likes

great advice, thanks guys!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.