Skip to content

Commit c786489

Browse files
author
Victor Tchistiak
committed
20190916 - rework ESP_BT_GAP_DISC_RES_EVT, added SPP_DISCONNECTED bit for disconnect event. + timeout for disconnect()
1 parent 784e388 commit c786489

File tree

3 files changed

+63
-38
lines changed

3 files changed

+63
-38
lines changed

libraries/BluetoothSerial/examples/SerialToSerialBTM/SerialToSerialBTM.ino

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,16 @@ void setup() {
2727
//delay(2000);
2828
SerialBT.connect(address);
2929
//SerialBT.connect(name); //slow as it needs to resolve name to address first, but allows to connect to different devices with the same name
30-
while(!SerialBT.connected()) {delay(1000); }
31-
SerialBT.disconnect();
32-
while(SerialBT.connected()) {delay(1000); }
30+
if(SerialBT.connected(60*1000)) {
31+
Serial.println("Connected Succesfully!");
32+
} else {
33+
Serial.println("Not connected yet?");
34+
}
35+
if (SerialBT.disconnect(2000)) {
36+
Serial.println("Disconnected Succesfully!");
37+
} else {
38+
Serial.println("Not disconnected yet?");
39+
}
3340
SerialBT.connect();
3441
}
3542

libraries/BluetoothSerial/src/BluetoothSerial.cpp

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ static esp_spp_cb_t * custom_spp_callback = NULL;
5555

5656
#define INQ_LEN 30
5757
#define INQ_NUM_RSPS 0
58+
#define READY_TIMEOUT 5000
5859
static esp_bd_addr_t _peer_bd_addr;
5960
static char _remote_name[ESP_BT_GAP_MAX_BDNAME_LEN + 1];
6061
static bool _isRemoteAddressSet;
@@ -67,6 +68,7 @@ static bool _enableSSP;
6768
#define SPP_RUNNING 0x01
6869
#define SPP_CONNECTED 0x02
6970
#define SPP_CONGESTED 0x04
71+
#define SPP_DISCONNECTED 0x08
7072

7173
typedef struct {
7274
size_t len;
@@ -241,7 +243,8 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
241243
secondConnectionAttempt = false;
242244
} else {
243245
_spp_client = 0;
244-
}
246+
xEventGroupSetBits(_spp_event_group, SPP_DISCONNECTED);
247+
}
245248
xEventGroupClearBits(_spp_event_group, SPP_CONNECTED);
246249
log_i("ESP_SPP_CLOSE_EVT");
247250
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
314317
case ESP_BT_GAP_DISC_RES_EVT:
315318
log_i("ESP_BT_GAP_DISC_RES_EVT");
316319
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;
343352
}
344353
}
345354
break;
@@ -646,7 +655,7 @@ bool BluetoothSerial::setPin(const char *pin) {
646655

647656
bool BluetoothSerial::connect(String remoteName)
648657
{
649-
if (!isReady(true)) return false;
658+
if (!isReady(true, READY_TIMEOUT)) return false;
650659
if (remoteName && remoteName.length() < 1) {
651660
log_e("No remote name is provided");
652661
return false;
@@ -661,7 +670,7 @@ bool BluetoothSerial::connect(String remoteName)
661670

662671
bool BluetoothSerial::connect(uint8_t remoteAddress[])
663672
{
664-
if (!isReady(true)) return false;
673+
if (!isReady(true, READY_TIMEOUT)) return false;
665674
if (!remoteAddress) {
666675
log_e("No remote address is provided");
667676
return false;
@@ -675,7 +684,7 @@ bool BluetoothSerial::connect(uint8_t remoteAddress[])
675684

676685
bool BluetoothSerial::connect()
677686
{
678-
if (!isReady(true)) return false;
687+
if (!isReady(true, READY_TIMEOUT)) return false;
679688
if (_isRemoteAddressSet){
680689
// use resolved or set address first
681690
log_i("master : remoteAddress");
@@ -689,11 +698,18 @@ bool BluetoothSerial::connect()
689698
return false;
690699
}
691700

692-
bool BluetoothSerial::disconnect() {
701+
bool BluetoothSerial::disconnect(int timeout) {
693702
if (_spp_client) {
694703
flush();
695704
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+
}
697713
}
698714
return false;
699715
}
@@ -703,7 +719,8 @@ bool BluetoothSerial::connected(int timeout) {
703719
if((xEventGroupWaitBits(_spp_event_group, SPP_CONNECTED, pdFALSE, pdTRUE, xTicksToWait) & SPP_CONNECTED)) {
704720
return true;
705721
}
706-
log_e("Timeout waiting for connected state");
722+
if (timeout)
723+
log_e("Timeout waiting for connected state");
707724
return false;
708725
}
709726

@@ -720,7 +737,8 @@ bool BluetoothSerial::isReady(bool checkMaster, int timeout) {
720737
if((xEventGroupWaitBits(_spp_event_group, SPP_RUNNING, pdFALSE, pdTRUE, xTicksToWait) & SPP_RUNNING)) {
721738
return true;
722739
}
723-
log_e("Timeout waiting for bt initialization to complete");
740+
if (timeout)
741+
log_e("Timeout waiting for bt initialization to complete");
724742
return false;
725743

726744
}

libraries/BluetoothSerial/src/BluetoothSerial.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class BluetoothSerial: public Stream
4848
bool connect();
4949
bool connected(int timeout=0);
5050
bool isReady(bool checkMaster=false, int timeout=0);
51-
bool disconnect();
51+
bool disconnect(int timeout=0);
5252

5353
private:
5454
String local_name;

0 commit comments

Comments
 (0)