diff --git a/app/Kconfig b/app/Kconfig index a5fa54f6..9f593701 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -185,6 +185,10 @@ config BT_CTLR_PHY_2M config BT_TINYCRYPT_ECC default y if BT_HCI && !BT_CTLR +config ZMK_BLE_FAST_SWITCHING + bool "Fast switching between bluetooth profiles by maintaining connection" + default y + config SYSTEM_WORKQUEUE_STACK_SIZE default 4096 if SOC_RP2040 default 2048 diff --git a/app/src/ble.c b/app/src/ble.c index 3efedcf4..de3190ba 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -261,7 +261,10 @@ int zmk_ble_prof_select(uint8_t index) { return 0; } +#if !IS_ENABLED(CONFIG_ZMK_BLE_FAST_SWITCHING) zmk_ble_prof_disconnect(active_profile); +#endif + active_profile = index; ble_save_profile(); @@ -454,12 +457,25 @@ static void connected(struct bt_conn *conn, uint8_t err) { } #endif // !IS_ENABLED(CONFIG_BT_GATT_AUTO_SEC_REQ) - update_advertising(); - if (is_conn_active_profile(conn)) { LOG_DBG("Active profile connected"); k_work_submit(&raise_profile_changed_event_work); + +#if !IS_ENABLED(CONFIG_ZMK_BLE_FAST_SWITCHING) + // Due to a bug with directed advertising, we had to use open + // advertising and probably connected to multiple profiles. + // Now that we have the desired active connection, we disconnect + // everything else. + for (int i = 0; i < ZMK_BLE_PROFILE_COUNT; i++) { + if (i != active_profile && !bt_addr_le_cmp(&profiles[i].peer, BT_ADDR_LE_ANY)) { + zmk_ble_prof_disconnect(i); + } + } +#endif } + + update_advertising(); + } static void disconnected(struct bt_conn *conn, uint8_t reason) { diff --git a/docs/docs/config/bluetooth.md b/docs/docs/config/bluetooth.md index 9149b36b..1e666010 100644 --- a/docs/docs/config/bluetooth.md +++ b/docs/docs/config/bluetooth.md @@ -16,3 +16,4 @@ See [Configuration Overview](index.md) for instructions on how to change these s | `CONFIG_ZMK_BLE_EXPERIMENTAL_FEATURES` | bool | Aggregate config that enables both `CONFIG_ZMK_BLE_EXPERIMENTAL_CONN` and `CONFIG_ZMK_BLE_EXPERIMENTAL_SEC`. | n | | `CONFIG_ZMK_BLE_PASSKEY_ENTRY` | bool | Enable passkey entry during pairing for enhanced security. (Note: After enabling this, you will need to re-pair all previously paired hosts.) | n | | `CONFIG_BT_GATT_ENFORCE_SUBSCRIPTION` | bool | Low level setting for GATT subscriptions. Set to `n` to work around an annoying Windows bug with battery notifications. | y | +| `CONFIG_ZMK_BLE_FAST_SWITCHING` | bool | Enables faster switching between profiles by maintaining the bluetooth connection even after switching to a different profile. Disabling this will disconnect (but remain paired) from the old profile when selecting a new one. | y |