BLE HID connections to multiple Centrals
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

BLE HID connections to multiple Centrals

by atmakers on Thu Apr 22, 2021 1:07 am

Hi folks,

I've been working with @danh on CircuitPython and @hathach on Arduino to try to find a way to handle a single NRF52840 Bluefruit device (Feather, ItsyBitsy, CircuitPlayground BLE) can bond to multiple Central Devices and then send HID events (Keyboard OR mouse - both would be nice, but one at a time is enough) to each device selectively.

@hathach asked me to write up a forum post so that we'd have a place to keep track of this and communicate.

The use case is from Assistive Tech - our users want to their switch interfaces (or custom mice, whatever) to be able to switch between their devices. For example, they want to pair one device to an iPhone, Android Tablet, PC, and Speech Generating Device as a Keyboard. Each of those devices has interfaces that will take keystrokes and convert them into "Switch Control" or another switch- or mouse-based system that maps the user's abilities (press these three switches in these combinations) to what they want to do ("move the cursor down the screen") So, the users want to be able to bond their devices to the Bluefruit, then switch between which device it's sending keystrokes to when they need to.

To make this happen, there are three things we need to do:

1) Bond (Pair) and connect to multiple centrals simultaneously as an HID Keyboard/Mouse (both Arduino & CP can do this)
2) Keep track of the devices so that the NRF52840 remembers the order they were bonded and can keep them in that order when they disconnect & reconnect (it looks like Arduino can do this using the getPeerAddr() and storing the list in non-volatile memory - I don't see a way to get this info in CP
3) Selectively choose which connection to send HID reports and send them only to the "current" device (CP currently sends to all connections as HIDService is not connection specific and has no way to target a single connection - Arduino library does)

We would like this to work in both Arduino and CircuitPython. At this point it looks like the Arduino library is further along in multi-connection support, but we have not yet gotten this to work. In the long run, our users prefer CircuitPython because it is simpler for them to customize and modify it in place... however, we can work with either or both.

@hathach has been good enough to point me to this example: https://github.com/adafruit/Adafruit_nR ... ti.ino#L97

While it does publish to all peers, it seems like it can do so selectively if it wants.. I plan to adjust this to rotate between the connected centrals and then to rewrite it to use the HID Service. Remaining questions are around how to keep track of which Central is which across disconnect/reconnects.

On the CircuitPython side, there seems no way to have two HIDServices that talk to different connections, and there is no way to tell the single HIDService to only send HID Reports to single connection. So, there is really no way with the current API to use the higher level API to do this. It MAY be possible with _bleio directly, but I'm pretty sure we want to add this support to the high-level API.

I'd love some feedback, especially on the CP side as it seems broken. On the Arduino side, info about how to get a unique ID for a connected Central that is the same across multiple connect/disconnect events would be very helpful

Thank you!

Posts: 23
Joined: Fri Mar 10, 2017 2:16 pm

Re: BLE HID connections to multiple Centrals

by hathach on Thu Apr 22, 2021 1:13 am

great post, you should use the current tip on github of Arduino nrf52, it has better support for pairing and bond management with resolvable address.

Posts: 1153
Joined: Tue Apr 23, 2013 1:02 am

Please be positive and constructive with your questions and comments.