This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Setting Bluetooth Device Name in Zephyr

Using Zephyr, I'm looking to configure the BLE name of my devices to use the serial number they are provisioned with.  

To this end there is the API "bt_set_name" that seems like it should do it, but I have not yet found anyway to get it to actually change the name.  The Device always advertises with whatever CONFIG_BT_DEVICE_NAME is set to.

Has anyone successfully done this with Zephyr?

Thank you so much for your help!

  • You need to enable the config "Allow to set Bluetooth device name on runtime". 

    In SESne:

    (top toolbar) Project --> Configure nRF Connect SDK project --> menuconfig --> type "device name" in the filter box --> enable the 'Allow to set Bluetooth device name on runtime' configuration by toggling it's button and click 'configure' afterwards.


  • I did try this and it didn't help on its own.

    What DID help is realizing that the "Advertising Data" struct that gets passed to "bt_le_adv_start" is where the name is actually being configured.  I just modified the value and length in that struct before starting the advertisement and it all worked.


    I think there is a way to use "bt_set_name" but I still haven't gotten that way to work.  Too many flags.

  • You can set the device name in your code by calling "bt_set_name" function, after when Bluetooth has enabled but before when advertisement has started.

    Also it is necessary to enable dynamic name capability in the project configuration in addition to add BT_LE_ADV_OPT_USE_NAME to  bt_le_adv_param variable.

    Add these options to prfj.conf file:

    ....
    
    # Enable Dynamic name modification
    CONFIG_BT_DEVICE_NAME_DYNAMIC=y
    # Maximum chcaracter of the device name
    # (Range: 2 to 248)
    CONFIG_BT_DEVICE_NAME_MAX=30

    and call bt_set_name after when bt has enabled but advertisement has not started

    main.c

    void main(void)
    {
        struct bt_le_adv_param param;
        ...
        bt_enable(NULL);
        ...
        bt_set_name("new name");
        ...
        param.options = BT_LE_ADV_OPT_USE_NAME;
        ...
        bt_le_ext_adv_create(&param, NULL, &adv_set);
        bt_le_per_adv_start(adv_set);
        
    }

Related