@@ -53,8 +53,8 @@ static EventGroupHandle_t _spp_event_group = NULL;
53
53
static boolean secondConnectionAttempt;
54
54
static esp_spp_cb_t * custom_spp_callback = NULL ;
55
55
56
- #define INQ_LEN 30 ;
57
- #define INQ_NUM_RSPS 0 ;
56
+ #define INQ_LEN 30
57
+ #define INQ_NUM_RSPS 0
58
58
static esp_bd_addr_t _peer_bd_addr;
59
59
static char _remote_name[ESP_BT_GAP_MAX_BDNAME_LEN + 1 ];
60
60
static bool _isRemoteAddressSet;
@@ -63,8 +63,6 @@ static esp_bt_pin_code_t _pin_code;
63
63
static int _pin_len;
64
64
static bool _isPinSet;
65
65
static bool _enableSSP;
66
- static bool _isInitializing;
67
- static bool _isInitialized;
68
66
69
67
#define SPP_RUNNING 0x01
70
68
#define SPP_CONNECTED 0x02
@@ -225,9 +223,6 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
225
223
esp_spp_start_srv (ESP_SPP_SEC_NONE, ESP_SPP_ROLE_SLAVE, 0 , _spp_server_name);
226
224
}
227
225
xEventGroupSetBits (_spp_event_group, SPP_RUNNING);
228
- _isInitialized = true ;
229
- _isInitializing = false ;
230
-
231
226
break ;
232
227
233
228
case ESP_SPP_SRV_OPEN_EVT:// Server connection open
@@ -320,7 +315,7 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
320
315
log_i (" ESP_BT_GAP_DISC_RES_EVT" );
321
316
for (int i = 0 ; i < param->disc_res .num_prop ; i++){
322
317
uint8_t peer_bdname_len;
323
- peer_bdname[ESP_BT_GAP_MAX_BDNAME_LEN + 1 ];
318
+ char peer_bdname[ESP_BT_GAP_MAX_BDNAME_LEN + 1 ];
324
319
if (param->disc_res .prop [i].type == ESP_BT_GAP_DEV_PROP_EIR
325
320
&& get_name_from_eir ((uint8_t *)param->disc_res .prop [i].val , peer_bdname, &peer_bdname_len)){
326
321
log_v (" ESP_BT_GAP_DISC_RES_EVT : EIR : %s" , peer_bdname);
@@ -401,7 +396,6 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
401
396
402
397
static bool _init_bt (const char *deviceName)
403
398
{
404
- _isInitializing = false ;
405
399
if (!_spp_event_group){
406
400
_spp_event_group = xEventGroupCreate ();
407
401
if (!_spp_event_group){
@@ -500,14 +494,11 @@ static bool _init_bt(const char *deviceName)
500
494
log_e (" set cod failed" );
501
495
return false ;
502
496
}
503
- _isInitializing = true ;
504
497
return true ;
505
498
}
506
499
507
500
static bool _stop_bt ()
508
501
{
509
- _isInitialized = false ;
510
- _isInitializing = false ;
511
502
if (btStarted ()){
512
503
if (_spp_client)
513
504
esp_spp_disconnect (_spp_client);
@@ -711,28 +702,33 @@ bool BluetoothSerial::disconnect() {
711
702
return false ;
712
703
}
713
704
714
- bool BluetoothSerial::connected () {
705
+ bool BluetoothSerial::connected (int timeout) {
706
+ TickType_t xTicksToWait = timeout / portTICK_PERIOD_MS;
707
+ if ((xEventGroupWaitBits (_spp_event_group, SPP_CONNECTED, pdFALSE, pdTRUE, xTicksToWait) & SPP_CONNECTED)) {
708
+ return true ;
709
+ } else {
710
+ log_e (" Timeout waiting for connected state" );
711
+ return false ;
712
+ }
715
713
return _spp_client != 0 ;
716
714
}
717
715
718
- bool BluetoothSerial::isReady (bool checkMaster)
719
- {
716
+ bool BluetoothSerial::isReady (bool checkMaster, int timeout) {
720
717
if (checkMaster && !_isMaster) {
721
718
log_e (" Master mode is not active. Call begin(localName, true) to enable Master mode" );
722
719
return false ;
723
720
}
724
- // btStarted() is not sufficient to indicate ESP_SPP_INIT_EVT is complete
725
- if (_isInitializing) {
726
- int retry = 10 ;
727
- do {
728
- delay (500 );
729
- log_i (" waiting for initialization to complete..." );
730
- } while (!_isInitialized && retry-- > 0 );
721
+ if (!btStarted ()) {
722
+ log_e (" BT is not initialized. Call begin() first" );
723
+ return false ;
731
724
}
732
- if (!_isInitialized) {
725
+ TickType_t xTicksToWait = timeout / portTICK_PERIOD_MS;
726
+ if ((xEventGroupWaitBits (_spp_event_group, SPP_RUNNING, pdFALSE, pdTRUE, xTicksToWait) & SPP_RUNNING)) {
727
+ return true ;
728
+ } else {
733
729
log_e (" Timeout waiting for bt initialization to complete" );
734
- return false ;
730
+ return false ;
735
731
}
736
- return true ;
737
- }
732
+
733
+ }
738
734
#endif
0 commit comments