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

how to update the advertisement packet

hi ...

how to update the advertisement  packet .. I have seen many post regarding this ,I am not getting clear , anyone can share me the piece of code which will help me 

Parents
  • Hello,

    Which SDK version are you working with?
    Are you planning to use the Advertising library?
    If so, the easiest way to go about updating the advertising data is to use the ble_advertising_advdata_update function.

    If you are using an SDK version older than v.17, you will need to store two different advertising data buffers, and alternate between which data advdata buffer you provide the _advdata_update function.
    I.e you may not provide the advdata_update function with the same ( but modified ) advdata buffer that you provided during the last call to advdata_update.
    If you are using SDK version >= v.17 then the advdata_update function handles this for you.

    If you are working with an older SDK version, you might also benefit from reading the discussion in this ticket.

    Best regards,
    Karl

  • hi ,

    karl Ylvisaker 

    I  am using 15.2 verion sdk . I Have created two buffers ..kindly check my code , is this correct ? correct me if were wrong And also explain how to use the advertise update function in 17 version sdk

    static ble_gap_adv_data_t m_adv_data1 =
    {
        .adv_data =
        {
            .p_data = m_raw_data_buffer1,
            .len    = BLE_GAP_ADV_SET_DATA_SIZE_MAX
        },
        .scan_rsp_data =
        {
            .p_data = NULL,
            .len    = 0
    
        }
    };
    static ble_gap_adv_data_t m_adv_data2 =
    {
        .adv_data =
        {
            .p_data = m_raw_data_buffer2,
            .len    = BLE_GAP_ADV_SET_DATA_SIZE_MAX
        },
        .scan_rsp_data =
        {
            .p_data = NULL,
            .len    = 0
    
        }
    };
    

    static advertising_init()
    
    {
    ......
    .....
    
      memset(&advdata, 0, sizeof(advdata));
    
        advdata.name_type             = BLE_ADVDATA_NO_NAME;
        advdata.flags                 = flags;
        advdata.p_manuf_specific_data = &manuf_specific_data;
    
        // Initialize advertising parameters (used when starting advertising).
        memset(&m_adv_params, 0, sizeof(m_adv_params));
    
        m_adv_params.properties.type = BLE_GAP_ADV_TYPE_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED;
        m_adv_params.p_peer_addr     = NULL;    // Undirected advertisement.
        m_adv_params.filter_policy   = BLE_GAP_ADV_FP_ANY;
        m_adv_params.interval        = NON_CONNECTABLE_ADV_INTERVAL;
        m_adv_params.duration        = 0;       // Never time out.
    
        err_code = ble_advdata_encode(&advdata, m_adv_data1.adv_data.p_data, &m_adv_data1.adv_data.len);
        APP_ERROR_CHECK(err_code);
    
        err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data1, &m_adv_params);
        APP_ERROR_CHECK(err_code);

    static advertising_update()
    {
    
    
    _ _ _ _ _
     _ _ _
     
     
     err_code = ble_advdata_encode(&advdata, m_adv_data2.adv_data.p_data, &m_adv_data2.adv_data.len);
        APP_ERROR_CHECK(err_code);
    
        err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data2, NULL);
        APP_ERROR_CHECK(err_code);

  • Hello again,

    ps_anu said:
    I  am using 15.2 verion sdk . I Have created two buffers ..kindly check my code , is this correct ?

    What happens when you try to run this code? Does it update your advertising data?
    The general approach looks correct, but I suspect that you have cut out some parts of the functions' code, so I can not say anything for certain.

    ps_anu said:
    also explain how to use the advertise update function in 17 version sdk

    The ble_advertising_advdata_update function updates the advertising and scan response data related to an advertising handle.
    If advertising has already started, it updates the advertising data without stopping and starting advertising.
    In SDK v.17.0.0 it is implemented like the following:

    ret_code_t ble_advertising_advdata_update(ble_advertising_t   * const p_advertising,
                                              ble_advdata_t const * const p_advdata,
                                              ble_advdata_t const * const p_srdata)
    {
        VERIFY_PARAM_NOT_NULL(p_advertising);
        if (p_advertising->initialized == false)
        {
            return NRF_ERROR_INVALID_STATE;
        }
    
        if ((p_advdata == NULL) && (p_srdata == NULL))
        {
            return NRF_ERROR_NULL;
        }
    
        ble_gap_adv_data_t new_adv_data;
        memset(&new_adv_data, 0, sizeof(new_adv_data));
    
        if (p_advdata != NULL)
        {
            new_adv_data.adv_data.p_data =
                (p_advertising->p_adv_data->adv_data.p_data != p_advertising->enc_advdata[0]) ?
                 p_advertising->enc_advdata[0] : p_advertising->enc_advdata[1];
            new_adv_data.adv_data.len = adv_set_data_size_max_get(p_advertising);
    
            ret_code_t ret = ble_advdata_encode(p_advdata,
                                                new_adv_data.adv_data.p_data,
                                                &new_adv_data.adv_data.len);
            VERIFY_SUCCESS(ret);
        }
    
        if (p_srdata != NULL)
        {
            new_adv_data.scan_rsp_data.p_data =
                (p_advertising->p_adv_data->scan_rsp_data.p_data != p_advertising->enc_scan_rsp_data[0]) ?
                 p_advertising->enc_scan_rsp_data[0] : p_advertising->enc_scan_rsp_data[1];
            new_adv_data.scan_rsp_data.len = adv_set_data_size_max_get(p_advertising);
    
            ret_code_t ret = ble_advdata_encode(p_srdata,
                                                new_adv_data.scan_rsp_data.p_data,
                                                &new_adv_data.scan_rsp_data.len);
            VERIFY_SUCCESS(ret);
        }
    
        memcpy(&p_advertising->adv_data, &new_adv_data, sizeof(p_advertising->adv_data));
        p_advertising->p_adv_data = &p_advertising->adv_data;
    
        return sd_ble_gap_adv_set_configure(&p_advertising->adv_handle,
                                            p_advertising->p_adv_data,
                                            NULL);
    }



    Best regards,
    Karl

Reply
  • Hello again,

    ps_anu said:
    I  am using 15.2 verion sdk . I Have created two buffers ..kindly check my code , is this correct ?

    What happens when you try to run this code? Does it update your advertising data?
    The general approach looks correct, but I suspect that you have cut out some parts of the functions' code, so I can not say anything for certain.

    ps_anu said:
    also explain how to use the advertise update function in 17 version sdk

    The ble_advertising_advdata_update function updates the advertising and scan response data related to an advertising handle.
    If advertising has already started, it updates the advertising data without stopping and starting advertising.
    In SDK v.17.0.0 it is implemented like the following:

    ret_code_t ble_advertising_advdata_update(ble_advertising_t   * const p_advertising,
                                              ble_advdata_t const * const p_advdata,
                                              ble_advdata_t const * const p_srdata)
    {
        VERIFY_PARAM_NOT_NULL(p_advertising);
        if (p_advertising->initialized == false)
        {
            return NRF_ERROR_INVALID_STATE;
        }
    
        if ((p_advdata == NULL) && (p_srdata == NULL))
        {
            return NRF_ERROR_NULL;
        }
    
        ble_gap_adv_data_t new_adv_data;
        memset(&new_adv_data, 0, sizeof(new_adv_data));
    
        if (p_advdata != NULL)
        {
            new_adv_data.adv_data.p_data =
                (p_advertising->p_adv_data->adv_data.p_data != p_advertising->enc_advdata[0]) ?
                 p_advertising->enc_advdata[0] : p_advertising->enc_advdata[1];
            new_adv_data.adv_data.len = adv_set_data_size_max_get(p_advertising);
    
            ret_code_t ret = ble_advdata_encode(p_advdata,
                                                new_adv_data.adv_data.p_data,
                                                &new_adv_data.adv_data.len);
            VERIFY_SUCCESS(ret);
        }
    
        if (p_srdata != NULL)
        {
            new_adv_data.scan_rsp_data.p_data =
                (p_advertising->p_adv_data->scan_rsp_data.p_data != p_advertising->enc_scan_rsp_data[0]) ?
                 p_advertising->enc_scan_rsp_data[0] : p_advertising->enc_scan_rsp_data[1];
            new_adv_data.scan_rsp_data.len = adv_set_data_size_max_get(p_advertising);
    
            ret_code_t ret = ble_advdata_encode(p_srdata,
                                                new_adv_data.scan_rsp_data.p_data,
                                                &new_adv_data.scan_rsp_data.len);
            VERIFY_SUCCESS(ret);
        }
    
        memcpy(&p_advertising->adv_data, &new_adv_data, sizeof(p_advertising->adv_data));
        p_advertising->p_adv_data = &p_advertising->adv_data;
    
        return sd_ble_gap_adv_set_configure(&p_advertising->adv_handle,
                                            p_advertising->p_adv_data,
                                            NULL);
    }



    Best regards,
    Karl

Children
  • thanks for your response , where i should include my new updating data ?   and how to assign to 

  • No problem at all, I am happy to help!

    ps_anu said:
    where i should include my new updating data ?   and how to assign to 

    I do not understand what you mean by this.
    You should set the contents of your m_adv_data2 at the start of your advertising_update function ( or if it is two static advertising data buffers, initialize them to the correct values ).

    Best regards,
    Karl

  • good to hear you karl .. 

    yes I tried i am getting the data as 0x47 raw output . I should get 0x32

    Here i am sharing the full code what are the changes i made  here. Kindly check please ,

    volatile static uint8_t my_temp[1];  // initially i am assigning 
    static ble_gap_adv_data_t m_adv_data1 =
    {
        .adv_data =
        {
            .p_data = m_raw_data_buffer1,
            .len    = BLE_GAP_ADV_SET_DATA_SIZE_MAX
        },
        .scan_rsp_data =
        {
            .p_data = NULL,
            .len    = 0
    
        }
    };
    static ble_gap_adv_data_t m_adv_data2 =
    {
        .adv_data =
        {
            .p_data = m_raw_data_buffer2,
            .len    = BLE_GAP_ADV_SET_DATA_SIZE_MAX
        },
        .scan_rsp_data =
        {
            .p_data = NULL,
            .len    = 0
    
        }
    };

    Here it is advertising init function 

    static void advertising_init(void)
    {
        uint32_t      err_code;
        ble_advdata_t advdata;
        uint8_t       flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
    
        ble_advdata_manuf_data_t manuf_specific_data;
    
        manuf_specific_data.company_identifier = APP_COMPANY_IDENTIFIER;
    
    #if defined(USE_UICR_FOR_MAJ_MIN_VALUES)
        // If USE_UICR_FOR_MAJ_MIN_VALUES is defined, the major and minor values will be read from the
        // UICR instead of using the default values. The major and minor values obtained from the UICR
        // are encoded into advertising data in big endian order (MSB First).
        // To set the UICR used by this example to a desired value, write to the address 0x10001080
        // using the nrfjprog tool. The command to be used is as follows.
        // nrfjprog --snr <Segger-chip-Serial-Number> --memwr 0x10001080 --val <your major/minor value>
        // For example, for a major value and minor value of 0xabcd and 0x0102 respectively, the
        // the following command should be used.
        // nrfjprog --snr <Segger-chip-Serial-Number> --memwr 0x10001080 --val 0xabcd0102
        uint16_t major_value = ((*(uint32_t *)UICR_ADDRESS) & 0xFFFF0000) >> 16;
        uint16_t minor_value = ((*(uint32_t *)UICR_ADDRESS) & 0x0000FFFF);
    
        uint8_t index = MAJ_VAL_OFFSET_IN_BEACON_INFO;
    
        m_beacon_info[index++] = MSB_16(major_value);
        m_beacon_info[index++] = LSB_16(major_value);
    
        m_beacon_info[index++] = MSB_16(minor_value);
        m_beacon_info[index++] = LSB_16(minor_value);
    #endif
    
        manuf_specific_data.data.p_data = (uint8_t *) my_temp;
        manuf_specific_data.data.size   = 1;
    
        // Build and set advertising data.
        memset(&advdata, 0, sizeof(advdata));
    
        advdata.name_type             = BLE_ADVDATA_NO_NAME;
        advdata.flags                 = flags;
        advdata.p_manuf_specific_data = &manuf_specific_data;
    
        // Initialize advertising parameters (used when starting advertising).
        memset(&m_adv_params, 0, sizeof(m_adv_params));
    
        m_adv_params.properties.type = BLE_GAP_ADV_TYPE_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED;
        m_adv_params.p_peer_addr     = NULL;    // Undirected advertisement.
        m_adv_params.filter_policy   = BLE_GAP_ADV_FP_ANY;
        m_adv_params.interval        = NON_CONNECTABLE_ADV_INTERVAL;
        m_adv_params.duration        = 0;       // Never time out.
    
        err_code = ble_advdata_encode(&advdata, m_adv_data1.adv_data.p_data, &m_adv_data1.adv_data.len);
        APP_ERROR_CHECK(err_code);
    
        err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data1, &m_adv_params);
        APP_ERROR_CHECK(err_code);
    }
    

    then i am sending the sensor data to it 

    update_advertising_data function

    static void sensor_data()
    {
    
      my_temp[1]=50;
    
    }
    
    static void update_advertising_init(void)
    {
        uint32_t      err_code;
        ble_advdata_t advdata;
        uint8_t       flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
    ble_advdata_manuf_data_t manuf_specific_data;
    manuf_specific_data.company_identifier = APP_COMPANY_IDENTIFIER;
        
         sensor_data();
       manuf_specific_data.data.p_data = sensor_data; // calling the above function uint8_t my_temp[1]={50}
        manuf_specific_data.data.size   = 1;
    
        // Build and set advertising data.
        memset(&advdata, 0, sizeof(advdata));
    
        advdata.name_type             = BLE_ADVDATA_NO_NAME;
        advdata.flags                 = flags;
        advdata.p_manuf_specific_data = &manuf_specific_data;
    
        // Initialize advertising parameters (used when starting advertising).
        memset(&m_adv_params, 0, sizeof(m_adv_params));
    
        m_adv_params.properties.type = BLE_GAP_ADV_TYPE_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED;
        m_adv_params.p_peer_addr     = NULL;    // Undirected advertisement.
        m_adv_params.filter_policy   = BLE_GAP_ADV_FP_ANY;
        m_adv_params.interval        = NON_CONNECTABLE_ADV_INTERVAL;
        m_adv_params.duration        = 0;       // Never time out.
    
        err_code = ble_advdata_encode(&advdata, m_adv_data2.adv_data.p_data, &m_adv_data2.adv_data.len);
        APP_ERROR_CHECK(err_code);
    
        err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data2, NULL);
        APP_ERROR_CHECK(err_code);
    }
    
    int main(void)
    {
        // Initialize.
        log_init();
        timers_init();
        leds_init();
        power_management_init();
        ble_stack_init();
        advertising_init();
     update_advertising_init();  //added
        // Start execution.
        NRF_LOG_INFO("Beacon example started.");
        advertising_start();
    
        // Enter main loop.
        for (;; )
        {
            idle_state_handle();
        }
    }
    
    
    

  • ps_anu said:
    yes I tried i am getting the data as 0x47 raw output . I should get 0x32

    From you code I can not see any content going into m_raw_data_buffer1 and 2. What are the contents of these?
    Furthermore, you are initializing your advertising manufacturer specific data to your my_temp variable - but you never actually initialize the contents of my_temp to anything. Is this on purpose?
    I see that my_temp is volatile - why is this?

    Do I understand you correctly that you are seeing 0x47 as your manufacturing specific data with the above code, but you expected to see 0x32? Please elaborate on where you are getting these values from - what made you expect 0x32, and where does 0x47 come from?
    I also would like to point out that you main function will only change the advertising data once, and this happens before advertising actually starts - so you are not here changing it 'during advertisement', just to be clear.

    As a side note, I would also advice against using any magic numbers in your code. Instead, you should create a define or const variable together with your array declaration, to keep track of its intended size.

    Best regards,
    Karl

  • From you code I can not see any content going into m_raw_data_buffer1 and 2. What are the contents of these?

    so i need to assign with my_temp 

    uint8_t my_temp[1]={90};

    m_raw_data_buffer1[]= my_temp; 

    is it so ? or wrong 

    then 

    Do I understand you correctly that you are seeing 0x47 as your manufacturing specific data with the above code, but you expected to see 0x32? Please elaborate on where you are getting these values from - what made you expect 0x32, and where does 0x47 come from?

     because in update_advertising_init() function i am passing the my_temp as 40 .. 40 hex value is 0x32 . 

    myself i dono where i am getting the 0x47 

    my_temp is volatile - why is this?

    because , my_temp will change it is not constant value 

    i want dynamical data 

Related