BLE-NRF51822 tutorial 8-dynamic Broadcast

Source: Internet
Author: User

BLE-NRF51822 tutorial 8-dynamic Broadcast

This topic describes how to implement dynamic broadcast.

The tutorial is based on the Uart example in the 9.0 sdk.


To implement dynamic broadcast, choose broadcast> stop broadcast> modify parameters> restart broadcast.


Therefore, we use a timer to Periodically disable broadcast and then modify broadcast data before enabling broadcast.


In Sdk 9.0, I implemented several broadcast modes, which was a little troublesome, so I made a big change to him.

First, the advertising_init function in main. c needs to be modified.


The red part is added to the main modification. Change the flag to GENERAL. The broadcast timeout value is set to 0 to enable wireless broadcasting.

The function for setting Broadcast Data Based on broadcast mode and setting for scanning response data are removed.

Void advertising_init (void)

{

Uint32_t err_code;

Ble_advdata_t advdata;

Ble_advdata_t scanrsp;


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;

Staticble_advdata_manuf_data_t manuf_data;

Staticuint8_t mydata = 0; // here static data is used

// The dynamic broadcast is used to call this function each time.

// Number, the vendor-defined field in the broadcast data will be added with 1

Manuf_data.company_identifier = 0 xffaa;

Manuf_data.data.size = 1;

Manuf_data.data.p_data = & mydata;

Mydata ++; // Add 1 after each call

Advdata. p_manuf_specific_data = & manuf_data;

Err_code = ble_advdata_set (& advdata, NULL );

APP_ERROR_CHECK (err_code );

}






Then start the broadcast function. The Main function uses ble_advertising_start.

We don't need it here. Directly implement a simple function


Void myadv_start (void ){

Ble_gap_adv_params_tadv_params;

Adv_params.interval = 1600; // set the broadcast interval to 1 s.

Adv_params.timeout = 0; // set 0 here and use the flag above to implement wireless broadcast.

Adv_params.type = BLE_GAP_ADV_TYPE_ADV_IND;

Adv_params.channel_mask.ch_37_off = 0;

Adv_params.channel_mask.ch_38_off = 0;

Adv_params.channel_mask.ch_39_off = 0;

Adv_params.type = BLE_GAP_ADV_TYPE_ADV_IND;

Adv_params.p_peer_addr = NULL;

Adv_params.fp = BLE_GAP_ADV_FP_ANY;

Adv_params.p_whitelist = NULL;

Sd_ble_gap_adv_start (& adv_params );

}


Then we make a timer. The timer is used to disable or modify broadcast parameters and start broadcast.









Make the following changes in the main function, create a 2 S timer my_timer, and enable it


Int main (void)

{

Uint32_t err_code;

Bool erase_bonds;

Uint8_t start_string [] = START_STRING;

App_timer_id_tmy_timer;

// Initialize.

APP_TIMER_INIT (APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, false );

Uart_init ();

App_timer_create (& my_timer, APP_TIMER_MODE_REPEATED,

My_timer_handler );

Buttons_leds_init (& erase_bonds );

Ble_stack_init ();

Gap_params_init ();

Services_init ();

Advertising_init ();

Conn_params_init ();

App_timer_start (my_timer, APP_TIMER_TICKS (2000, APP_TIMER_PRESCALER), NULL );

Myadv_start (); // The start broadcast function defined here.


For (;;)

{

Power_manage ();

}

}





Then implement the overflow processing function of the timer.


Void my_timer_handler (void * p_contex ){

Int I = 50;

Sd_ble_gap_adv_stop (); // disable broadcast.

Advertising_init (); // modify the data in the vendor-defined field

While (I --);

Myadv_start (); // enable Broadcast

}


Dynamic broadcast is used to dynamically modify the vendor-defined field in broadcast data. Each call

When the advertising_init function is used, the value of the vendor-defined field increases progressively. Annotations are provided in the advertising_init function.




At this point, dynamic broadcasting has been done.

However, we also need to make a change in the uart example. We have created a timer of our own, but the maximum number of timer instances allowed in the uart example is the number of available timer instances, therefore, an error occurs when we add our own timer.


Solution: Find the APP_TIMER_MAX_TIMERS macro in the main. c file and increase it by 1.


Then the program will be burned to see that the vendor-defined field in the broadcast data is dynamically changed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.