Skip to content

fix(esp32): Added a timeout option to the BLEClient's connect function #9005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions libraries/BLE/src/BLEClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,23 @@ bool BLEClient::connect(BLEAdvertisedDevice* device) {
return connect(address, type);
}

/**
* Add overloaded function to ease connect to peer device with not public address
*/
bool BLEClient::connectTimeout(BLEAdvertisedDevice* device, uint32_t timeoutMs) {
BLEAddress address = device->getAddress();
esp_ble_addr_type_t type = device->getAddressType();
return connect(address, type, timeoutMs);
}

/**
* @brief Connect to the partner (BLE Server).
* @param [in] address The address of the partner.
* @param [in] type The type of the address.
* @param [in] timeoutMs The number of milliseconds to wait for the connection to complete.
* @return True on success.
*/
bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) {
bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type, uint32_t timeoutMs) {
log_v(">> connect(%s)", address.toString().c_str());

// We need the connection handle that we get from registering the application. We register the app
Expand Down Expand Up @@ -142,9 +153,10 @@ bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) {
return false;
}

rc = m_semaphoreOpenEvt.wait("connect"); // Wait for the connection to complete.
bool got_sem = m_semaphoreOpenEvt.timedWait("connect", timeoutMs); // Wait for the connection to complete.
rc = m_semaphoreOpenEvt.value();
// check the status of the connection and cleanup in case of failure
if (rc != ESP_GATT_OK) {
if (!got_sem || rc != ESP_GATT_OK) {
BLEDevice::removePeerDevice(m_appId, true);
esp_ble_gattc_app_unregister(m_gattc_if);
m_gattc_if = ESP_GATT_IF_NONE;
Expand Down
3 changes: 2 additions & 1 deletion libraries/BLE/src/BLEClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class BLEClient {
~BLEClient();

bool connect(BLEAdvertisedDevice* device);
bool connect(BLEAddress address, esp_ble_addr_type_t type = BLE_ADDR_TYPE_PUBLIC); // Connect to the remote BLE Server
bool connectTimeout(BLEAdvertisedDevice* device, uint32_t timeoutMS = portMAX_DELAY);
bool connect(BLEAddress address, esp_ble_addr_type_t type = BLE_ADDR_TYPE_PUBLIC, uint32_t timeoutMS = portMAX_DELAY); // Connect to the remote BLE Server
void disconnect(); // Disconnect from the remote BLE Server
BLEAddress getPeerAddress(); // Get the address of the remote BLE Server
int getRssi(); // Get the RSSI of the remote BLE Server
Expand Down