Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ble): add behavior to disconnect from BLE profile #1638

Merged
merged 5 commits into from
Nov 20, 2023

Conversation

chrisandreae
Copy link
Contributor

@chrisandreae chrisandreae commented Jan 28, 2023

Adds new functionality and a behavior to disconnect an active BLE connection. The motivation for this is that for some devices like phones, the presence of an active BLE connection results in the onscreen keyboard being selected.

This behavior allows the active connection for a given profile to be explicitly disconnected, by calling bt_conn_disconnect(). The connection will be re-established (via update_advertising()), when that profile is re-selected.

I believe this behavior is complementary to the proposed automatic disconnection of non-selected devices. Automatic disconnection without a timeout could be frustrating to use when actively switching between devices, as there's a slight but noticeable delay on re-establishing the connnection. On the other hand if a timeout is used, there might be times when the user wishes to disconnect before the timeout has elapsed. This behaviour would permit that.

Questions:

In the case that OUT_USB is selected, there is still a BLE profile which is considered active and selected, and is being advertised for. If the user tries to disconnect from that profile, it will be immediately re-connected from the advertisement. This would be awkward in the case that the user was wanting to switch between their computer via USB and their phone via BLE. I'm wondering if it would be most consistent to stop advertising for the selected BLE profile in the case that OUT_USB is selected and the USB connection is present and active. This would make selecting the USB output more consistent with selecting a different BLE output.

@PabloLION
Copy link

I want to specify another motivation if I may:
I always put my mac studio into lock-screen mode since it's basically sleeping mode. with this BLE function showing which device is online, the mac wakes up every once in a while. This is especially annoying when I'm taking a nap in front of the screen. It seems this PR will fix the issue.

Copy link
Contributor

@petejohanson petejohanson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment on the code. This also needs to update the BT behavior docs page to document the new command. See https://zmk.dev/docs/behaviors/bluetooth#bluetooth-command-defines

app/src/ble.c Outdated Show resolved Hide resolved
app/src/ble.c Outdated Show resolved Hide resolved
Adds new functionality and a behaviour to disconnect an active BLE connection.
The motivation for this is that for some devices like phones, the presence of an
active BLE connection results in the onscreen keyboard being selected.
@chrisandreae chrisandreae force-pushed the bt-disconnect-behavior branch from fa28ab8 to 14767c0 Compare November 11, 2023 05:38
@chrisandreae chrisandreae requested a review from a team as a code owner November 11, 2023 05:38
| `BT_NXT` | Switch to the next profile, cycling through to the first one when the end is reached. |
| `BT_PRV` | Switch to the previous profile, cycling through to the last one when the beginning is reached. |
| `BT_SEL` | Select the 0-indexed profile by number. Please note: this definition must include a number as an argument in the keymap to work correctly. eg. `BT_SEL 0` |
| `BT_DISCONNECT` | Disconnect from the 0-indexed profile by number, if it's currently connected and inactive. Please note: this definition must include a number as an |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The length of this define kind of sticks out from the other ones. What do you think about abbreviating it, e.g. BT_DIS or BT_DSC?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm torn, because those abbreviations aren't especially transparent; you'd have to check the docs to interpret them. NXT, PRV, and SEL all don't have that problem, they're quite self-explanatory.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly think BT_DISC is descriptive enough, at least as descriptive as BT_SEL is, and as long as it's clearly documented, should be fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, BT_DISC works for me, I'll make the change


An inactive connected profile can be explicitly disconnected using the `BT_DISCONNECT` behavior. This can be helpful in
cases when host devices behave differently when a bluetooth keyboard is connected, for example by hiding their on-screen
keyboard.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might not have understood #1998 correctly, but does it imply that this shouldn't be used with the currently active profile when USB endpoint is active? If so, can we add a warning note on that here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, if the disconnected profile is the currently active one, it'll successfully disconnect but then immediately reconnect, because the currently active BT profile always advertises—even if the USB output is selected. #1998 is about a follow-up to make the pause in advertising when switching away from a BT profile consistent, whether that's switching to another BT profile or switching to USB.

I mentioned it to Pete, and he said he'd prefer to leave that to a follow-up because he has a BT refactor in the works that they'd interfere with, and to add an issue to keep track of it.

I don't have a strong opinion about whether the automatic re-connection should be documented explicitly here. If the advertising behavior is changed later, the documentation can always be updated to match.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrisandreae I would document that expected behavior here for now, and we can update later as needed. Omitting it may just lead to some confusion on how this works today for the active profile.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I've done that. Does this look OK?

Copy link
Contributor

@petejohanson petejohanson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good to me. Happy to merge once the docs addition is in.

Copy link
Contributor

@petejohanson petejohanson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@petejohanson petejohanson merged commit 0a4b1a6 into zmkfirmware:main Nov 20, 2023
43 checks passed
@chrisandreae chrisandreae deleted the bt-disconnect-behavior branch November 21, 2023 05:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants