Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

Robust Bluetooth LE communication between CPB boards
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Robust Bluetooth LE communication between CPB boards

by kevinjwalters on Wed Jan 08, 2020 4:57 pm

I'd like to know how to write code which is robust for communicatin over Bluetooth Low Energy between Circuit Playground Bluefruit (CPB) boards.

There's some useful code to use two CPBs as a central and periperhal device to allow commucation between them in Color Remote with Circuit Playground Bluefruit: Code the Color Remote with CircuitPython. That code:

  • Catches OSError from write to a UARTService object.
  • Checks for ble.connected in peripheral device in the main loop and only attempts reads when it has recently returned True.

I took a device out of range in the middle of exchanging some data packets and got one of these:

Code: Select all | TOGGLE FULL SIZE
Traceback (most recent call last):
  File "code.py", line 544, in <module>
  File "code.py", line 492, in <module>
  File "code.py", line 339, in barrier
  File "/lib/adafruit_ble/services/nordic.py", line 108, in write
  File "/lib/adafruit_ble/characteristics/stream.py", line 49, in write
_bleio.ConnectionError: Not connected


There must be more that's needed here for a chatty protocol with packets passing both to and from the peripheral to make it more robust and allow recovery for transient connectivity issues. I'd like to know what clean up needs to be done from that exception to reconnect.

kevinjwalters
 
Posts: 982
Joined: Sun Oct 01, 2017 3:15 pm

Re: Robust Bluetooth LE communication between CPB boards

by tannewt on Wed Jan 08, 2020 6:22 pm

Hi Kevin, we haven't come up with a good way to manage BLE connections. They can be lost at any time and may generate the ConnectionError. For now, I'd suggest catching that error whenever you do something over BLE.

While this is annoying, I haven't thought of a better way to manage it. If we don't raise an exception then we'd need a way to return fake valid data that won't cause a subsequent, more confusing exception.

With BLE bonding support (coming soon), we'll be able to re-pair upon reconnect. However, for now a reconnection with pairing require manual intervention.

In summary, experiment and let us know what works best for you. It's all very new and exciting.

tannewt
 
Posts: 2680
Joined: Thu Oct 06, 2016 8:48 pm

Re: Robust Bluetooth LE communication between CPB boards

by kevinjwalters on Thu Jan 09, 2020 11:39 am

Thanks, I've realised I only need the connection for about a minute so in my case it's unlikely to get disconnected but I'll do a bit more experimentation.

On a related subject, the Packet class uses single character letters in the serialised data to type the packets, is there any recommended range of characters for application specific custom packets?

kevinjwalters
 
Posts: 982
Joined: Sun Oct 01, 2017 3:15 pm

Re: Robust Bluetooth LE communication between CPB boards

by kevinjwalters on Thu Jan 09, 2020 1:55 pm

Forgot to mention there's also example code for UARTService based commucation in Circuit Playground Bluefruit NeoPixel Animation and Color Remote Control.

kevinjwalters
 
Posts: 982
Joined: Sun Oct 01, 2017 3:15 pm

Re: Robust Bluetooth LE communication between CPB boards

by kevinjwalters on Thu Jan 09, 2020 2:04 pm

There's also more characters in use than just the ones in the packets in https://github.com/adafruit/Adafruit_Ci ... it_connect .I see an I (capital i) in Adafruit Learn: Bluefruit LE Connect for iOS and Android: Image Transfer. Are all of these listed/registered somewhere?

kevinjwalters
 
Posts: 982
Joined: Sun Oct 01, 2017 3:15 pm

Re: Robust Bluetooth LE communication between CPB boards

by tannewt on Fri Jan 10, 2020 3:17 pm

I'd suggest creating a custom service or using the Nordic UART if you want custom packets. The Adafruit BluefruitConnect protocol is just for interfacing with the matching app.

tannewt
 
Posts: 2680
Joined: Thu Oct 06, 2016 8:48 pm

Re: Robust Bluetooth LE communication between CPB boards

by kevinjwalters on Thu Jan 23, 2020 11:33 am

I'd already started sub-classing Packet to make my own application specific packets for Adafruit Learn: Circuit Playground Bluefruit Quick Draw Duo. This looked like a neat and easy way of doing it. Are there any recommendations either way on doing this?

If there's going to be more end users coding for Bluetooth it would be useful to have a (cross-language) easy way to send data between devices.

kevinjwalters
 
Posts: 982
Joined: Sun Oct 01, 2017 3:15 pm

Re: Robust Bluetooth LE communication between CPB boards

by danhalbert on Thu Jan 23, 2020 12:34 pm

Bluefruit is an ad hoc protocol built on top the Nordic UART Service. Subclassing Packet as you did is the straightforward way to do it. As tannewt2 mentioned, you can also define your own BLE Services, which can be used on either the client or server side. We don't have a Learn Guide of how to do this in general right now, though the client and periph examples in the CircuitPython_Adafruit_BLE library are a starting point.

Also in the examples in that library is an example (ble_color_prximity.py) of using only advertising (no connections) to talk among multiple boards.

danhalbert
 
Posts: 2603
Joined: Tue Aug 08, 2017 12:37 pm

Please be positive and constructive with your questions and comments.