@@ -39,7 +39,7 @@ static const char* LOG_TAG = "BLEDevice";
39
39
BLEServer* BLEDevice::m_pServer = nullptr ;
40
40
BLEScan* BLEDevice::m_pScan = nullptr ;
41
41
BLEClient* BLEDevice::m_pClient = nullptr ;
42
-
42
+ bool initialized = false ;
43
43
/* *
44
44
* @brief Create a new instance of a client.
45
45
* @return A new instance of the client.
@@ -178,68 +178,77 @@ BLEScan* BLEDevice::getScan() {
178
178
* @param deviceName The device name of the device.
179
179
*/
180
180
void BLEDevice::init (std::string deviceName) {
181
- esp_err_t errRc = ::nvs_flash_init ();
182
- if (errRc != ESP_OK) {
183
- ESP_LOGE (LOG_TAG, " nvs_flash_init: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
184
- return ;
185
- }
186
-
187
- esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT ();
188
- errRc = esp_bt_controller_init (&bt_cfg);
189
- if (errRc != ESP_OK) {
190
- ESP_LOGE (LOG_TAG, " esp_bt_controller_init: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
191
- return ;
192
- }
181
+ if (!initialized){
182
+ initialized = true ;
183
+ esp_err_t errRc = ::nvs_flash_init ();
184
+ if (errRc != ESP_OK) {
185
+ ESP_LOGE (LOG_TAG, " nvs_flash_init: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
186
+ return ;
187
+ }
193
188
194
- errRc = esp_bt_controller_enable (ESP_BT_MODE_BLE);
195
- if (errRc != ESP_OK) {
196
- ESP_LOGE (LOG_TAG, " esp_bt_controller_enable: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
197
- return ;
198
- }
189
+ esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT ();
190
+ errRc = esp_bt_controller_init (&bt_cfg);
191
+ if (errRc != ESP_OK) {
192
+ ESP_LOGE (LOG_TAG, " esp_bt_controller_init: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
193
+ return ;
194
+ }
195
+ #ifndef CLASSIC_BT_ENABLED
196
+ // esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT); //FIXME waiting for response from esp-idf issue
197
+ errRc = esp_bt_controller_enable (ESP_BT_MODE_BLE);
198
+ if (errRc != ESP_OK) {
199
+ ESP_LOGE (LOG_TAG, " esp_bt_controller_enable: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
200
+ return ;
201
+ }
202
+ #else
203
+ errRc = esp_bt_controller_enable (ESP_BT_MODE_BTDM);
204
+ if (errRc != ESP_OK) {
205
+ ESP_LOGE (LOG_TAG, " esp_bt_controller_enable: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
206
+ return ;
207
+ }
208
+ #endif
209
+ errRc = esp_bluedroid_init ();
210
+ if (errRc != ESP_OK) {
211
+ ESP_LOGE (LOG_TAG, " esp_bluedroid_init: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
212
+ return ;
213
+ }
199
214
200
- errRc = esp_bluedroid_init ();
201
- if (errRc != ESP_OK) {
202
- ESP_LOGE (LOG_TAG, " esp_bluedroid_init : rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
203
- return ;
204
- }
215
+ errRc = esp_bluedroid_enable ();
216
+ if (errRc != ESP_OK) {
217
+ ESP_LOGE (LOG_TAG, " esp_bluedroid_enable : rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
218
+ return ;
219
+ }
205
220
206
- errRc = esp_bluedroid_enable ( );
207
- if (errRc != ESP_OK) {
208
- ESP_LOGE (LOG_TAG, " esp_bluedroid_enable : rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
209
- return ;
210
- }
221
+ errRc = esp_ble_gap_register_callback (BLEDevice::gapEventHandler );
222
+ if (errRc != ESP_OK) {
223
+ ESP_LOGE (LOG_TAG, " esp_ble_gap_register_callback : rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
224
+ return ;
225
+ }
211
226
212
- errRc = esp_ble_gap_register_callback (BLEDevice::gapEventHandler );
213
- if (errRc != ESP_OK) {
214
- ESP_LOGE (LOG_TAG, " esp_ble_gap_register_callback : rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
215
- return ;
216
- }
227
+ errRc = esp_ble_gattc_register_callback (BLEDevice::gattClientEventHandler );
228
+ if (errRc != ESP_OK) {
229
+ ESP_LOGE (LOG_TAG, " esp_ble_gattc_register_callback : rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
230
+ return ;
231
+ }
217
232
218
- errRc = esp_ble_gattc_register_callback (BLEDevice::gattClientEventHandler );
219
- if (errRc != ESP_OK) {
220
- ESP_LOGE (LOG_TAG, " esp_ble_gattc_register_callback : rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
221
- return ;
222
- }
233
+ errRc = esp_ble_gatts_register_callback (BLEDevice::gattServerEventHandler );
234
+ if (errRc != ESP_OK) {
235
+ ESP_LOGE (LOG_TAG, " esp_ble_gatts_register_callback : rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
236
+ return ;
237
+ }
223
238
224
- errRc = esp_ble_gatts_register_callback (BLEDevice::gattServerEventHandler);
225
- if (errRc != ESP_OK) {
226
- ESP_LOGE (LOG_TAG, " esp_ble_gatts_register_callback: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
227
- return ;
239
+ errRc = ::esp_ble_gap_set_device_name (deviceName.c_str ());
240
+ if (errRc != ESP_OK) {
241
+ ESP_LOGE (LOG_TAG, " esp_ble_gap_set_device_name: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
242
+ return ;
243
+ };
244
+
245
+ esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE;
246
+ errRc = ::esp_ble_gap_set_security_param (ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof (uint8_t ));
247
+ if (errRc != ESP_OK) {
248
+ ESP_LOGE (LOG_TAG, " esp_ble_gap_set_security_param: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
249
+ return ;
250
+ };
228
251
}
229
-
230
- errRc = ::esp_ble_gap_set_device_name (deviceName.c_str ());
231
- if (errRc != ESP_OK) {
232
- ESP_LOGE (LOG_TAG, " esp_ble_gap_set_device_name: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
233
- return ;
234
- };
235
-
236
- esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE;
237
- errRc = ::esp_ble_gap_set_security_param (ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof (uint8_t ));
238
- if (errRc != ESP_OK) {
239
- ESP_LOGE (LOG_TAG, " esp_ble_gap_set_security_param: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
240
- return ;
241
- };
242
-
243
252
vTaskDelay (200 /portTICK_PERIOD_MS); // Delay for 200 msecs as a workaround to an apparent Arduino environment issue.
244
253
} // init
245
254
0 commit comments