Skip to content

Commit 247bca8

Browse files
BluetoothSerial : Re-set _isRemoteAddressSet to false if connect() fails. (#6728)
The internal _isRemoteAddressSet variable is set to true when calling connect() functions. If connecting fails _isRemoteAddressSet needs to be re-set to false, otherwise other functions, such as discover() will fail without clear error messages.
1 parent 46a026a commit 247bca8

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

Diff for: libraries/BluetoothSerial/src/BluetoothSerial.cpp

+19-10
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,8 @@ bool BluetoothSerial::setPin(const char *pin) {
921921

922922
bool BluetoothSerial::connect(String remoteName)
923923
{
924+
bool retval = false;
925+
924926
if (!isReady(true, READY_TIMEOUT)) return false;
925927
if (remoteName && remoteName.length() < 1) {
926928
log_e("No remote name is provided");
@@ -942,9 +944,12 @@ bool BluetoothSerial::connect(String remoteName)
942944
#endif
943945
xEventGroupClearBits(_spp_event_group, SPP_CLOSED);
944946
if (esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, INQ_LEN, INQ_NUM_RSPS) == ESP_OK) {
945-
return waitForConnect(SCAN_TIMEOUT);
947+
retval = waitForConnect(SCAN_TIMEOUT);
946948
}
947-
return false;
949+
if (retval == false) {
950+
_isRemoteAddressSet = false;
951+
}
952+
return retval;
948953
}
949954

950955
/**
@@ -958,6 +963,7 @@ bool BluetoothSerial::connect(String remoteName)
958963
*/
959964
bool BluetoothSerial::connect(uint8_t remoteAddress[], int channel, esp_spp_sec_t sec_mask, esp_spp_role_t role)
960965
{
966+
bool retval = false;
961967
if (!isReady(true, READY_TIMEOUT)) return false;
962968
if (!remoteAddress) {
963969
log_e("No remote address is provided");
@@ -981,10 +987,10 @@ bool BluetoothSerial::connect(uint8_t remoteAddress[], int channel, esp_spp_sec_
981987
#endif
982988
if(esp_spp_connect(sec_mask, role, channel, _peer_bd_addr) != ESP_OK ) {
983989
log_e("spp connect failed");
984-
return false;
985-
}
986-
bool rc=waitForConnect(READY_TIMEOUT);
987-
if(rc) {
990+
retval = false;
991+
} else {
992+
retval = waitForConnect(READY_TIMEOUT);
993+
if(retval) {
988994
log_i("connected");
989995
} else {
990996
if(this->isClosed()) {
@@ -993,12 +999,15 @@ bool BluetoothSerial::connect(uint8_t remoteAddress[], int channel, esp_spp_sec_
993999
log_e("connect timed out after %dms", READY_TIMEOUT);
9941000
}
9951001
}
996-
return rc;
9971002
}
998-
if (esp_spp_start_discovery(_peer_bd_addr) == ESP_OK) {
999-
return waitForConnect(READY_TIMEOUT);
1003+
} else if (esp_spp_start_discovery(_peer_bd_addr) == ESP_OK) {
1004+
retval = waitForConnect(READY_TIMEOUT);
1005+
}
1006+
1007+
if (!retval) {
1008+
_isRemoteAddressSet = false;
10001009
}
1001-
return false;
1010+
return retval;
10021011
}
10031012

10041013
bool BluetoothSerial::connect()

0 commit comments

Comments
 (0)