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