@@ -55,6 +55,7 @@ static esp_spp_cb_t * custom_spp_callback = NULL;
55
55
56
56
#define INQ_LEN 30
57
57
#define INQ_NUM_RSPS 0
58
+ #define READY_TIMEOUT 5000
58
59
static esp_bd_addr_t _peer_bd_addr;
59
60
static char _remote_name[ESP_BT_GAP_MAX_BDNAME_LEN + 1 ];
60
61
static bool _isRemoteAddressSet;
@@ -67,6 +68,7 @@ static bool _enableSSP;
67
68
#define SPP_RUNNING 0x01
68
69
#define SPP_CONNECTED 0x02
69
70
#define SPP_CONGESTED 0x04
71
+ #define SPP_DISCONNECTED 0x08
70
72
71
73
typedef struct {
72
74
size_t len;
@@ -241,7 +243,8 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
241
243
secondConnectionAttempt = false ;
242
244
} else {
243
245
_spp_client = 0 ;
244
- }
246
+ xEventGroupSetBits (_spp_event_group, SPP_DISCONNECTED);
247
+ }
245
248
xEventGroupClearBits (_spp_event_group, SPP_CONNECTED);
246
249
log_i (" ESP_SPP_CLOSE_EVT" );
247
250
break ;
@@ -314,32 +317,38 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
314
317
case ESP_BT_GAP_DISC_RES_EVT:
315
318
log_i (" ESP_BT_GAP_DISC_RES_EVT" );
316
319
for (int i = 0 ; i < param->disc_res .num_prop ; i++){
317
- uint8_t peer_bdname_len;
318
- char peer_bdname[ESP_BT_GAP_MAX_BDNAME_LEN + 1 ];
319
- if (param->disc_res .prop [i].type == ESP_BT_GAP_DEV_PROP_EIR
320
- && get_name_from_eir ((uint8_t *)param->disc_res .prop [i].val , peer_bdname, &peer_bdname_len)){
321
- log_v (" ESP_BT_GAP_DISC_RES_EVT : EIR : %s" , peer_bdname);
322
- if (strlen (_remote_name) == peer_bdname_len
323
- && strncmp (peer_bdname, _remote_name, peer_bdname_len) == 0 ) {
324
- log_v (" ESP_BT_GAP_DISC_RES_EVT : SPP_DISCOVERY_EIR : %s" , peer_bdname);
325
- _isRemoteAddressSet = true ;
326
- memcpy (_peer_bd_addr, param->disc_res .bda , ESP_BD_ADDR_LEN);
327
- esp_spp_start_discovery (_peer_bd_addr);
328
- esp_bt_gap_cancel_discovery ();
329
- }
330
- } else if (param->disc_res .prop [i].type == ESP_BT_GAP_DEV_PROP_BDNAME) {
331
- peer_bdname_len = param->disc_res .prop [i].len ;
332
- memcpy (peer_bdname, param->disc_res .prop [i].val , peer_bdname_len);
333
- peer_bdname[peer_bdname_len] = ' \0 ' ;
334
- log_v (" ESP_BT_GAP_DISC_RES_EVT : BDNAME : %s" , peer_bdname);
335
- if (strlen (_remote_name) == peer_bdname_len
336
- && strncmp (peer_bdname, _remote_name, peer_bdname_len) == 0 ) {
337
- log_v (" ESP_BT_GAP_DISC_RES_EVT : SPP_DISCOVERY_BDNAME : %s" , peer_bdname);
338
- _isRemoteAddressSet = true ;
339
- memcpy (_peer_bd_addr, param->disc_res .bda , ESP_BD_ADDR_LEN);
340
- esp_spp_start_discovery (_peer_bd_addr);
341
- esp_bt_gap_cancel_discovery ();
342
- }
320
+ static uint8_t peer_bdname_len;
321
+ static char peer_bdname[ESP_BT_GAP_MAX_BDNAME_LEN + 1 ];
322
+ switch (param->disc_res .prop [i].type ) {
323
+ case ESP_BT_GAP_DEV_PROP_EIR:
324
+ if (get_name_from_eir ((uint8_t *)param->disc_res .prop [i].val , peer_bdname, &peer_bdname_len)) {
325
+ log_v (" ESP_BT_GAP_DISC_RES_EVT : EIR : %s : %d" , peer_bdname, peer_bdname_len);
326
+ if (strlen (_remote_name) == peer_bdname_len
327
+ && strncmp (peer_bdname, _remote_name, peer_bdname_len) == 0 ) {
328
+ log_v (" ESP_BT_GAP_DISC_RES_EVT : SPP_DISCOVERY_EIR : %s" , peer_bdname, peer_bdname_len);
329
+ _isRemoteAddressSet = true ;
330
+ memcpy (_peer_bd_addr, param->disc_res .bda , ESP_BD_ADDR_LEN);
331
+ esp_spp_start_discovery (_peer_bd_addr);
332
+ esp_bt_gap_cancel_discovery ();
333
+ }
334
+ }
335
+ break ;
336
+ case ESP_BT_GAP_DEV_PROP_BDNAME:
337
+ peer_bdname_len = param->disc_res .prop [i].len ;
338
+ memcpy (peer_bdname, param->disc_res .prop [i].val , peer_bdname_len);
339
+ peer_bdname_len--; // the len include 0 terminator
340
+ log_v (" ESP_BT_GAP_DISC_RES_EVT : BDNAME : %s : %d" , peer_bdname, peer_bdname_len);
341
+ if (strlen (_remote_name) == peer_bdname_len
342
+ && strncmp (peer_bdname, _remote_name, peer_bdname_len) == 0 ) {
343
+ log_v (" ESP_BT_GAP_DISC_RES_EVT : SPP_DISCOVERY_BDNAME : %s" , peer_bdname);
344
+ _isRemoteAddressSet = true ;
345
+ memcpy (_peer_bd_addr, param->disc_res .bda , ESP_BD_ADDR_LEN);
346
+ esp_spp_start_discovery (_peer_bd_addr);
347
+ esp_bt_gap_cancel_discovery ();
348
+ }
349
+ break ;
350
+ default :
351
+ break ;
343
352
}
344
353
}
345
354
break ;
@@ -646,7 +655,7 @@ bool BluetoothSerial::setPin(const char *pin) {
646
655
647
656
bool BluetoothSerial::connect (String remoteName)
648
657
{
649
- if (!isReady (true )) return false ;
658
+ if (!isReady (true , READY_TIMEOUT )) return false ;
650
659
if (remoteName && remoteName.length () < 1 ) {
651
660
log_e (" No remote name is provided" );
652
661
return false ;
@@ -661,7 +670,7 @@ bool BluetoothSerial::connect(String remoteName)
661
670
662
671
bool BluetoothSerial::connect (uint8_t remoteAddress[])
663
672
{
664
- if (!isReady (true )) return false ;
673
+ if (!isReady (true , READY_TIMEOUT )) return false ;
665
674
if (!remoteAddress) {
666
675
log_e (" No remote address is provided" );
667
676
return false ;
@@ -675,7 +684,7 @@ bool BluetoothSerial::connect(uint8_t remoteAddress[])
675
684
676
685
bool BluetoothSerial::connect ()
677
686
{
678
- if (!isReady (true )) return false ;
687
+ if (!isReady (true , READY_TIMEOUT )) return false ;
679
688
if (_isRemoteAddressSet){
680
689
// use resolved or set address first
681
690
log_i (" master : remoteAddress" );
@@ -689,11 +698,18 @@ bool BluetoothSerial::connect()
689
698
return false ;
690
699
}
691
700
692
- bool BluetoothSerial::disconnect () {
701
+ bool BluetoothSerial::disconnect (int timeout ) {
693
702
if (_spp_client) {
694
703
flush ();
695
704
log_i (" disconnecting" );
696
- return (esp_spp_disconnect (_spp_client) == ESP_OK);
705
+ if (esp_spp_disconnect (_spp_client) == ESP_OK) {
706
+ if (timeout) {
707
+ TickType_t xTicksToWait = timeout / portTICK_PERIOD_MS;
708
+ if ((xEventGroupWaitBits (_spp_event_group, SPP_DISCONNECTED, pdFALSE, pdTRUE, xTicksToWait) & SPP_DISCONNECTED)) {
709
+ return true ;
710
+ }
711
+ }
712
+ }
697
713
}
698
714
return false ;
699
715
}
@@ -703,7 +719,8 @@ bool BluetoothSerial::connected(int timeout) {
703
719
if ((xEventGroupWaitBits (_spp_event_group, SPP_CONNECTED, pdFALSE, pdTRUE, xTicksToWait) & SPP_CONNECTED)) {
704
720
return true ;
705
721
}
706
- log_e (" Timeout waiting for connected state" );
722
+ if (timeout)
723
+ log_e (" Timeout waiting for connected state" );
707
724
return false ;
708
725
}
709
726
@@ -720,7 +737,8 @@ bool BluetoothSerial::isReady(bool checkMaster, int timeout) {
720
737
if ((xEventGroupWaitBits (_spp_event_group, SPP_RUNNING, pdFALSE, pdTRUE, xTicksToWait) & SPP_RUNNING)) {
721
738
return true ;
722
739
}
723
- log_e (" Timeout waiting for bt initialization to complete" );
740
+ if (timeout)
741
+ log_e (" Timeout waiting for bt initialization to complete" );
724
742
return false ;
725
743
726
744
}
0 commit comments