Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ble): add support for generating device name based on MAC address #2785

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,15 @@ config BT_PERIPHERAL_PREF_LATENCY
config BT_PERIPHERAL_PREF_TIMEOUT
default 400

# The device name should be 16 characters or less so it fits within the
# advertising data.
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
22 changes: 21 additions & 1 deletion app/src/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ static uint8_t active_profile;
#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)

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

static struct bt_data zmk_ble_ad[] = {
BT_DATA_BYTES(BT_DATA_GAP_APPEARANCE, 0xC1, 0x03),
Expand Down Expand Up @@ -700,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
Loading