Skip to content

Commit

Permalink
feat(ble): add support for generating device name based on MAC address
Browse files Browse the repository at this point in the history
This is useful for distinguishing multiple identical keyboards.
  • Loading branch information
xudongzheng committed Jan 21, 2025
1 parent feb38e2 commit a64d2c2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ config BT_PERIPHERAL_PREF_TIMEOUT
config BT_DEVICE_NAME_MAX
default 16

config ZMK_BLE_KEYBOARD_NAME_MAC
bool "Include MAC address in BLE device name"
depends on ZMK_SPLIT_ROLE_CENTRAL

endif # ZMK_BLE

endmenu # Output Types
Expand Down
18 changes: 18 additions & 0 deletions app/src/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,24 @@ static int zmk_ble_complete_startup(void) {

#endif // IS_ENABLED(CONFIG_ZMK_BLE_CLEAR_BONDS_ON_START)

#if IS_ENABLED(CONFIG_ZMK_BLE_KEYBOARD_NAME_MAC)
bt_addr_le_t addrs[CONFIG_BT_ID_MAX];
size_t id_count;
bt_id_get(addrs, &id_count);
if (id_count < 1) {
LOG_ERR("Failed to get Bluetooth device address");
} else {
// The generated name can be a maximum of 29 bytes (plus NULL) since
// CONFIG_BT_DEVICE_NAME is 16 bytes at most.
char name[30] = {};
uint8_t *a = addrs[0].a.val;
snprintf(name, sizeof(name), "%s %02X%02X%02X%02X%02X%02X", CONFIG_BT_DEVICE_NAME, a[5],
a[4], a[3], a[2], a[1], a[0]);
name[CONFIG_BT_DEVICE_NAME_MAX] = '\0';
zmk_ble_set_device_name(name);
}
#endif

bt_conn_cb_register(&conn_callbacks);
bt_conn_auth_cb_register(&zmk_ble_auth_cb_display);
bt_conn_auth_info_cb_register(&zmk_ble_auth_info_cb_display);
Expand Down

0 comments on commit a64d2c2

Please sign in to comment.