Bluetooth deinitialize and initialize process
Posted: Thu Jul 21, 2022 2:32 pm
Hello,
We have a use case were we initialize bluetooth, disable/deinit bluetooth and initialize it again.
Considering ble gatt sever example.
Initialize bluetooth:
De-initialize Bluetooth:
First initialize is working fine and bluetooth is been de-initialize Successfully. After this when we again initialize, facing below error and code restarts:
What is the appropriate way to implement bluetooth re-initialization.
We have a use case were we initialize bluetooth, disable/deinit bluetooth and initialize it again.
Considering ble gatt sever example.
Initialize bluetooth:
- void Bluetooth_Service_Initialize()
- {
- esp_err_t ret;
- /* Initialize NVS. */
- ret = nvs_flash_init();
- if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
- ESP_ERROR_CHECK(nvs_flash_erase());
- ret = nvs_flash_init();
- }
- ESP_ERROR_CHECK( ret );
- ESP_ERROR_CHECK(nvs_flash_erase());
- ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
- esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
- ret = esp_bt_controller_init(&bt_cfg);
- if (ret) {
- ESP_LOGE(EXAMPLE_TAG, "%s enable controller failed: %s", __func__, esp_err_to_name(ret));
- return;
- }
- ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
- if (ret) {
- ESP_LOGE(EXAMPLE_TAG, "%s enable controller failed: %s", __func__, esp_err_to_name(ret));
- return;
- }
- ret = esp_bluedroid_init();
- if (ret) {
- ESP_LOGE(EXAMPLE_TAG, "%s init bluetooth failed: %s", __func__, esp_err_to_name(ret));
- return;
- }
- ret = esp_bluedroid_enable();
- if (ret) {
- ESP_LOGE(EXAMPLE_TAG, "%s enable bluetooth failed: %s", __func__, esp_err_to_name(ret));
- return;
- }
- ret = esp_ble_gatts_register_callback(gatts_event_handler);
- if (ret){
- ESP_LOGE(EXAMPLE_TAG, "gatts register error, error code = %x", ret);
- return;
- }
- ret = esp_ble_gap_register_callback(gap_event_handler);
- if (ret){
- ESP_LOGE(EXAMPLE_TAG, "gap register error, error code = %x", ret);
- return;
- }
- ret = esp_ble_gatts_app_register(ESP_APP_ID);
- if (ret){
- ESP_LOGE(EXAMPLE_TAG, "gatts app register error, error code = %x", ret);
- return;
- }
- esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(33);
- if (local_mtu_ret){
- ESP_LOGE(EXAMPLE_TAG, "set local MTU failed, error code = %x", local_mtu_ret);
- }
- /* set the security iocap & auth_req & key size & init key response key parameters to the stack*/
- esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_MITM_BOND; //bonding with peer device after authentication
- esp_ble_io_cap_t iocap = ESP_IO_CAP_OUT; //set the IO capability to No output No input
- uint8_t key_size = 16; //the key size should be 7~16 bytes
- uint8_t init_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
- uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
- uint32_t passkey = 123456;
- esp_ble_gap_set_security_param(ESP_BLE_SM_SET_STATIC_PASSKEY, &passkey, sizeof(uint32_t));
- esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(uint8_t));
- esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t));
- esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size, sizeof(uint8_t));
- /* If your BLE device act as a Slave, the init_key means you hope which types of key of the master should distribute to you,
- and the response key means which key you can distribute to the Master;
- If your BLE device act as a master, the response key means you hope which types of key of the slave should distribute to you,
- and the init key means which key you can distribute to the slave. */
- esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t));
- esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t));
- }
De-initialize Bluetooth:
- esp_err_t Bluetooth_Deinitialize (void)
- {
- esp_err_t ret = 0;
- esp_ble_gap_stop_advertising ();
- ret = esp_bluedroid_disable ();
- if (ret != ESP_OK)
- {
- printf ("esp_bt_controller_disable FAILEd =%d\n", ret);
- return ret;
- }
- ret = esp_bluedroid_deinit ();
- if (ret != ESP_OK)
- {
- printf ("esp_bluedroid_deinit FAILEd =%d\n", ret);
- return ret;
- }
- ret = esp_bt_controller_disable ();
- if (ret != ESP_OK)
- {
- printf ("esp_bt_controller_disable FAILEd =%d\n", ret);
- return ret;
- }
- ret = esp_bt_controller_deinit ();
- if (ret != ESP_OK)
- {
- printf ("ble deiniet FAILEd =%d\n", ret);
- return ret;
- }
- ret = esp_bt_controller_mem_release (ESP_BT_MODE_BLE);
- if (ret != ESP_OK)
- {
- printf ("ble mem_release FAILEd =%d\n", ret);
- return ret;
- }
- return ret;
- }
First initialize is working fine and bluetooth is been de-initialize Successfully. After this when we again initialize, facing below error and code restarts:
Code: Select all
ESP_ERROR_CHECK failed: esp_err_t 0x103 (ESP_ERR_INVALID_STATE) at 0x4008feec
file: "../main/ble_compatibility_test.c" line 632
func: Bluetooth_Service_Initialize
expression: esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT)
abort() was called at PC 0x4008feef on core 0
Backtrace:0x40081a5e:0x3ffbbf100x4008fef9:0x3ffbbf30 0x40095062:0x3ffbbf50 0x4008feef:0x3ffbbfc0 0x400d721c:0x3ffbbfe0 0x400d744d:0x3ffbc040 0x40141a3d:0x3ffbc060 0x40092af1:0x3ffbc080
ELF file SHA256: f4bf2cf85d852af7
Rebooting...
ets Jul 29 2019 12:21:46
rst:0xc (SW_CPU_RESET),boot:0x32 (SPI_FAST_FLASH_BOOT)
What is the appropriate way to implement bluetooth re-initialization.