Skip to content

Commit

Permalink
feat(ble): Set device name at runtime
Browse files Browse the repository at this point in the history
This allows for the device name to be set at runtime, possible uses for this include a custom display screen that lets you type it in using the keycode state event, future configuration in zmk studio, or altering it per profile as per #1169
  • Loading branch information
ReFil authored Sep 25, 2024
1 parent 58dcf5c commit 17f0a4d
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 19 deletions.
1 change: 1 addition & 0 deletions app/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ menuconfig ZMK_BLE
select BT_SMP_APP_PAIRING_ACCEPT
select BT_PERIPHERAL
select BT_DIS
imply BT_DEVICE_NAME_DYNAMIC
imply BT_SETTINGS if !ARCH_POSIX
imply SETTINGS if !ARCH_POSIX
imply ZMK_BATTERY_REPORTING if !ARCH_POSIX
Expand Down
2 changes: 2 additions & 0 deletions app/include/zmk/ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ char *zmk_ble_active_profile_name(void);

int zmk_ble_unpair_all(void);

int zmk_ble_set_device_name(char *name);

#if IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
int zmk_ble_put_peripheral_addr(const bt_addr_le_t *addr);
#endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) */
28 changes: 24 additions & 4 deletions app/src/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ enum advertising_type {
#define CURR_ADV(adv) (adv << 4)

#define ZMK_ADV_CONN_NAME \
BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_ONE_TIME, BT_GAP_ADV_FAST_INT_MIN_2, \
BT_GAP_ADV_FAST_INT_MAX_2, NULL)
BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_ONE_TIME | BT_LE_ADV_OPT_USE_NAME | \
BT_LE_ADV_OPT_FORCE_NAME_IN_AD, \
BT_GAP_ADV_FAST_INT_MIN_2, BT_GAP_ADV_FAST_INT_MAX_2, NULL)

static struct zmk_ble_profile profiles[ZMK_BLE_PROFILE_COUNT];
static uint8_t active_profile;
Expand All @@ -67,8 +68,7 @@ static uint8_t active_profile;

BUILD_ASSERT(DEVICE_NAME_LEN <= 16, "ERROR: BLE device name is too long. Max length: 16");

static const struct bt_data zmk_ble_ad[] = {
BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
static struct bt_data zmk_ble_ad[] = {
BT_DATA_BYTES(BT_DATA_GAP_APPEARANCE, 0xC1, 0x03),
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
BT_DATA_BYTES(BT_DATA_UUID16_SOME, 0x12, 0x18, /* HID Service */
Expand Down Expand Up @@ -335,6 +335,26 @@ struct bt_conn *zmk_ble_active_profile_conn(void) {

char *zmk_ble_active_profile_name(void) { return profiles[active_profile].name; }

int zmk_ble_set_device_name(char *name) {
// Copy new name to advertising parameters
int err = bt_set_name(name);
LOG_DBG("New device name: %s", name);
if (err) {
LOG_ERR("Failed to set new device name (err %d)", err);
return err;
}
if (advertising_status == ZMK_ADV_CONN) {
// Stop current advertising so it can restart with new name
err = bt_le_adv_stop();
advertising_status = ZMK_ADV_NONE;
if (err) {
LOG_ERR("Failed to stop advertising (err %d)", err);
return err;
}
}
return update_advertising();
}

#if IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL)

int zmk_ble_put_peripheral_addr(const bt_addr_le_t *addr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ profile 0 <wrn> bt_id: No static addresses stored in controller
profile 0 <dbg> ble_central: main: [Bluetooth initialized]
profile 0 <dbg> ble_central: start_scan: [Scanning successfully started]
profile 0 <dbg> ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59
profile 0 <dbg> ble_central: eir_found: [AD]: 9 data_len 0
profile 0 <dbg> ble_central: eir_found: [AD]: 25 data_len 2
profile 0 <dbg> ble_central: eir_found: [AD]: 1 data_len 1
profile 0 <dbg> ble_central: eir_found: [AD]: 2 data_len 4
Expand All @@ -13,7 +12,6 @@ profile 1 <wrn> bt_id: No static addresses stored in controller
profile 1 <dbg> ble_central: main: [Bluetooth initialized]
profile 1 <dbg> ble_central: start_scan: [Scanning successfully started]
profile 1 <dbg> ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59
profile 1 <dbg> ble_central: eir_found: [AD]: 9 data_len 0
profile 1 <dbg> ble_central: eir_found: [AD]: 25 data_len 2
profile 1 <dbg> ble_central: eir_found: [AD]: 1 data_len 1
profile 1 <dbg> ble_central: eir_found: [AD]: 2 data_len 4
Expand Down
2 changes: 0 additions & 2 deletions app/tests/ble/profiles/bond-to-cleared-profile/snapshot.log
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ profile 0 <wrn> bt_id: No static addresses stored in controller
profile 0 <dbg> ble_central: main: [Bluetooth initialized]
profile 0 <dbg> ble_central: start_scan: [Scanning successfully started]
profile 0 <dbg> ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59
profile 0 <dbg> ble_central: eir_found: [AD]: 9 data_len 0
profile 0 <dbg> ble_central: eir_found: [AD]: 25 data_len 2
profile 0 <dbg> ble_central: eir_found: [AD]: 1 data_len 1
profile 0 <dbg> ble_central: eir_found: [AD]: 2 data_len 4
Expand All @@ -13,7 +12,6 @@ profile 1 <wrn> bt_id: No static addresses stored in controller
profile 1 <dbg> ble_central: main: [Bluetooth initialized]
profile 1 <dbg> ble_central: start_scan: [Scanning successfully started]
profile 1 <dbg> ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59
profile 1 <dbg> ble_central: eir_found: [AD]: 9 data_len 0
profile 1 <dbg> ble_central: eir_found: [AD]: 25 data_len 2
profile 1 <dbg> ble_central: eir_found: [AD]: 1 data_len 1
profile 1 <dbg> ble_central: eir_found: [AD]: 2 data_len 4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<dbg> ble_central: main: [Bluetooth initialized]
<dbg> ble_central: start_scan: [Scanning successfully started]
<dbg> ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59
<dbg> ble_central: eir_found: [AD]: 9 data_len 0
<dbg> ble_central: eir_found: [AD]: 25 data_len 2
<dbg> ble_central: eir_found: [AD]: 1 data_len 1
<dbg> ble_central: eir_found: [AD]: 2 data_len 4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ profile 0 <wrn> bt_id: No static addresses stored in controller
profile 0 <dbg> ble_central: main: [Bluetooth initialized]
profile 0 <dbg> ble_central: start_scan: [Scanning successfully started]
profile 0 <dbg> ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59
profile 0 <dbg> ble_central: eir_found: [AD]: 9 data_len 0
profile 0 <dbg> ble_central: eir_found: [AD]: 25 data_len 2
profile 0 <dbg> ble_central: eir_found: [AD]: 1 data_len 1
profile 0 <dbg> ble_central: eir_found: [AD]: 2 data_len 4
Expand All @@ -13,7 +12,6 @@ profile 1 <wrn> bt_id: No static addresses stored in controller
profile 1 <dbg> ble_central: main: [Bluetooth initialized]
profile 1 <dbg> ble_central: start_scan: [Scanning successfully started]
profile 1 <dbg> ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59
profile 1 <dbg> ble_central: eir_found: [AD]: 9 data_len 0
profile 1 <dbg> ble_central: eir_found: [AD]: 25 data_len 2
profile 1 <dbg> ble_central: eir_found: [AD]: 1 data_len 1
profile 1 <dbg> ble_central: eir_found: [AD]: 2 data_len 4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ profile 0 <wrn> bt_id: No static addresses stored in controller
profile 0 <dbg> ble_central: main: [Bluetooth initialized]
profile 0 <dbg> ble_central: start_scan: [Scanning successfully started]
profile 0 <dbg> ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59
profile 0 <dbg> ble_central: eir_found: [AD]: 9 data_len 0
profile 0 <dbg> ble_central: eir_found: [AD]: 25 data_len 2
profile 0 <dbg> ble_central: eir_found: [AD]: 1 data_len 1
profile 0 <dbg> ble_central: eir_found: [AD]: 2 data_len 4
Expand All @@ -22,7 +21,6 @@ profile 0 00 00 04 00 00 00 00 00 |.
profile 0 <dbg> ble_central: notify_func: payload
profile 0 00 00 00 00 00 00 00 00 |........
profile 1 <dbg> ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59
profile 1 <dbg> ble_central: eir_found: [AD]: 9 data_len 0
profile 1 <dbg> ble_central: eir_found: [AD]: 25 data_len 2
profile 1 <dbg> ble_central: eir_found: [AD]: 1 data_len 1
profile 1 <dbg> ble_central: eir_found: [AD]: 2 data_len 4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<dbg> ble_central: main: [Bluetooth initialized]
<dbg> ble_central: start_scan: [Scanning successfully started]
<dbg> ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59
<dbg> ble_central: eir_found: [AD]: 9 data_len 0
<dbg> ble_central: eir_found: [AD]: 25 data_len 2
<dbg> ble_central: eir_found: [AD]: 1 data_len 1
<dbg> ble_central: eir_found: [AD]: 2 data_len 4
Expand All @@ -12,7 +11,6 @@
<dbg> ble_central: disconnected: [Disconnected]: FD:9E:B2:48:47:39 (random) (reason 0x16)
<dbg> ble_central: start_scan: [Scanning successfully started]
<dbg> ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59
<dbg> ble_central: eir_found: [AD]: 9 data_len 0
<dbg> ble_central: eir_found: [AD]: 25 data_len 2
<dbg> ble_central: eir_found: [AD]: 1 data_len 1
<dbg> ble_central: eir_found: [AD]: 2 data_len 4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<dbg> ble_central: main: [Bluetooth initialized]
<dbg> ble_central: start_scan: [Scanning successfully started]
<dbg> ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59
<dbg> ble_central: eir_found: [AD]: 9 data_len 0
<dbg> ble_central: eir_found: [AD]: 25 data_len 2
<dbg> ble_central: eir_found: [AD]: 1 data_len 1
<dbg> ble_central: eir_found: [AD]: 2 data_len 4
Expand All @@ -12,7 +11,6 @@
<dbg> ble_central: disconnected: [Disconnected]: FD:9E:B2:48:47:39 (random) (reason 0x16)
<dbg> ble_central: start_scan: [Scanning successfully started]
<dbg> ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59
<dbg> ble_central: eir_found: [AD]: 9 data_len 0
<dbg> ble_central: eir_found: [AD]: 25 data_len 2
<dbg> ble_central: eir_found: [AD]: 1 data_len 1
<dbg> ble_central: eir_found: [AD]: 2 data_len 4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<dbg> ble_central: main: [Bluetooth initialized]
<dbg> ble_central: start_scan: [Scanning successfully started]
<dbg> ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59
<dbg> ble_central: eir_found: [AD]: 9 data_len 0
<dbg> ble_central: eir_found: [AD]: 25 data_len 2
<dbg> ble_central: eir_found: [AD]: 1 data_len 1
<dbg> ble_central: eir_found: [AD]: 2 data_len 4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<dbg> ble_central: main: [Bluetooth initialized]
<dbg> ble_central: start_scan: [Scanning successfully started]
<dbg> ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59
<dbg> ble_central: eir_found: [AD]: 9 data_len 0
<dbg> ble_central: eir_found: [AD]: 25 data_len 2
<dbg> ble_central: eir_found: [AD]: 1 data_len 1
<dbg> ble_central: eir_found: [AD]: 2 data_len 4
Expand Down

0 comments on commit 17f0a4d

Please sign in to comment.