diff --git a/app/include/dt-bindings/zmk/bt.h b/app/include/dt-bindings/zmk/bt.h index 8ca10606..7083c9b8 100644 --- a/app/include/dt-bindings/zmk/bt.h +++ b/app/include/dt-bindings/zmk/bt.h @@ -8,13 +8,14 @@ #define BT_NXT_CMD 1 #define BT_PRV_CMD 2 #define BT_SEL_CMD 3 -// #define BT_FULL_RESET_CMD 4 +#define BT_RST_CMD 4 /* Note: Some future commands will include additional parameters, so we defines these aliases up front. */ +#define BT_RST BT_RST_CMD 0 #define BT_CLR BT_CLR_CMD 0 #define BT_NXT BT_NXT_CMD 0 #define BT_PRV BT_PRV_CMD 0 diff --git a/app/include/zmk/ble.h b/app/include/zmk/ble.h index f6f6e191..89dcc72f 100644 --- a/app/include/zmk/ble.h +++ b/app/include/zmk/ble.h @@ -13,6 +13,7 @@ int zmk_ble_clear_bonds(); int zmk_ble_prof_next(); int zmk_ble_prof_prev(); int zmk_ble_prof_select(uint8_t index); +int zmk_ble_reset_clear_all_bonds(); int zmk_ble_active_profile_index(); bt_addr_le_t *zmk_ble_active_profile_addr(); diff --git a/app/src/behaviors/behavior_bt.c b/app/src/behaviors/behavior_bt.c index 9a171e0f..44e4908b 100644 --- a/app/src/behaviors/behavior_bt.c +++ b/app/src/behaviors/behavior_bt.c @@ -24,6 +24,8 @@ static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, switch (binding->param1) { case BT_CLR_CMD: return zmk_ble_clear_bonds(); + case BT_RST_CMD: + return zmk_ble_reset_clear_all_bonds(); case BT_NXT_CMD: return zmk_ble_prof_next(); case BT_PRV_CMD: diff --git a/app/src/ble.c b/app/src/ble.c index a9f2afe9..d4264283 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -548,6 +548,28 @@ static void zmk_ble_ready(int err) { update_advertising(); } +int zmk_ble_reset_clear_all_bonds() { + LOG_WRN("Clearing all existing BLE bond information from the keyboard"); + + for (int i = 0; i < 10; i++) { + bt_unpair(i, NULL); + } + int ret = 0; + for (int i = 0; i < PROFILE_COUNT; i++) { + char setting_name[15]; + sprintf(setting_name, "ble/profiles/%d", i); + + int err = settings_delete(setting_name); + if (err) { + LOG_ERR("Failed to delete setting: %d", err); + ret -= 1; + } + } + + update_advertising(); + return ret; +} + static int zmk_ble_init(const struct device *_arg) { int err = bt_enable(NULL); @@ -573,21 +595,7 @@ static int zmk_ble_init(const struct device *_arg) { #endif #if IS_ENABLED(CONFIG_ZMK_BLE_CLEAR_BONDS_ON_START) - LOG_WRN("Clearing all existing BLE bond information from the keyboard"); - - for (int i = 0; i < 10; i++) { - bt_unpair(i, NULL); - } - - for (int i = 0; i < PROFILE_COUNT; i++) { - char setting_name[15]; - sprintf(setting_name, "ble/profiles/%d", i); - - err = settings_delete(setting_name); - if (err) { - LOG_ERR("Failed to delete setting: %d", err); - } - } + zmk_ble_reset_clear_all_bonds(); #endif bt_conn_cb_register(&conn_callbacks); diff --git a/docs/docs/behaviors/bluetooth.md b/docs/docs/behaviors/bluetooth.md index df017b05..4c17a537 100644 --- a/docs/docs/behaviors/bluetooth.md +++ b/docs/docs/behaviors/bluetooth.md @@ -36,6 +36,8 @@ Here is a table describing the command for each define: | `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_RST` | Clear bond information between the keyboard and host for all profiles. | + ## Bluetooth Behavior @@ -73,6 +75,12 @@ The bluetooth behavior completes an bluetooth action given on press. &bt BT_SEL 1 ``` +1. Clear the bond information between the keyboard and host for all profiles: + + ``` + &bt BT_RST + ``` + ## Bluetooth Pairing and Profiles ZMK support bluetooth “profiles” which allows connection to multiple devices (5 by default). Each profile stores the bluetooth MAC address of a peer, which can be empty if a profile has not been paired with a device yet. Upon switching to a profile, ZMK does the following: