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 Reply Children
  • 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

  • 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();
        }
    }
    
    
    

Related