diff --git a/app/Kconfig b/app/Kconfig index 108fcbe7096..b0ffc72ac02 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -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 diff --git a/app/include/zmk/ble.h b/app/include/zmk/ble.h index cc55a6ce142..c44c5e16869 100644 --- a/app/include/zmk/ble.h +++ b/app/include/zmk/ble.h @@ -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) */ diff --git a/app/src/ble.c b/app/src/ble.c index 9ecfb7ec394..e63c63b0cca 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -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; @@ -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 */ @@ -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) { diff --git a/app/tests/ble/profiles/bond-clear-then-bond-second-client/snapshot.log b/app/tests/ble/profiles/bond-clear-then-bond-second-client/snapshot.log index 077634f249d..c7a0884d4f7 100644 --- a/app/tests/ble/profiles/bond-clear-then-bond-second-client/snapshot.log +++ b/app/tests/ble/profiles/bond-clear-then-bond-second-client/snapshot.log @@ -2,7 +2,6 @@ profile 0 bt_id: No static addresses stored in controller profile 0 ble_central: main: [Bluetooth initialized] profile 0 ble_central: start_scan: [Scanning successfully started] profile 0 ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59 -profile 0 ble_central: eir_found: [AD]: 9 data_len 0 profile 0 ble_central: eir_found: [AD]: 25 data_len 2 profile 0 ble_central: eir_found: [AD]: 1 data_len 1 profile 0 ble_central: eir_found: [AD]: 2 data_len 4 @@ -13,7 +12,6 @@ profile 1 bt_id: No static addresses stored in controller profile 1 ble_central: main: [Bluetooth initialized] profile 1 ble_central: start_scan: [Scanning successfully started] profile 1 ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59 -profile 1 ble_central: eir_found: [AD]: 9 data_len 0 profile 1 ble_central: eir_found: [AD]: 25 data_len 2 profile 1 ble_central: eir_found: [AD]: 1 data_len 1 profile 1 ble_central: eir_found: [AD]: 2 data_len 4 diff --git a/app/tests/ble/profiles/bond-to-cleared-profile/snapshot.log b/app/tests/ble/profiles/bond-to-cleared-profile/snapshot.log index 077634f249d..c7a0884d4f7 100644 --- a/app/tests/ble/profiles/bond-to-cleared-profile/snapshot.log +++ b/app/tests/ble/profiles/bond-to-cleared-profile/snapshot.log @@ -2,7 +2,6 @@ profile 0 bt_id: No static addresses stored in controller profile 0 ble_central: main: [Bluetooth initialized] profile 0 ble_central: start_scan: [Scanning successfully started] profile 0 ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59 -profile 0 ble_central: eir_found: [AD]: 9 data_len 0 profile 0 ble_central: eir_found: [AD]: 25 data_len 2 profile 0 ble_central: eir_found: [AD]: 1 data_len 1 profile 0 ble_central: eir_found: [AD]: 2 data_len 4 @@ -13,7 +12,6 @@ profile 1 bt_id: No static addresses stored in controller profile 1 ble_central: main: [Bluetooth initialized] profile 1 ble_central: start_scan: [Scanning successfully started] profile 1 ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59 -profile 1 ble_central: eir_found: [AD]: 9 data_len 0 profile 1 ble_central: eir_found: [AD]: 25 data_len 2 profile 1 ble_central: eir_found: [AD]: 1 data_len 1 profile 1 ble_central: eir_found: [AD]: 2 data_len 4 diff --git a/app/tests/ble/profiles/connnect-and-output-to-selection/snapshot.log b/app/tests/ble/profiles/connnect-and-output-to-selection/snapshot.log index 62cb2d6d92f..61f3e09e1c2 100644 --- a/app/tests/ble/profiles/connnect-and-output-to-selection/snapshot.log +++ b/app/tests/ble/profiles/connnect-and-output-to-selection/snapshot.log @@ -2,7 +2,6 @@ ble_central: main: [Bluetooth initialized] ble_central: start_scan: [Scanning successfully started] ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59 - ble_central: eir_found: [AD]: 9 data_len 0 ble_central: eir_found: [AD]: 25 data_len 2 ble_central: eir_found: [AD]: 1 data_len 1 ble_central: eir_found: [AD]: 2 data_len 4 diff --git a/app/tests/ble/profiles/dont-bond-to-taken-profile/snapshot.log b/app/tests/ble/profiles/dont-bond-to-taken-profile/snapshot.log index 6ea4fc721b9..d754b9c4807 100644 --- a/app/tests/ble/profiles/dont-bond-to-taken-profile/snapshot.log +++ b/app/tests/ble/profiles/dont-bond-to-taken-profile/snapshot.log @@ -2,7 +2,6 @@ profile 0 bt_id: No static addresses stored in controller profile 0 ble_central: main: [Bluetooth initialized] profile 0 ble_central: start_scan: [Scanning successfully started] profile 0 ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59 -profile 0 ble_central: eir_found: [AD]: 9 data_len 0 profile 0 ble_central: eir_found: [AD]: 25 data_len 2 profile 0 ble_central: eir_found: [AD]: 1 data_len 1 profile 0 ble_central: eir_found: [AD]: 2 data_len 4 @@ -13,7 +12,6 @@ profile 1 bt_id: No static addresses stored in controller profile 1 ble_central: main: [Bluetooth initialized] profile 1 ble_central: start_scan: [Scanning successfully started] profile 1 ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59 -profile 1 ble_central: eir_found: [AD]: 9 data_len 0 profile 1 ble_central: eir_found: [AD]: 25 data_len 2 profile 1 ble_central: eir_found: [AD]: 1 data_len 1 profile 1 ble_central: eir_found: [AD]: 2 data_len 4 diff --git a/app/tests/ble/profiles/first-and-second-profile-paired-then-send-data/snapshot.log b/app/tests/ble/profiles/first-and-second-profile-paired-then-send-data/snapshot.log index 1a88748d1df..bc345b0bfb6 100644 --- a/app/tests/ble/profiles/first-and-second-profile-paired-then-send-data/snapshot.log +++ b/app/tests/ble/profiles/first-and-second-profile-paired-then-send-data/snapshot.log @@ -2,7 +2,6 @@ profile 0 bt_id: No static addresses stored in controller profile 0 ble_central: main: [Bluetooth initialized] profile 0 ble_central: start_scan: [Scanning successfully started] profile 0 ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59 -profile 0 ble_central: eir_found: [AD]: 9 data_len 0 profile 0 ble_central: eir_found: [AD]: 25 data_len 2 profile 0 ble_central: eir_found: [AD]: 1 data_len 1 profile 0 ble_central: eir_found: [AD]: 2 data_len 4 @@ -22,7 +21,6 @@ profile 0 00 00 04 00 00 00 00 00 |. profile 0 ble_central: notify_func: payload profile 0 00 00 00 00 00 00 00 00 |........ profile 1 ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59 -profile 1 ble_central: eir_found: [AD]: 9 data_len 0 profile 1 ble_central: eir_found: [AD]: 25 data_len 2 profile 1 ble_central: eir_found: [AD]: 1 data_len 1 profile 1 ble_central: eir_found: [AD]: 2 data_len 4 diff --git a/app/tests/ble/profiles/overwrite-enabled-reconnect-without-bond-then-output-to-selection/snapshot.log b/app/tests/ble/profiles/overwrite-enabled-reconnect-without-bond-then-output-to-selection/snapshot.log index 6c0bac58898..e54d7092395 100644 --- a/app/tests/ble/profiles/overwrite-enabled-reconnect-without-bond-then-output-to-selection/snapshot.log +++ b/app/tests/ble/profiles/overwrite-enabled-reconnect-without-bond-then-output-to-selection/snapshot.log @@ -2,7 +2,6 @@ ble_central: main: [Bluetooth initialized] ble_central: start_scan: [Scanning successfully started] ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59 - ble_central: eir_found: [AD]: 9 data_len 0 ble_central: eir_found: [AD]: 25 data_len 2 ble_central: eir_found: [AD]: 1 data_len 1 ble_central: eir_found: [AD]: 2 data_len 4 @@ -12,7 +11,6 @@ ble_central: disconnected: [Disconnected]: FD:9E:B2:48:47:39 (random) (reason 0x16) ble_central: start_scan: [Scanning successfully started] ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59 - ble_central: eir_found: [AD]: 9 data_len 0 ble_central: eir_found: [AD]: 25 data_len 2 ble_central: eir_found: [AD]: 1 data_len 1 ble_central: eir_found: [AD]: 2 data_len 4 diff --git a/app/tests/ble/profiles/reconnect-then-output-to-selection/snapshot.log b/app/tests/ble/profiles/reconnect-then-output-to-selection/snapshot.log index 2323de6faae..c6f2d914508 100644 --- a/app/tests/ble/profiles/reconnect-then-output-to-selection/snapshot.log +++ b/app/tests/ble/profiles/reconnect-then-output-to-selection/snapshot.log @@ -2,7 +2,6 @@ ble_central: main: [Bluetooth initialized] ble_central: start_scan: [Scanning successfully started] ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59 - ble_central: eir_found: [AD]: 9 data_len 0 ble_central: eir_found: [AD]: 25 data_len 2 ble_central: eir_found: [AD]: 1 data_len 1 ble_central: eir_found: [AD]: 2 data_len 4 @@ -12,7 +11,6 @@ ble_central: disconnected: [Disconnected]: FD:9E:B2:48:47:39 (random) (reason 0x16) ble_central: start_scan: [Scanning successfully started] ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59 - ble_central: eir_found: [AD]: 9 data_len 0 ble_central: eir_found: [AD]: 25 data_len 2 ble_central: eir_found: [AD]: 1 data_len 1 ble_central: eir_found: [AD]: 2 data_len 4 diff --git a/app/tests/ble/security/read-hid-after-connect-with-auto-sec/snapshot.log b/app/tests/ble/security/read-hid-after-connect-with-auto-sec/snapshot.log index b8ee29eb257..fa40c20a67b 100644 --- a/app/tests/ble/security/read-hid-after-connect-with-auto-sec/snapshot.log +++ b/app/tests/ble/security/read-hid-after-connect-with-auto-sec/snapshot.log @@ -2,7 +2,6 @@ ble_central: main: [Bluetooth initialized] ble_central: start_scan: [Scanning successfully started] ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59 - ble_central: eir_found: [AD]: 9 data_len 0 ble_central: eir_found: [AD]: 25 data_len 2 ble_central: eir_found: [AD]: 1 data_len 1 ble_central: eir_found: [AD]: 2 data_len 4 diff --git a/app/tests/ble/security/read-hid-after-connect-without-auto-sec/snapshot.log b/app/tests/ble/security/read-hid-after-connect-without-auto-sec/snapshot.log index 2157fd035d6..e32f76afb7c 100644 --- a/app/tests/ble/security/read-hid-after-connect-without-auto-sec/snapshot.log +++ b/app/tests/ble/security/read-hid-after-connect-without-auto-sec/snapshot.log @@ -2,7 +2,6 @@ ble_central: main: [Bluetooth initialized] ble_central: start_scan: [Scanning successfully started] ble_central: device_found: [DEVICE]: FD:9E:B2:48:47:39 (random), AD evt type 0, AD data len 15, RSSI -59 - ble_central: eir_found: [AD]: 9 data_len 0 ble_central: eir_found: [AD]: 25 data_len 2 ble_central: eir_found: [AD]: 1 data_len 1 ble_central: eir_found: [AD]: 2 data_len 4