How to correctly enable/disable the Bluetooth radio on the nRF Distance Measurement with BLE Discovery?"

Hey, 

I took the code example for nRF Distance Measurement with Bluetooth LE discovery, and updated the application to start or stop the radio based on a specific condition. I have a button for this and I am using callback.  I found that disabling the BLE radio is not supported (link). I am attempting to halt the scanning and advertising processes. However, this is not working as expected.

I invoke this function to deactivate both advertising and scanning.

void stop_ble_system(void){

	int err;
	ble_working = 0;
	LOG_INF("BLE Stopping\n");
	k_sleep(K_USEC(50));
	
	err = bt_scan_stop();
	if (err) {
		LOG_INF("bt_scan_stop failed (err %d)\n", err);
		return;
	}
	err = bt_le_ext_adv_stop(adv);
	if (err) {
		printk("Failed to stop extended advertising  (err %d)\n", err);
		return;
	}
	dk_set_led(DK_LED1, (ble_working));

}

And turn it on with this function:

void start_ble_system(void){

	int err;
	ble_working = 1;
	dk_set_led(DK_LED1, (ble_working));
	err = bt_enable(NULL);
	struct bt_le_ext_adv_start_param ext_adv_start_param = {0};
	if (err) {
		LOG_INF("Bluetooth init failed (err %d)\n", err);
		return ;
	}

	err = bt_le_ext_adv_create(adv_param, &adv_cb, &adv);
	if (err) {
		printk("Failed to create advertising set (err %d)\n", err);
		return;
	}

	err = bt_le_ext_adv_set_data(adv, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
	if (err) {
		printk("Failed setting adv data (err %d)\n", err);
		return;
	}

	err = bt_le_ext_adv_start(adv, &ext_adv_start_param);
	if (err) {
		printk("Failed to start extended advertising  (err %d)\n", err);
		return;
	}

	err = scan_start();
	if (err) {
		printk("Failed to start scanning (err %d)\n", err);
	}
}

So, essentially, ranging runs alongside the scanner and advertising processes. During this procedure, I am attempting to deactivate both. I'm uncertain if this is the correct approach.

While ranging, when I disable advertising and scanning, the application doesn't crash. However, the application crashes when I attempt to restart it. I noticed this because the LED is not blinking (I have set it to blink at 1 Hz). Do I need to deactivate scanning and advertising in a different order or do I need to call an additional function?

Here is the console output: 

I have searched the forum but no Q&A topic to help me.

Thanks for the help!!!

Parents
  • Hi

    Sorry for the late reply, but we're falling behind with handling some cases, so I'm taking over this case now. As my colleague Einar explains in the link you shared on "disabling Bluetooth" he states that you should terminate active connections and stop scanning/advertising on both ends. From what I can see in your initial ticket, it only seems like you stop scanning/advertising, but don't terminate the connections. I haven't had time to look at your full project yet, but please make sure you terminate connections on both ends properly and disable scanning/advertising. Then I think you should reinit the Bluetooth stack to avoid any of the error messages you're seeing.

    Best regards,

    Simon

  • Hello, 

    I'll check and then let you knowif this solved the issue.

    Thanks

Reply Children
No Data
Related