Change 'Connection Advertising Period(time)' dynamically in nRF52840

Hello Nordic Team,

I am currently working with development of applications for custom boards utilizing the nRF Connect SDK v2.4.0, with a focus on the nRF52840 microcontroller.

For this current project, the client has requested the implementation of a feature wherein the Advertising Period of the nRF52840 device needs to be dynamically changed from a mobile application. To be specific, what he requires is: When device boot, it should advertise for 1 second and than the device should disappear for 60secs and the process continuous. This cycle is intended to optimize power consumption.

I came across the following resource: https://academy.nordicsemi.com/courses/bluetooth-low-energy-fundamentals/lessons/lesson-2-bluetooth-le-advertising/

Based on the information provided, it seems that the referenced code snippet may address the requirements. However, can I have clarification from your end?.

static struct bt_le_adv_param *adv_param = BT_LE_ADV_PARAM(
	(BT_LE_ADV_OPT_CONNECTABLE |
	 BT_LE_ADV_OPT_USE_IDENTITY), /* Connectable advertising and use identity address */
	800, /* Min Advertising Interval 500ms (800*0.625ms) */
	801, /* Max Advertising Interval 500.625ms (801*0.625ms) */
	NULL); /* Set to NULL for undirected advertising */

Could you kindly confirm if the specified process (in bold) is feasible? If affirmative, could you provide guidance on the appropriate configuration for achieving the desired 'Advertising Period' settings?

Regards,

Anmol

Parents
  • Hi,

    the client has requested the implementation of a feature wherein the Advertising Period of the nRF52840 device needs to be dynamically changed from a mobile application.

    In order to change the advertising period from the mobile application, you need to create a connection between the phone and the advertiser, and implement a service (or use an existing Bluetooth service) to send the new period from the application to the nRF52840 device. The device can then use this new period to configure the advertising.

    When device boot, it should advertise for 1 second and than the device should disappear for 60secs and the process continuous.

    The advertiser can be started with a timeout, to stop advertising after the given time. You will need to use a timer to restart the advertising after the sleep interval. Set CONFIG_BT_LIM_ADV_TIMEOUT and add BT_LE_AD_LIMITED to the BT_DATA_FLAGS.

    Best regards,
    Jørgen

  • Hi,

    Anmol Maske said:
    I have used this function for setting the timers. Can you please suggest is this the right timer I am using or need to change for the same?

    Yes, this timer should work. In the timer handler you need to restart the advertising, for instance by calling bt_le_adv_start().

    Anmol Maske said:
    However, I have encountered an issue wherein the advertisement fails to restart after the timeout period.

    Can you elaborate what exactly fails?

    Anmol Maske said:
    I have reviewed the code and configuration settings, but I have not been able to identify the root cause of the problem.

    Can you post your full code to configure the timer and restarting the advertising?

    Best regards,
    Jørgen

  • Hi,

    void my_timer_handler(struct k_timer *my_timer)
    {
       int err = bt_le_adv_start(adv_param, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
    	if (err) {
    		LOG_ERR("Advertising failed to start (err %d)\n", err);
    		return;
    	}
    }
    
    K_TIMER_DEFINE(my_timer, my_timer_handler, NULL);
    
    
    
    int main(void)
    {
    	int blink_status = 0;
    	int err;
    
    	LOG_INF("Starting Gatt Services \n");
    
    	err = dk_leds_init();
    	if (err) {
    		LOG_ERR("LEDs init failed (err %d)\n", err);
    		return;
    	}
    
    	err = bt_enable(NULL);
    	if (err) {
    		LOG_ERR("Bluetooth init failed (err %d)\n", err);
    		return;
    	}
    	bt_conn_cb_register(&connection_callbacks);
    
        LOG_INF("Bluetooth initialized\n");
    	
    
    	k_timer_start(&my_timer, K_SECONDS(1), K_SECONDS(30));
    
    
    	for (;;) {
    		dk_set_led(RUN_STATUS_LED, (++blink_status) % 2);
    		k_sleep(K_MSEC(RUN_LED_BLINK_INTERVAL));
    	}
    	return 0;
    }

    Using the changes as mentioned above, getting the following error and the device is rebooting as well. Please suggest some solution?

  • Hi,

    I don't see your error, but it might be that you can't call the bt_le_adv_start() function in the timer handler because it runs in interrupt context.

    You can set a flag or a semaphore to indicate that the timer triggered, or use a thread to restart the advertising at a regular interval.

    I have attached a sample showing the latter solution: peripheral_lbs_adv_restart.zip

    To see the changes, you can compare it to peripheral_lbs sample in NCS v2.5.0.

    Best regards,
    Jørgen

Reply Children
No Data
Related