Skip to content

Commit

Permalink
Add overloads for NimBLEAdvertising::setServiceData
Browse files Browse the repository at this point in the history
* Added `NimBLEAdvertising::setServiceData(const NimBLEUUID& uuid, const uint8_t* data, size_t length)` overload.
* Added `NimBLEAdvertising::setServiceData(const NimBLEUUID& uuid, const std::vector<uint8_t>& data)` overload.
  • Loading branch information
h2zero committed Nov 21, 2024
1 parent 7dc77ed commit 8a1326b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/NimBLEAdvertising.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,31 +191,32 @@ void NimBLEAdvertising::setURI(const std::string& uri) {
* @brief Set the service data advertised for the UUID.
* @param [in] uuid The UUID the service data belongs to.
* @param [in] data The data to advertise.
* @param [in] length The length of the data.
* @note If data length is 0 the service data will not be advertised.
*/
void NimBLEAdvertising::setServiceData(const NimBLEUUID& uuid, const std::string& data) {
void NimBLEAdvertising::setServiceData(const NimBLEUUID& uuid, const uint8_t* data, size_t length) {
switch (uuid.bitSize()) {
case 16: {
std::vector<uint8_t>(uuid.getValue(), uuid.getValue() + 2).swap(m_svcData16);
m_svcData16.insert(m_svcData16.end(), data.begin(), data.end());
m_svcData16.insert(m_svcData16.end(), data, data + length);
m_advData.svc_data_uuid16 = &m_svcData16[0];
m_advData.svc_data_uuid16_len = (data.length() > 0) ? m_svcData16.size() : 0;
m_advData.svc_data_uuid16_len = (length > 0) ? m_svcData16.size() : 0;
break;
}

case 32: {
std::vector<uint8_t>(uuid.getValue(), uuid.getValue() + 4).swap(m_svcData32);
m_svcData32.insert(m_svcData32.end(), data.begin(), data.end());
m_svcData32.insert(m_svcData32.end(), data, data + length);
m_advData.svc_data_uuid32 = &m_svcData32[0];
m_advData.svc_data_uuid32_len = (data.length() > 0) ? m_svcData32.size() : 0;
m_advData.svc_data_uuid32_len = (length > 0) ? m_svcData32.size() : 0;
break;
}

case 128: {
std::vector<uint8_t>(uuid.getValue(), uuid.getValue() + 16).swap(m_svcData128);
m_svcData128.insert(m_svcData128.end(), data.begin(), data.end());
m_svcData128.insert(m_svcData128.end(), data, data + length);
m_advData.svc_data_uuid128 = &m_svcData128[0];
m_advData.svc_data_uuid128_len = (data.length() > 0) ? m_svcData128.size() : 0;
m_advData.svc_data_uuid128_len = (length > 0) ? m_svcData128.size() : 0;
break;
}

Expand All @@ -226,6 +227,26 @@ void NimBLEAdvertising::setServiceData(const NimBLEUUID& uuid, const std::string
m_advDataSet = false;
} // setServiceData

/**
* @brief Set the service data advertised for the UUID.
* @param [in] uuid The UUID the service data belongs to.
* @param [in] data The data to advertise.
* @note If data length is 0 the service data will not be advertised.
*/
void NimBLEAdvertising::setServiceData(const NimBLEUUID& uuid, const std::vector<uint8_t>& data) {
setServiceData(uuid, data.data(), data.size());
} // setServiceData

/**
* @brief Set the service data advertised for the UUID.
* @param [in] uuid The UUID the service data belongs to.
* @param [in] data The data to advertise.
* @note If data length is 0 the service data will not be advertised.
*/
void NimBLEAdvertising::setServiceData(const NimBLEUUID& uuid, const std::string& data) {
setServiceData(uuid, reinterpret_cast<const uint8_t*>(data.data()), data.length());
} // setServiceData

/**
* @brief Set the type of connectable mode to advertise.
* @param [in] mode The connectable mode:
Expand Down
2 changes: 2 additions & 0 deletions src/NimBLEAdvertising.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ class NimBLEAdvertising {
void setManufacturerData(const std::string& data);
void setManufacturerData(const std::vector<uint8_t>& data);
void setURI(const std::string& uri);
void setServiceData(const NimBLEUUID& uuid, const uint8_t* data, size_t length);
void setServiceData(const NimBLEUUID& uuid, const std::string& data);
void setConnectableMode(uint8_t mode);
void setDiscoverableMode(uint8_t mode);
void setServiceData(const NimBLEUUID& uuid, const std::vector<uint8_t>& data);
void setAdvertisingInterval(uint16_t interval);
void setMaxInterval(uint16_t maxInterval);
void setMinInterval(uint16_t minInterval);
Expand Down

0 comments on commit 8a1326b

Please sign in to comment.