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

how to add custom data in advertise packet with sdk17.2.0

Hi,

I am working on nrf52832 development board.

I am using SDK17.2.0.

I am running the device in peripheral mode in always advertising mode.

And I would like to add custom data to the advertise packet so that the central device on the other side can connect to this peripheral device based on this custom data. And I am not sure how to add that custom data into advertise packet.

I don't want to change the manufacturer or other general information, but want to add my own data to the packet when ever I need without calling stop advertising function.

Please help me with the API's that I have to use and a code snippets which can make my job easier.

Parents
  • Hi,

    Please see our tutorial on advertising on how to customize the advertising packet.

    regards

    Jared 

  • I went through the tutorial. This code is for adding data in advertising_init() funciton called before calling advertising_start() function. I want to update data during advertising but a button press or something.

    static void advertising_init(void)
    {
    ret_code_t err_code;
    ble_advertising_init_t init; // Struct containing advertising parameters
    // Build advertising data struct to pass into @ref ble_advertising_init.
    memset(&init, 0, sizeof(init));

    ble_advdata_manuf_data_t manuf_data; //Variable to hold manufacturer specific data
    uint8_t data[] = "SomeData!"; //Our data to advertise
    manuf_data.company_identifier = 0x0059; //Nordics company ID
    manuf_data.data.p_data = data;
    manuf_data.data.size = sizeof(data);
    init.advdata.p_manuf_specific_data = &manuf_data;

    init.advdata.name_type = BLE_ADVDATA_FULL_NAME; // Use a shortened name
    init.advdata.short_name_len = 6; // Advertise only first 6 letters of name
    init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
    init.advdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
    init.advdata.uuids_complete.p_uuids = m_adv_uuids;

    init.config.ble_adv_fast_enabled = true;
    init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
    init.config.ble_adv_fast_timeout = APP_ADV_DURATION;

    init.evt_handler = on_adv_evt;

    err_code = ble_advertising_init(&m_advertising, &init);
    APP_ERROR_CHECK(err_code);

    ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
    }

  • And i have already tried this code.

    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);

    But is that ble_advdata_encode(); needed for sure? Can i send data without encoding?

Reply
  • And i have already tried this code.

    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);

    But is that ble_advdata_encode(); needed for sure? Can i send data without encoding?

Children
Related