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.
This commit is contained in:
h-actsafe
2022-05-27 11:20:43 +02:00
committed by GitHub
parent 46a026a45d
commit 247bca8bda

View File

@ -921,6 +921,8 @@ bool BluetoothSerial::setPin(const char *pin) {
bool BluetoothSerial::connect(String remoteName) bool BluetoothSerial::connect(String remoteName)
{ {
bool retval = false;
if (!isReady(true, READY_TIMEOUT)) return false; if (!isReady(true, READY_TIMEOUT)) return false;
if (remoteName && remoteName.length() < 1) { if (remoteName && remoteName.length() < 1) {
log_e("No remote name is provided"); log_e("No remote name is provided");
@ -942,9 +944,12 @@ bool BluetoothSerial::connect(String remoteName)
#endif #endif
xEventGroupClearBits(_spp_event_group, SPP_CLOSED); xEventGroupClearBits(_spp_event_group, SPP_CLOSED);
if (esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, INQ_LEN, INQ_NUM_RSPS) == ESP_OK) { if (esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, INQ_LEN, INQ_NUM_RSPS) == ESP_OK) {
return waitForConnect(SCAN_TIMEOUT); retval = waitForConnect(SCAN_TIMEOUT);
} }
return false; if (retval == false) {
_isRemoteAddressSet = false;
}
return retval;
} }
/** /**
@ -958,6 +963,7 @@ bool BluetoothSerial::connect(String remoteName)
*/ */
bool BluetoothSerial::connect(uint8_t remoteAddress[], int channel, esp_spp_sec_t sec_mask, esp_spp_role_t role) bool BluetoothSerial::connect(uint8_t remoteAddress[], int channel, esp_spp_sec_t sec_mask, esp_spp_role_t role)
{ {
bool retval = false;
if (!isReady(true, READY_TIMEOUT)) return false; if (!isReady(true, READY_TIMEOUT)) return false;
if (!remoteAddress) { if (!remoteAddress) {
log_e("No remote address is provided"); log_e("No remote address is provided");
@ -981,10 +987,10 @@ bool BluetoothSerial::connect(uint8_t remoteAddress[], int channel, esp_spp_sec_
#endif #endif
if(esp_spp_connect(sec_mask, role, channel, _peer_bd_addr) != ESP_OK ) { if(esp_spp_connect(sec_mask, role, channel, _peer_bd_addr) != ESP_OK ) {
log_e("spp connect failed"); log_e("spp connect failed");
return false; retval = false;
} } else {
bool rc=waitForConnect(READY_TIMEOUT); retval = waitForConnect(READY_TIMEOUT);
if(rc) { if(retval) {
log_i("connected"); log_i("connected");
} else { } else {
if(this->isClosed()) { if(this->isClosed()) {
@ -993,12 +999,15 @@ bool BluetoothSerial::connect(uint8_t remoteAddress[], int channel, esp_spp_sec_
log_e("connect timed out after %dms", READY_TIMEOUT); log_e("connect timed out after %dms", READY_TIMEOUT);
} }
} }
return rc;
} }
if (esp_spp_start_discovery(_peer_bd_addr) == ESP_OK) { } else if (esp_spp_start_discovery(_peer_bd_addr) == ESP_OK) {
return waitForConnect(READY_TIMEOUT); retval = waitForConnect(READY_TIMEOUT);
}
if (!retval) {
_isRemoteAddressSet = false;
} }
return false; return retval;
} }
bool BluetoothSerial::connect() bool BluetoothSerial::connect()