Page 1 of 1

How to properly disable Bluetooth ?

Posted: Fri Mar 29, 2019 9:15 am
by balix53
Hi,

I have an application that will mount a GATT server for a specific task. Once this task is done I need to be sure that the bluetooth is totally and properly disabled.

Here is how it is initialized (note that I am using esp-idf v3.1.3) :

Code: Select all

static bool initialize_ble(void)
{
	esp_err_t status = esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
	if (status != ESP_OK) {
		printf("esp_bt_controller_mem_release status=%d\n", status);
		return false;
	}

	esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
	status = esp_bt_controller_init(&bt_cfg);
	if (status != ESP_OK) {
		printf("esp_bt_controller_init status=%d\n", status);
		return false;
	}

	status = esp_bt_controller_enable(ESP_BT_MODE_BLE);
	if (status != ESP_OK) {
		printf("esp_bt_controller_enable status=%d\n", status);
		return false;
	}

	status = esp_bluedroid_init();
	if (status != ESP_OK) {
		printf("esp_bluedroid_init status=%d\n", status);
		return false;
	}

	status = esp_bluedroid_enable();
	if (status != ESP_OK) {
		printf("esp_bluedroid_enable status=%d\n", status);
		return false;
	}

	status = esp_ble_gatts_register_callback(handle_event_gatts);
	if (status != ESP_OK) {
		printf("esp_ble_gatts_register_callback status=%d\n", status);
		return false;
	}

	status = esp_ble_gatts_app_register(APP_ID);
	if (status != ESP_OK) {
		printf("esp_ble_gatts_app_register status=%d\n", status);
		return false;
	}

	return true;
}
And here is how it is disabled :

Code: Select all

static bool deinit_ble(void)
{
	esp_err_t status = esp_ble_gatts_app_unregister(app_gatts_if);
	if (status != ESP_OK) {
		printf("esp_ble_gatts_app_unregister status=%d\n", status);
		return false;
	}

	status = esp_bluedroid_disable();
	if (status != ESP_OK) {
		printf("esp_bluedroid_disable status=%d\n", status);
		return false;
	}

	status = esp_bluedroid_deinit();
	if (status != ESP_OK) {
		printf("esp_bluedroid_deinit status=%d\n", status);
		return false;
	}

	status = esp_bt_controller_disable();
	if (status != ESP_OK) {
		printf("esp_bt_controller_disable status=%d\n", status);
		return false;
	}

	status = esp_bt_controller_deinit();
	if (status != ESP_OK) {
		printf("esp_bt_controller_deinit status=%d\n", status);
		return false;
	}

	status = esp_bt_controller_mem_release(ESP_BT_MODE_BLE);
	if (status != ESP_OK) {
		printf("esp_bt_controller_mem_release status=%d\n", status);
		return false;
	}

	return true;
}
I wanted to know if it is done properly and nothing is missing. I also could not find any example of how to do this in the esp-idf v3.1.3 release.

Feel free to ask me anything if more details are needed.

Regards.