zmk icon indicating copy to clipboard operation
zmk copied to clipboard

feat(ble): Set device name at runtime

Open ReFil opened this issue 1 year ago • 8 comments

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

Testing shows restarting advertising was necessary to update the new name, although some host OS's don't reflect the name change properly even with this in mind, also of note is some hosts don't update the name after pairing too

ReFil avatar Jun 17 '24 09:06 ReFil

Should this change the USB device name as well?

caksoylar avatar Jun 17 '24 18:06 caksoylar

I can probably make a function to do it for USB, I don't think one function should do both as there's no logical place for that one function to live, whatever system the developer is using at the other end should call both to change both. I think there's also a good argument for keeping them separate as there are good reasons to want to change them independently e.g. per profile naming

ReFil avatar Jun 17 '24 18:06 ReFil

Ah neat I didn't know that. I did find in my testing that results from this were inconsistent. iOS and Linux reflected the name change whilst scanning, android didn't. That should probably sort this. I've also got it saving and restoring from flash, and will incorporate that too

ReFil avatar Jun 17 '24 20:06 ReFil

Ah neat I didn't know that. I did find in my testing that results from this were inconsistent. iOS and Linux reflected the name change whilst scanning, android didn't. That should probably sort this. I've also got it saving and restoring from flash, and will incorporate that too

Yeah, bt_set_name implementation will store to settings if it's enabled. See hci_core.c:bt_set_name

petejohanson avatar Jun 17 '24 20:06 petejohanson

I've got it working now using CONFIG_BT_DEVICE_NAME_DYNAMIC. If i'm reading the PR associated with this function properly i think we get up to 27 chars for device name now too which is a neat bonus. The saving to flash can't be debounced, because it's outside of our control but i cant imagine that as a major problem. The advertising packet had to be changed, as ZMK used the advertising packet to report the name previously. I suppose the final question is do we want the name to be writable by the host device? There's no downsides to it i guess

ReFil avatar Jun 18 '24 09:06 ReFil

Just checked and CONFIG_BT_DEVICE_NAME_GATT_WRITABLE is enabled even when only enabling CONFIG_BT_DEVICE_NAME_DYNAMIC. Name can sucessfully be read and written using nRF connect. all seems to be working properly

ReFil avatar Jun 18 '24 16:06 ReFil

@ReFil Check the BLE test error.. maybe advertising data changes?

petejohanson avatar Jun 21 '24 19:06 petejohanson

Yes I think that's the issue, name is now at the end of the ad packet not the beginning. Will sort that when I have a mo

ReFil avatar Jun 21 '24 20:06 ReFil

Yes I think that's the issue, name is now at the end of the ad packet not the beginning. Will sort that when I have a mo

@ReFil Nudge.

petejohanson avatar Sep 06 '24 19:09 petejohanson

@petejohanson Sorry for the delay in addressing it. I've tweaked the tests to remove the offending line from the logs and they all pass now

ReFil avatar Sep 09 '24 08:09 ReFil