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
  • 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).

    Could you try to run in Debug mode and set some brake points, then go through the scenario where you think the device is crashing. 



    Here is the console output: 

    Does not seem like the output indicates a crash. Does the console output just stop ? 


    Running in debug mode should give you some insight on what is happening and will show where in the application it gets stuck. 


    Regards,
    Jonathan

  • Could you try to run in Debug mode and set some brake points, then go through the scenario where you think the device is crashing

    I tried it and no exception is thrown. When you call stop_ble_system() and start_ble_system(), you would think that the application is running normally. At second glance, you notice that LED4 is no longer blinking (The main is toggling the LED at 1Hz). Also the DM request and DM ranging pins (0.28, 0.29) do not toggle. When I press pause to see where the application is, I am usually in the scanning process:

    When I try to continue to execute the program, the Error Fault Handler is called because the BLE constraints are violated.

    Does not seem like the output indicates a crash. Does the console output just stop ? 

    Yes, my output freezes like that:

    I have attached a project of my application. Maybe it will be faster this way. Below is the minimal version of the project:

    dm_minimal.zip

    If you press key 1, the measurement will be stopped, and if you press key 2, the measurement will be started again.

    I hope this helps a bit.

  • What NCS version are you using?

    Regards,
    Jonathan

Reply Children
Related