Connect to multiple BLE devices sequentially

I am working on an app where I will have anywhere from 2 to 32 ble devices that I will need to send strings to. I am able to scan, connect, and send strings to a single device without issue.

What I can't seem to workout is how to take a list of the devices, connect/send strings/disconnect and then repeat on the next device in the list. I end up locking the companion app, or crashing it. Or it runs through the processes I'm trying to use, never connecting and sending the string.

Searching here for multiple ble shows a few examples of using multiple bluetoothLE components to connect to devices simultaneously however I don't believe that is what I need to do.

I've included my latest failed attempt at working this out. I'm nearly certain I'm just "doing it wrong" but I don't know where or why it is wrong. The idea here is I press the send button, it pulls entries in the tinydb that match and pulls the MAC address from that entry and connects to it. Then it waits till connected, calls to send the strings, sends the strings and comes back to disconnect and then move on to the next entry.
This example hangs the companion app.

Any suggestions would be greatly appreciated. I'm running out of hair to pull out over this.


Your WHILE loop is a problem.

Pull in a Clock component from the Sensors drawer, and use its Timer event block in conjunction with a global list of BRX_ID values loaded from your TinyDB to test connections in one pass over the list per Timer event.

You might need two lists:

  • a list of IDs with connect attempts in progress
  • a list of IDs with disconnect attempts in progress.

Any successful connection/disconnection would be an occasion for you to call the appropriate procedure, then remove the BEX_ID value from whichever list you are servicing.

P.S. Check the BLE block docs to see if there is an event that responds to a successful WriteStringsWith Response operation. If so, it would be a good place to update your lists of things that are needed to be done.

Thank you for the suggestions. I'm not sure I get it but I will see what I can come up with. I certainly was not working with the clock in any of my previous attempts.

Promising success followed by unexplained failures.

I was able to work through your suggestions and came up with something that did the basics of what I was after. It would connect to device 1, send a string, disconnect and then move to device 2 to repeat. That was promising, proof of concept achieved so I moved on to adding a list of strings to send.

At this point it seems as the connection reliability deteriorated, such that now using the same components that allowed it to move through 4 devices in my test, now gives a connection timed out.

I used nRFConnect from the same phone I am running the companion app to verify that I can still connect to the devices and send strings to the successfully to rule out the phone and the device.

In this process I did upgrade the BLE extension in my project as I was attempting to add some functionality to one of my existing projects. In searching for possible solutions I came across a thread saying something to the effect that the old and new extensions reside in different places and that might be causing a issue.

Any further insights?