Implemented bluetooth disable key feature

This commit is contained in:
elagil 2021-01-18 19:46:28 +01:00
parent 5978990e6d
commit 7e65e4c5c4
5 changed files with 29 additions and 0 deletions

View file

@ -9,6 +9,7 @@
#define BT_PRV_CMD 2
#define BT_SEL_CMD 3
// #define BT_FULL_RESET_CMD 4
#define BT_DIS_CMD 5
/*
Note: Some future commands will include additional parameters, so we
@ -18,4 +19,5 @@ defines these aliases up front.
#define BT_CLR BT_CLR_CMD 0
#define BT_NXT BT_NXT_CMD 0
#define BT_PRV BT_PRV_CMD 0
#define BT_DIS BT_DIS_CMD 0
#define BT_SEL BT_SEL_CMD

View file

@ -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_disconnect();
int zmk_ble_active_profile_index();
bt_addr_le_t *zmk_ble_active_profile_addr();

View file

@ -28,6 +28,8 @@ static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
return zmk_ble_prof_prev();
case BT_SEL_CMD:
return zmk_ble_prof_select(binding->param2);
case BT_DIS_CMD:
return zmk_ble_disconnect();
default:
LOG_ERR("Unknown BT command: %d", binding->param1);
}

View file

@ -594,6 +594,23 @@ static int zmk_ble_init(const struct device *_arg) {
return 0;
}
int zmk_ble_disconnect() {
LOG_DBG("");
for (uint8_t profile_index = 0; profile_index < PROFILE_COUNT; profile_index++) {
struct zmk_ble_profile *profile = &profiles[profile_index];
if (bt_addr_le_cmp(&(profile->peer), BT_ADDR_LE_ANY)) {
LOG_DBG("Disconnecting profile %d!", profile_index);
struct bt_conn *conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &(profile->peer));
bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
}
}
return 0;
}
int zmk_ble_unpair_all() {
int resp = 0;
for (int i = BT_ID_DEFAULT; i < CONFIG_BT_ID_MAX; i++) {

View file

@ -32,6 +32,7 @@ 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_DIS` | Disconnects all bluetooth profiles. This command disables bluetooth advertising until a board reset is issued. |
## Bluetooth Behavior
@ -69,6 +70,12 @@ The bluetooth behavior completes an bluetooth action given on press.
&bt BT_SEL 1
```
1. Behavior binding to disconnect bluetooth:
```
&bt BT_DIS
```
## Bluetooth Pairing and Profiles
ZMK support bluetooth “profiles” which allows connection to multiple devices (5 by default, or 4 if you are using split keyboards). 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: