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

Modify BLE Advertising tutorial to change advertising data on button press

Hello, I've been tinkering around with the BLE Advertising tutorial on my nRF52840 Dongle (using SoftDevice 140) and I have a question:

How do I configure the dongle's user button to change the advertising data on press?

Currently, I've modified prepended some code to one of the functions in `main.c`

static void bsp_event_handler(bsp_event_t event)
{
    ret_code_t err_code;

    ble_gap_adv_data_t my_data;
    uint8_t data[]                            = "New!"; // New data to advertise
    my_data.adv_data.p_data = data;
    my_data.adv_data.len = sizeof(data);

    sd_ble_gap_adv_set_configure(&(m_advertising.adv_handle), &my_data, NULL); // Update existing advertising data
    
    // The rest of the original function is still here
}

I scan for the Dongle with nRF Connect on my phone--it shows up with the original advertising data and does not update to "New!" even when I press the button.

I'm new to the Nordic nRF family; is there something I'm missing?  Help would be greatly appreciated.

Thank you!

  • Hi,

    You need to encode the data to construct a valid advertisement packet. The ble_advdata_encode() function my be used for this. Try to see if the code below works for you.

    #define APP_COMPANY_IDENTIFIER          0x0059                      /**< Company identifier for Nordic Semiconductor ASA. as per www.bluetooth.org. */
    
    static uint8_t   m_encoded_data[2][BLE_GAP_ADV_SET_DATA_SIZE_MAX];  /**< Buffer for storing an encoded advertising set. */
    
    /**@brief Struct that contains pointers to the encoded advertising data. */
    static ble_gap_adv_data_t m_adv_data =
    {
        .adv_data =
        {
            .p_data = m_encoded_data[0],
            .len    = BLE_GAP_ADV_SET_DATA_SIZE_MAX
        },
        .scan_rsp_data =
        {
            .p_data = NULL,
            .len    = 0
    
        }
    };
    
    void bsp_event_handler()
    {
    
        ret_code_t               err_code;
        ble_advdata_t            mydata;
        ble_advdata_manuf_data_t manuf_specific_data;
    
        uint8_t data[] = "New!"; //data to include in the manufacturer field
        
    
        manuf_specific_data.data.p_data = data;
        manuf_specific_data.data.size   = sizeof(data);
        manuf_specific_data.company_identifier = APP_COMPANY_IDENTIFIER; // company identifier is required
    
        memset(&mydata, 0, sizeof(mydata));
    
        mydata.flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
        mydata.p_manuf_specific_data = &manuf_specific_data;
    
        /*swap adv data buffer - from API doc: "In order to update advertising 
        data while advertising,new advertising buffers must be provided"*/
        m_adv_data.adv_data.p_data = (m_adv_data.adv_data.p_data == m_encoded_data[0])
                                    ? m_encoded_data[1] : m_encoded_data[0];
    
        err_code = ble_advdata_encode(&mydata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
        APP_ERROR_CHECK(err_code);
    
    
        err_code = sd_ble_gap_adv_set_configure(&(m_advertising.adv_handle), &m_adv_data, NULL);
        APP_ERROR_CHECK(err_code);
    }

  • This did it!  Thank you ! And just to round things out a bit: anyone wanting to maintain the Bluetooth device name on the advertisement should also set the following inside Vidar's modified bsp_event_handler():

    mydata.name_type = BLE_ADVDATA_SHORT_NAME; // or something else from the enum
    mydata.short_name_len = 6; // or whatever other length you picked

  • Could you share your whole main.c file? I would like to have a look for inspiration

  •  i am facing issue ,please Could you share your whole main.c file? I would like to have a look for inspiration

Related