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

  • 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 

  • Hello,

    ps_anu said:

    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 

    Well, you could do this, but you are here just creating a shallow copy of the my_temp variable.
    I do not know if this is intentional in your case.
    But yes, you should initialize the variables. If you do not initialize them, you have no way of knowing what the variable might start out to be. For example, the 0x47 you saw earlier could have been a result of your random initialization here.

    ps_anu said:

    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 

    Could you try to test this again, but this time, having initialized your my_temp variable to something else - like dec 0 or dec 100, and then changing it in the sensor_data funtion to dec 50. Try this, and let me know if you are seeing the same results.

    I see that you have made additions and edits to your previously shared code - please do not do this.
    I do not recommend doing this, since I then have to read through the code and remember what you might have changed since my last comments. Instead, you should share only the snippets you have changed in your new replies, for example: "I have changed this and that function as shown below, and I am not seeing ... ".
    By adding and editing the previously shared code, you are also voiding all my previously written comments - as they are comments to code that no longer exist or is structured in the way that it was. This makes it hard for you too, if you ever are to go back and read these comments.

    In your appended sensor_data function, you are trying to write my_temp[1], buy my_temp is an array of length 1. You probably meant to write to my_temp[0] here.
    Furthermore, the change you have made in line 17 of your code snippet titled update_advertising_data function does not make sense, as sensor_data is first called as a function, and then subsequently used as a variable. I think you meant to use my_temp here, but please do not make such additions and edits because it is incredible hard to read and account for, and in this case probably also wrong entirely.

    In your appended sensor_data function the value of my_temp[1] is set to 50, for the record. 50 decimal value is 0x32 in hex value, 40 decimal value is 0x28 in hex value.

    ps_anu said:

    because , my_temp will change it is not constant value 

    i want dynamical data 

    That is fine. The volatile modifier is used when a register or value might change without the compiler seeing that it can happen - such as if you are pointing to a register that is connected to an external interrupts.
    Global variables that are not declared const can be changed as normal.

    Best regards,
    Karl

  • Thanks alot.. It worked , I have made changes as per you said  and one more here i need to advertise my own name how to do that ? I am working on beacon example in this example gap function is not there 

Reply Children
Related