Skip to content

Commit 5ecb60d

Browse files
committed
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<uint8_t>& data)` overload.
1 parent 59b7e3e commit 5ecb60d

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

src/NimBLEAdvertising.cpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,31 +195,32 @@ void NimBLEAdvertising::setURI(const std::string& uri) {
195195
* @brief Set the service data advertised for the UUID.
196196
* @param [in] uuid The UUID the service data belongs to.
197197
* @param [in] data The data to advertise.
198+
* @param [in] length The length of the data.
198199
* @note If data length is 0 the service data will not be advertised.
199200
*/
200-
void NimBLEAdvertising::setServiceData(const NimBLEUUID& uuid, const std::string& data) {
201+
void NimBLEAdvertising::setServiceData(const NimBLEUUID& uuid, const uint8_t* data, size_t length) {
201202
switch (uuid.bitSize()) {
202203
case 16: {
203204
std::vector<uint8_t>(uuid.getValue(), uuid.getValue() + 2).swap(m_svcData16);
204-
m_svcData16.insert(m_svcData16.end(), data.begin(), data.end());
205+
m_svcData16.insert(m_svcData16.end(), data, data + length);
205206
m_advData.svc_data_uuid16 = &m_svcData16[0];
206-
m_advData.svc_data_uuid16_len = (data.length() > 0) ? m_svcData16.size() : 0;
207+
m_advData.svc_data_uuid16_len = (length > 0) ? m_svcData16.size() : 0;
207208
break;
208209
}
209210

210211
case 32: {
211212
std::vector<uint8_t>(uuid.getValue(), uuid.getValue() + 4).swap(m_svcData32);
212-
m_svcData32.insert(m_svcData32.end(), data.begin(), data.end());
213+
m_svcData32.insert(m_svcData32.end(), data, data + length);
213214
m_advData.svc_data_uuid32 = &m_svcData32[0];
214-
m_advData.svc_data_uuid32_len = (data.length() > 0) ? m_svcData32.size() : 0;
215+
m_advData.svc_data_uuid32_len = (length > 0) ? m_svcData32.size() : 0;
215216
break;
216217
}
217218

218219
case 128: {
219220
std::vector<uint8_t>(uuid.getValue(), uuid.getValue() + 16).swap(m_svcData128);
220-
m_svcData128.insert(m_svcData128.end(), data.begin(), data.end());
221+
m_svcData128.insert(m_svcData128.end(), data, data + length);
221222
m_advData.svc_data_uuid128 = &m_svcData128[0];
222-
m_advData.svc_data_uuid128_len = (data.length() > 0) ? m_svcData128.size() : 0;
223+
m_advData.svc_data_uuid128_len = (length > 0) ? m_svcData128.size() : 0;
223224
break;
224225
}
225226

@@ -230,6 +231,26 @@ void NimBLEAdvertising::setServiceData(const NimBLEUUID& uuid, const std::string
230231
m_advDataSet = false;
231232
} // setServiceData
232233

234+
/**
235+
* @brief Set the service data advertised for the UUID.
236+
* @param [in] uuid The UUID the service data belongs to.
237+
* @param [in] data The data to advertise.
238+
* @note If data length is 0 the service data will not be advertised.
239+
*/
240+
void NimBLEAdvertising::setServiceData(const NimBLEUUID& uuid, const std::vector<uint8_t>& data) {
241+
setServiceData(uuid, data.data(), data.size());
242+
} // setServiceData
243+
244+
/**
245+
* @brief Set the service data advertised for the UUID.
246+
* @param [in] uuid The UUID the service data belongs to.
247+
* @param [in] data The data to advertise.
248+
* @note If data length is 0 the service data will not be advertised.
249+
*/
250+
void NimBLEAdvertising::setServiceData(const NimBLEUUID& uuid, const std::string& data) {
251+
setServiceData(uuid, reinterpret_cast<const uint8_t*>(data.data()), data.length());
252+
} // setServiceData
253+
233254
/**
234255
* @brief Set the type of advertisement to use.
235256
* @param [in] adv_type:

src/NimBLEAdvertising.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ class NimBLEAdvertising {
9898
void setManufacturerData(const std::string& data);
9999
void setManufacturerData(const std::vector<uint8_t>& data);
100100
void setURI(const std::string& uri);
101+
void setServiceData(const NimBLEUUID& uuid, const uint8_t* data, size_t length);
101102
void setServiceData(const NimBLEUUID& uuid, const std::string& data);
103+
void setServiceData(const NimBLEUUID& uuid, const std::vector<uint8_t>& data);
102104
void setAdvertisementType(uint8_t adv_type);
103105
void setAdvertisingInterval(uint16_t interval);
104106
void setMaxInterval(uint16_t maxInterval);

0 commit comments

Comments
 (0)