Skip to content

Commit ad72ac3

Browse files
author
Victor Tchistiak
committed
20190916 - Limited scope and duration for the scan, log device address during scan in info mode as it is very difficult to find out sometimes. Fixed get_name_from_eir() not resetting the name when called.
1 parent d9dfb29 commit ad72ac3

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

libraries/BluetoothSerial/src/BluetoothSerial.cpp

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ static EventGroupHandle_t _spp_event_group = NULL;
5353
static boolean secondConnectionAttempt;
5454
static esp_spp_cb_t * custom_spp_callback = NULL;
5555

56-
#define INQ_LEN 30
57-
#define INQ_NUM_RSPS 0
56+
#define INQ_LEN 0x10
57+
#define INQ_NUM_RSPS 20
5858
#define READY_TIMEOUT 5000
5959
static esp_bd_addr_t _peer_bd_addr;
6060
static char _remote_name[ESP_BT_GAP_MAX_BDNAME_LEN + 1];
@@ -75,12 +75,29 @@ typedef struct {
7575
uint8_t data[];
7676
} spp_packet_t;
7777

78+
static char *bda2str(esp_bd_addr_t bda, char *str, size_t size)
79+
{
80+
if (bda == NULL || str == NULL || size < 18) {
81+
return NULL;
82+
}
83+
84+
uint8_t *p = bda;
85+
sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
86+
p[0], p[1], p[2], p[3], p[4], p[5]);
87+
return str;
88+
}
89+
7890
static bool get_name_from_eir(uint8_t *eir, char *bdname, uint8_t *bdname_len)
7991
{
8092
uint8_t *rmt_bdname = NULL;
8193
uint8_t rmt_bdname_len = 0;
8294

83-
if (!eir) {
95+
if (bdname) {
96+
*bdname_len = 0;
97+
*bdname = 0;
98+
}
99+
100+
if (!eir || !bdname || !bdname_len) {
84101
return false;
85102
}
86103

@@ -316,16 +333,18 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
316333
switch(event){
317334
case ESP_BT_GAP_DISC_RES_EVT:
318335
log_i("ESP_BT_GAP_DISC_RES_EVT");
336+
char bda_str[18];
337+
log_i("Scanned device: %s", bda2str(param->disc_res.bda, bda_str, 18));
319338
for (int i = 0; i < param->disc_res.num_prop; i++){
320339
uint8_t peer_bdname_len;
321340
char peer_bdname[ESP_BT_GAP_MAX_BDNAME_LEN + 1];
322341
switch(param->disc_res.prop[i].type) {
323-
case ESP_BT_GAP_DEV_PROP_EIR:
342+
case ESP_BT_GAP_DEV_PROP_EIR:
324343
if (get_name_from_eir((uint8_t*)param->disc_res.prop[i].val, peer_bdname, &peer_bdname_len)) {
325344
log_v("ESP_BT_GAP_DISC_RES_EVT : EIR : %s : %d", peer_bdname, peer_bdname_len);
326345
if (strlen(_remote_name) == peer_bdname_len
327346
&& 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);
347+
log_v("ESP_BT_GAP_DISC_RES_EVT : SPP_START_DISCOVERY_EIR : %s", peer_bdname, peer_bdname_len);
329348
_isRemoteAddressSet = true;
330349
memcpy(_peer_bd_addr, param->disc_res.bda, ESP_BD_ADDR_LEN);
331350
esp_spp_start_discovery(_peer_bd_addr);
@@ -340,13 +359,19 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
340359
log_v("ESP_BT_GAP_DISC_RES_EVT : BDNAME : %s : %d", peer_bdname, peer_bdname_len);
341360
if (strlen(_remote_name) == peer_bdname_len
342361
&& strncmp(peer_bdname, _remote_name, peer_bdname_len) == 0) {
343-
log_v("ESP_BT_GAP_DISC_RES_EVT : SPP_DISCOVERY_BDNAME : %s", peer_bdname);
362+
log_v("ESP_BT_GAP_DISC_RES_EVT : SPP_START_DISCOVERY_BDNAME : %s", peer_bdname);
344363
_isRemoteAddressSet = true;
345364
memcpy(_peer_bd_addr, param->disc_res.bda, ESP_BD_ADDR_LEN);
346365
esp_spp_start_discovery(_peer_bd_addr);
347366
esp_bt_gap_cancel_discovery();
348367
}
349368
break;
369+
case ESP_BT_GAP_DEV_PROP_COD:
370+
//log_i("ESP_BT_GAP_DEV_PROP_COD");
371+
break;
372+
case ESP_BT_GAP_DEV_PROP_RSSI:
373+
//log_i("ESP_BT_GAP_DEV_PROP_RSSI");
374+
break;
350375
default:
351376
break;
352377
}
@@ -665,6 +690,7 @@ bool BluetoothSerial::connect(String remoteName)
665690
_remote_name[ESP_BT_GAP_MAX_BDNAME_LEN] = 0;
666691
log_i("master : remoteName");
667692
// will first resolve name to address
693+
esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE);
668694
return (esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, INQ_LEN, INQ_NUM_RSPS) == ESP_OK);
669695
}
670696

@@ -692,6 +718,7 @@ bool BluetoothSerial::connect()
692718
} else if (_remote_name[0]) {
693719
log_i("master : remoteName");
694720
// will resolve name to address first - it may take a while
721+
esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE);
695722
return (esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, INQ_LEN, INQ_NUM_RSPS) == ESP_OK);
696723
}
697724
log_e("Neither Remote name nor address was provided");

0 commit comments

Comments
 (0)