Skip to content
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

fix: BLEAdvertising methods return error codes #11154

Merged
merged 4 commits into from
Mar 27, 2025
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
34 changes: 19 additions & 15 deletions libraries/BLE/src/BLEAdvertising.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,36 +183,38 @@ void BLEAdvertising::setScanFilter(bool scanRequestWhitelistOnly, bool connectWh
* @brief Set the advertisement data that is to be published in a regular advertisement.
* @param [in] advertisementData The data to be advertised.
*/
void BLEAdvertising::setAdvertisementData(BLEAdvertisementData &advertisementData) {
bool BLEAdvertising::setAdvertisementData(BLEAdvertisementData &advertisementData) {
log_v(">> setAdvertisementData");
esp_err_t errRc = ::esp_ble_gap_config_adv_data_raw((uint8_t *)advertisementData.getPayload().c_str(), advertisementData.getPayload().length());
if (errRc != ESP_OK) {
log_e("esp_ble_gap_config_adv_data_raw: %d %s", errRc, GeneralUtils::errorToString(errRc));
}
m_customAdvData = true; // Set the flag that indicates we are using custom advertising data.
log_v("<< setAdvertisementData");
return ESP_OK == errRc;
} // setAdvertisementData

/**
* @brief Set the advertisement data that is to be published in a scan response.
* @param [in] advertisementData The data to be advertised.
*/
void BLEAdvertising::setScanResponseData(BLEAdvertisementData &advertisementData) {
bool BLEAdvertising::setScanResponseData(BLEAdvertisementData &advertisementData) {
log_v(">> setScanResponseData");
esp_err_t errRc = ::esp_ble_gap_config_scan_rsp_data_raw((uint8_t *)advertisementData.getPayload().c_str(), advertisementData.getPayload().length());
if (errRc != ESP_OK) {
log_e("esp_ble_gap_config_scan_rsp_data_raw: %d %s", errRc, GeneralUtils::errorToString(errRc));
}
m_customScanResponseData = true; // Set the flag that indicates we are using custom scan response data.
log_v("<< setScanResponseData");
return ESP_OK == errRc;
} // setScanResponseData

/**
* @brief Start advertising.
* Start advertising.
* @return N/A.
*/
void BLEAdvertising::start() {
bool BLEAdvertising::start() {
log_v(">> start: customAdvData: %d, customScanResponseData: %d", m_customAdvData, m_customScanResponseData);

// We have a vector of service UUIDs that we wish to advertise. In order to use the
Expand All @@ -225,7 +227,7 @@ void BLEAdvertising::start() {
m_advData.p_service_uuid = (uint8_t *)malloc(m_advData.service_uuid_len);
if (!m_advData.p_service_uuid) {
log_e(">> start failed: out of memory");
return;
return false;
}

uint8_t *p = m_advData.p_service_uuid;
Expand All @@ -250,7 +252,7 @@ void BLEAdvertising::start() {
errRc = ::esp_ble_gap_config_adv_data(&m_advData);
if (errRc != ESP_OK) {
log_e("<< esp_ble_gap_config_adv_data: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
return false;
}
}

Expand All @@ -266,7 +268,7 @@ void BLEAdvertising::start() {
errRc = ::esp_ble_gap_config_adv_data(&m_scanRespData);
if (errRc != ESP_OK) {
log_e("<< esp_ble_gap_config_adv_data (Scan response): rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
return false;
}
}

Expand All @@ -279,24 +281,26 @@ void BLEAdvertising::start() {
errRc = ::esp_ble_gap_start_advertising(&m_advParams);
if (errRc != ESP_OK) {
log_e("<< esp_ble_gap_start_advertising: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
} else {
log_v("<< start");
}
log_v("<< start");
return ESP_OK == errRc;
} // start

/**
* @brief Stop advertising.
* Stop advertising.
* @return N/A.
*/
void BLEAdvertising::stop() {
bool BLEAdvertising::stop() {
log_v(">> stop");
esp_err_t errRc = ::esp_ble_gap_stop_advertising();
if (errRc != ESP_OK) {
log_e("esp_ble_gap_stop_advertising: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
} else {
log_v("<< stop");
}
log_v("<< stop");
return ESP_OK == errRc;
} // stop

/**
Expand All @@ -305,17 +309,17 @@ void BLEAdvertising::stop() {
* @param [in] Bluetooth address type.
* Set BLE address.
*/

void BLEAdvertising::setDeviceAddress(esp_bd_addr_t addr, esp_ble_addr_type_t type) {
bool BLEAdvertising::setDeviceAddress(esp_bd_addr_t addr, esp_ble_addr_type_t type) {
log_v(">> setPrivateAddress");

m_advParams.own_addr_type = type;
esp_err_t errRc = esp_ble_gap_set_rand_addr((uint8_t *)addr);
if (errRc != ESP_OK) {
log_e("esp_ble_gap_set_rand_addr: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
} else {
log_v("<< setPrivateAddress");
}
log_v("<< setPrivateAddress");
return ESP_OK == errRc;
} // setPrivateAddress

/**
Expand Down
10 changes: 5 additions & 5 deletions libraries/BLE/src/BLEAdvertising.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ class BLEAdvertising {
bool removeServiceUUID(int index);
bool removeServiceUUID(BLEUUID serviceUUID);
bool removeServiceUUID(const char *serviceUUID);
void start();
void stop();
bool start();
bool stop();
void setAppearance(uint16_t appearance);
void setAdvertisementType(esp_ble_adv_type_t adv_type);
void setAdvertisementChannelMap(esp_ble_adv_channel_t channel_map);
void setMaxInterval(uint16_t maxinterval);
void setMinInterval(uint16_t mininterval);
void setAdvertisementData(BLEAdvertisementData &advertisementData);
bool setAdvertisementData(BLEAdvertisementData &advertisementData);
void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly);
void setScanResponseData(BLEAdvertisementData &advertisementData);
bool setScanResponseData(BLEAdvertisementData &advertisementData);
void setPrivateAddress(esp_ble_addr_type_t type = BLE_ADDR_TYPE_RANDOM);
void setDeviceAddress(esp_bd_addr_t addr, esp_ble_addr_type_t type = BLE_ADDR_TYPE_RANDOM);
bool setDeviceAddress(esp_bd_addr_t addr, esp_ble_addr_type_t type = BLE_ADDR_TYPE_RANDOM);

void handleGAPEvent(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
void setMinPreferred(uint16_t);
Expand Down
Loading