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

ble_app_template based app with adv frame update during runntime?


(SES + SDK15)

Hi!

I am trying to create an new firmware prototype based  on ble_app_template_sample

Until now I have a connectable firmware, with one service and several characteristics. Everything is working well.

But now I have the need to change the advertisement manuf data on the fly while advertising.
It seems that in SDK 15 the way the manuf data is changed is different.
Until now I haven't succeeded on this.

Is there any  ble peripheral connectable sample that also updates manuf data using SDK15 new approach?


I have this prototype code:


//Some globals
BLE_ADVERTISING_DEF(m_advertising);                                             /**< Advertising module instance. */
ble_advertising_init_t
ble_advdata_manuf_data_t manuf_data; //Variable to hold manufacturer specific data


void advertising_init(void)
{
    ret_code_t             err_code;
    memset(&init, 0, sizeof(init));

    //just to...
    data[0] = 0xCA;    
    data[1] = 0xCA;    
    data[2] = 0xCA;    
    data[3] = 0xCA;
    data[4] = 0xCA;
    data[5] = 0xCA;
    data[6] = 0xCA;
    data[7] = 0xCA;

    manuf_data.company_identifier             =  0xFFFF;
    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_SHORT_NAME; // Use a shortened name
    init.advdata.short_name_len = 6;    
    init.advdata.include_appearance      = true;
    init.advdata.flags                   = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
    int8_t tx_power                       = -4;
    init.advdata.p_tx_power_level = &tx_power;
 
    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);
}


Then, in an app timer I have this starting code:


static void timer_timeout_handler(void * p_context)
{
    UNUSED_PARAMETER(p_context);

   //Reset to avoid double buffering
   ret_code_t ret = sd_ble_gap_adv_set_configure(m_advertising.adv_handle,NULL,NULL);

   //???

   ret = sd_ble_gap_adv_set_configure(m_advertising.adv_handle,m_advertising.p_adv_data,NULL);
   VERIFY_SUCCESS(ret);

}

Please, how to change one or two sample bytes of advertising frame in the //??? place?

Thanks al lot

Alex

Parents
  • Hey, the advertising data can be changed during advertising, but you need to use two buffers and switch between them. Refer to the code below to see how it can be implemented.

    static void advertising_data_update2(uint8_t adv_manuf_byte)
    {
       ble_advdata_t              advdata;       /**< Advertising data: name, appearance, discovery flags, and more. */
       
    	ble_advdata_manuf_data_t 	adv_manuf_data;
    	
        uint8_array_t            	adv_manuf_data_array;
        uint32_t ret = NRF_SUCCESS;
        static uint8_t buffer1[28];
    	static uint8_t buffer2[28];
    	
    	uint8_t * buffer;
    	
    	static bool b = true;
    	
    	buffer = b ? buffer1 : buffer2;
    	
    	adv_manuf_data_data[0]     = adv_manuf_byte;
    		adv_manuf_data_data[2]     = adv_manuf_byte;
    
        memset(&advdata, 0, sizeof(advdata));
       
        advdata.name_type          	        = BLE_ADVDATA_FULL_NAME;
        advdata.include_appearance 	        = false;
        advdata.flags              	        = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
    
    
    	adv_manuf_data_array.p_data         = adv_manuf_data_data;
    	adv_manuf_data_array.size 			= sizeof(adv_manuf_data_data);
    	adv_manuf_data.company_identifier   = 0x0059;
    	adv_manuf_data.data 				= adv_manuf_data_array;
    	advdata.p_manuf_specific_data 		= &adv_manuf_data;
    	
    
        memset(buffer, 0, m_advertising.adv_data.adv_data.len);
        ble_gap_adv_data_t new_advdata;
        memset(&new_advdata, 0, sizeof(new_advdata));
        new_advdata.adv_data.p_data = buffer;
        new_advdata.adv_data.len = m_advertising.adv_data.adv_data.len;      
        ret = ble_advdata_encode(&advdata, new_advdata.adv_data.p_data, &new_advdata.adv_data.len);
    	  NRF_LOG_INFO("ble_advdata_encode ret:%d",ret);
        APP_ERROR_CHECK(ret);
    	
    	
    	  ret = ble_advertising_advdata_update(&m_advertising,&new_advdata,false);
    	  NRF_LOG_INFO("ble_advertising_advdata_update ret:%d",ret);
        APP_ERROR_CHECK(ret);
    
    	b = !b;
    }

Reply
  • Hey, the advertising data can be changed during advertising, but you need to use two buffers and switch between them. Refer to the code below to see how it can be implemented.

    static void advertising_data_update2(uint8_t adv_manuf_byte)
    {
       ble_advdata_t              advdata;       /**< Advertising data: name, appearance, discovery flags, and more. */
       
    	ble_advdata_manuf_data_t 	adv_manuf_data;
    	
        uint8_array_t            	adv_manuf_data_array;
        uint32_t ret = NRF_SUCCESS;
        static uint8_t buffer1[28];
    	static uint8_t buffer2[28];
    	
    	uint8_t * buffer;
    	
    	static bool b = true;
    	
    	buffer = b ? buffer1 : buffer2;
    	
    	adv_manuf_data_data[0]     = adv_manuf_byte;
    		adv_manuf_data_data[2]     = adv_manuf_byte;
    
        memset(&advdata, 0, sizeof(advdata));
       
        advdata.name_type          	        = BLE_ADVDATA_FULL_NAME;
        advdata.include_appearance 	        = false;
        advdata.flags              	        = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
    
    
    	adv_manuf_data_array.p_data         = adv_manuf_data_data;
    	adv_manuf_data_array.size 			= sizeof(adv_manuf_data_data);
    	adv_manuf_data.company_identifier   = 0x0059;
    	adv_manuf_data.data 				= adv_manuf_data_array;
    	advdata.p_manuf_specific_data 		= &adv_manuf_data;
    	
    
        memset(buffer, 0, m_advertising.adv_data.adv_data.len);
        ble_gap_adv_data_t new_advdata;
        memset(&new_advdata, 0, sizeof(new_advdata));
        new_advdata.adv_data.p_data = buffer;
        new_advdata.adv_data.len = m_advertising.adv_data.adv_data.len;      
        ret = ble_advdata_encode(&advdata, new_advdata.adv_data.p_data, &new_advdata.adv_data.len);
    	  NRF_LOG_INFO("ble_advdata_encode ret:%d",ret);
        APP_ERROR_CHECK(ret);
    	
    	
    	  ret = ble_advertising_advdata_update(&m_advertising,&new_advdata,false);
    	  NRF_LOG_INFO("ble_advertising_advdata_update ret:%d",ret);
        APP_ERROR_CHECK(ret);
    
    	b = !b;
    }

Children
Related