BLE, disconnecting from a central

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
FriehoffT
 
Posts: 56
Joined: Wed Sep 11, 2019 7:30 am

BLE, disconnecting from a central

Post by FriehoffT »

I can connect with my CP Keyboard app to multiple centrals simultaneously.
While connected I can do:

Code: Select all

        i = 0
        for c in self.ble.connections:
            i += 1
            if i > 1:
                print(f" one disconnect i:{i} c:{c}", dir(c))
                c.disconnect()
Which disconnects from all connections except the first one.
But I would like to get more information from the BLEConnection or from ._bleio_connection of the BLEConnection object about the central which is "behind" this connection,
Is there any way to query such information?

Thanks
Thomas

User avatar
danhalbert
 
Posts: 4654
Joined: Tue Aug 08, 2017 12:37 pm

Re: BLE, disconnecting from a central

Post by danhalbert »

There isn't much information available through the API. Internally, there is a handle number for the connection, but that's used by the nRF SoftDevice. Are you trying to find out the central's address or something like that?

The central's address is often a random number that changes each time, for security reasons. So it's not really useful to help identify which central it is.

User avatar
FriehoffT
 
Posts: 56
Joined: Wed Sep 11, 2019 7:30 am

Re: BLE, disconnecting from a central

Post by FriehoffT »

danhalbert wrote:There isn't much information available through the API. Internally, there is a handle number for the connection, but that's used by the nRF SoftDevice. Are you trying to find out the central's address or something like that?
I am trying to find out which iPhone / iPad / Computer connected to my keyboard in order to disconnect the currently not desired ones.

Basically I (or the user of the Keyboard) want's to decide to which central the keys should be send at the current time.

Thomas

User avatar
danhalbert
 
Posts: 4654
Joined: Tue Aug 08, 2017 12:37 pm

Re: BLE, disconnecting from a central

Post by danhalbert »

I think you mentioned there was an existing project or product that does this? How does it distinguish?

User avatar
FriehoffT
 
Posts: 56
Joined: Wed Sep 11, 2019 7:30 am

Re: BLE, disconnecting from a central

Post by FriehoffT »

Sure,
I build -with others- a Braille Keyboard for blind persons. This Braille keyboard is "simulating" a standard query keyboard that can (and does successfully) connect to different hosts like iPhones, iPads, Mac*s and PC's.
Users of this keyboard do not want to buy / use one of these Braille keyboards for each of these hosts or (looking at it from the BLE perspective) centrals.
And they certainly do not want to change the Bluetooth setting on their computers when disconnecting from one host and connecting to the other.
As you may remember I am asking this (or similar) questions for some time....
I could argue that this desired feature is "technical" impossible with BLE....
But there are plenty of Bluetooth Querty Keyboards on the market which can do exactly what I am asking here.
Best regards
Thomas

User avatar
danhalbert
 
Posts: 4654
Joined: Tue Aug 08, 2017 12:37 pm

Re: BLE, disconnecting from a central

Post by danhalbert »

I remember that, and I am interested in how those existing commercial keyboards identify the multiple hosts. In order to determine how they actually work might require some BLE sniffing, but knowing what their UI is might at least give us some clues. If you have some pointers to their instruction manuals or similar, that would be helpful, I think.

User avatar
FriehoffT
 
Posts: 56
Joined: Wed Sep 11, 2019 7:30 am

Re: BLE, disconnecting from a central

Post by FriehoffT »

I will send you some pictures of BT keyboards I have.
Any yes, it may be that they are using BT3 with SSP (Secure Simple Pairing)
But I do not have good tools to verify this.
Thomas

User avatar
FriehoffT
 
Posts: 56
Joined: Wed Sep 11, 2019 7:30 am

Re: BLE, disconnecting from a central

Post by FriehoffT »

Thinking more about the pairing process of these mentioned keyboards, I think I remember a difference between these and the ItsyBitsy.
After touching the new "standard Qwerty" keyboard in Bluetooth Settings of the IOS device pairing happens immediately without another dialog shown by IOS.
After touching the new keyboard presented / simulated by the ItsyBitsy, IOS always comes up with a dialog like: "Do you want to pair? Cancel / Couple"
Last edited by FriehoffT on Wed Jan 12, 2022 6:09 pm, edited 1 time in total.

User avatar
FriehoffT
 
Posts: 56
Joined: Wed Sep 11, 2019 7:30 am

Re: BLE, disconnecting from a central

Post by FriehoffT »

Hmm... Maybe I can set up the ItsyBitsy Keyboard with some different MAC addresses and Names (switchable at run time) so the hosts are "thinking" the pairing takes place with different physical devices....

Can I change the MAC address of the nRF52840 chip at runtime from CP?

User avatar
danhalbert
 
Posts: 4654
Joined: Tue Aug 08, 2017 12:37 pm

Re: BLE, disconnecting from a central

Post by danhalbert »

Yes, BLE (as opposed to classic Bluetooth) keyboards are pretty rare, so you may be seeing a different mechanism.

User avatar
danhalbert
 
Posts: 4654
Joined: Tue Aug 08, 2017 12:37 pm

Re: BLE, disconnecting from a central

Post by danhalbert »

Can I change the MAC address of the nRF52840 chip at runtime from CP?
You cannot set the address, you can only fetch it: BLERadio.address_bytes.
You can change the name, but the host (especially iOS) often uses a cached version of the name. On Android forgetting the device will forget the old name, but iOS can be much more sticky and continue to use an old name even if you forget the device.

You might want to erase all the bonding info on the CircuitPython board. Don't do this on every run, but you can do it by hand to get to a known unpaired state:

Code: Select all

>>> import _bleio
>>> _bleio.adapter.erase_bonding()

User avatar
FriehoffT
 
Posts: 56
Joined: Wed Sep 11, 2019 7:30 am

Re: BLE, disconnecting from a central

Post by FriehoffT »

Yeah, but the user does not want to erase the bonding so that he needs to pair again when he want's to use this central again.
The user "only" wants to "temporarily" switch to another device.
Like a Network Administrator which is (was in the past;) switching his keyboard from one server to another.

User avatar
danhalbert
 
Posts: 4654
Joined: Tue Aug 08, 2017 12:37 pm

Re: BLE, disconnecting from a central

Post by danhalbert »

Sure - I was only describing how to erase the bonding in case things got confused. Definitely it should not be a regular thing. We saw issues moving from 6.x to 7.x, and erasing the bonding helped.

User avatar
FriehoffT
 
Posts: 56
Joined: Wed Sep 11, 2019 7:30 am

Re: BLE, disconnecting from a central

Post by FriehoffT »

So maybe a solution based on the central's address (even if it is often a random number that changes each time) would be the best thing we could offer.
At least for one session it should not change.
Or are clients required to maintain a connection even if the MAC Address of a central changes?
Is there a chance that you can get the MAC address of the central through the nRF SoftDevice API?

Other topic:
Will we run into the same / similar problems with the UART Service?
We are offering in parallel to the HID-Service a UART-Service in our Braille Keyboard and plan to come up with a IOS companion App to talk to the "keyboard".
Here things like current Keyboard-Layout or other setting can be communicated between the central and the client.
Will we be able to identify from which central the UART data is coming from? Or will "chunks" of data from a central at least be collected into one "packet"?
If UART data from different centrals will be mixed on a byte level this Service will not be helpful at all for us....

User avatar
danhalbert
 
Posts: 4654
Joined: Tue Aug 08, 2017 12:37 pm

Re: BLE, disconnecting from a central

Post by danhalbert »

The address changes for advertising purposes, but once a connection is made and maintained, you don't need to worry about re-establishing it. The address of the peer is given to us when the connection is established, but we don't do anything with it. It is not clear to me if this address can change or not. It is a public address, but I am not sure whether it changes. I have been studying material from https://devzone.nordicsemi.com/ and the API docs, and some other material. I am not an expert on all this at all, and it would be worth your studying some material on all this.

It might be that you need to rely on the IRK (not the address) to identify the central you are talking to.

Perhaps there are some examples associated with the Nordic SDK or the Arduino nRF library that show how to distinguish multiple centrals. We could add a feature to return the address or some other data from the connection. I am sorry I cannot be authoritative on this.

The UARTService requires no pairing (and is less secure because of that).

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”