Skip to content

Commit c6efba2

Browse files
author
Victor Tchistiak
committed
20190916 - fix build issues and implement geoup events for status verification.
1 parent bc06174 commit c6efba2

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

libraries/BluetoothSerial/src/BluetoothSerial.cpp

Lines changed: 22 additions & 26 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 30
57+
#define INQ_NUM_RSPS 0
5858
static esp_bd_addr_t _peer_bd_addr;
5959
static char _remote_name[ESP_BT_GAP_MAX_BDNAME_LEN + 1];
6060
static bool _isRemoteAddressSet;
@@ -63,8 +63,6 @@ static esp_bt_pin_code_t _pin_code;
6363
static int _pin_len;
6464
static bool _isPinSet;
6565
static bool _enableSSP;
66-
static bool _isInitializing;
67-
static bool _isInitialized;
6866

6967
#define SPP_RUNNING 0x01
7068
#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)
225223
esp_spp_start_srv(ESP_SPP_SEC_NONE, ESP_SPP_ROLE_SLAVE, 0, _spp_server_name);
226224
}
227225
xEventGroupSetBits(_spp_event_group, SPP_RUNNING);
228-
_isInitialized = true;
229-
_isInitializing = false;
230-
231226
break;
232227

233228
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
320315
log_i("ESP_BT_GAP_DISC_RES_EVT");
321316
for (int i = 0; i < param->disc_res.num_prop; i++){
322317
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];
324319
if (param->disc_res.prop[i].type == ESP_BT_GAP_DEV_PROP_EIR
325320
&& get_name_from_eir((uint8_t*)param->disc_res.prop[i].val, peer_bdname, &peer_bdname_len)){
326321
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
401396

402397
static bool _init_bt(const char *deviceName)
403398
{
404-
_isInitializing = false;
405399
if(!_spp_event_group){
406400
_spp_event_group = xEventGroupCreate();
407401
if(!_spp_event_group){
@@ -500,14 +494,11 @@ static bool _init_bt(const char *deviceName)
500494
log_e("set cod failed");
501495
return false;
502496
}
503-
_isInitializing = true;
504497
return true;
505498
}
506499

507500
static bool _stop_bt()
508501
{
509-
_isInitialized = false;
510-
_isInitializing = false;
511502
if (btStarted()){
512503
if(_spp_client)
513504
esp_spp_disconnect(_spp_client);
@@ -711,28 +702,33 @@ bool BluetoothSerial::disconnect() {
711702
return false;
712703
}
713704

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+
}
715713
return _spp_client != 0;
716714
}
717715

718-
bool BluetoothSerial::isReady(bool checkMaster)
719-
{
716+
bool BluetoothSerial::isReady(bool checkMaster, int timeout) {
720717
if (checkMaster && !_isMaster) {
721718
log_e("Master mode is not active. Call begin(localName, true) to enable Master mode");
722719
return false;
723720
}
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;
731724
}
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 {
733729
log_e("Timeout waiting for bt initialization to complete");
734-
return false;
730+
return false;
735731
}
736-
return true;
737-
}
732+
733+
}
738734
#endif

libraries/BluetoothSerial/src/BluetoothSerial.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class BluetoothSerial: public Stream
4646
bool connect(String remoteName);
4747
bool connect(uint8_t remoteAddress[]);
4848
bool connect();
49-
bool connected();
50-
bool isReady(bool checkMaster=false);
49+
bool connected(int timeout=0);
50+
bool isReady(bool checkMaster=false, int timeout=0);
5151
bool disconnect();
5252

5353
private:

0 commit comments

Comments
 (0)