From 34fb6b8b874e1b7bf29f13a52d23615d9033882e Mon Sep 17 00:00:00 2001 From: h2zero Date: Tue, 19 Nov 2024 15:43:50 -0700 Subject: [PATCH] Add overloads for `NimBLEAdvertising::setServiceData` * Added `NimBLEAdvertising::setServiceData(const NimBLEUUID& uuid, const uint8_t* data, size_t length)` overload. * Added `NimBLEAdvertising::setServiceData(const NimBLEUUID& uuid, const std::vector& data)` overload. --- src/NimBLEAdvertising.cpp | 35 ++++++++++++++++++++++++++++------- src/NimBLEAdvertising.h | 2 ++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/NimBLEAdvertising.cpp b/src/NimBLEAdvertising.cpp index 10ccaf6..a3757ae 100644 --- a/src/NimBLEAdvertising.cpp +++ b/src/NimBLEAdvertising.cpp @@ -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(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(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(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; } @@ -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& 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(data.data()), data.length()); +} // setServiceData + /** * @brief Set the type of connectable mode to advertise. * @param [in] mode The connectable mode: diff --git a/src/NimBLEAdvertising.h b/src/NimBLEAdvertising.h index ca03dd0..4b96a9b 100644 --- a/src/NimBLEAdvertising.h +++ b/src/NimBLEAdvertising.h @@ -98,9 +98,11 @@ class NimBLEAdvertising { void setManufacturerData(const std::string& data); void setManufacturerData(const std::vector& 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& data); void setAdvertisingInterval(uint16_t interval); void setMaxInterval(uint16_t maxInterval); void setMinInterval(uint16_t minInterval);