Skip to content

Commit 530a2a1

Browse files
authored
Add generic advertisement 'type' functions (#575)
1 parent bc333cc commit 530a2a1

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/NimBLEAdvertisedDevice.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,24 @@ std::string NimBLEAdvertisedDevice::getURI() {
203203
return "";
204204
} // getURI
205205

206+
/**
207+
* @brief Get the data from any type available in the advertisement
208+
* @param [in] type The advertised data type BLE_HS_ADV_TYPE
209+
* @return The data available under the type `type`
210+
*/
211+
std::string NimBLEAdvertisedDevice::getPayloadByType(uint16_t type) {
212+
size_t data_loc = 0;
213+
214+
if(findAdvField(type, 0, &data_loc) > 0) {
215+
ble_hs_adv_field *field = (ble_hs_adv_field *)&m_payload[data_loc];
216+
if(field->length > 1) {
217+
return std::string((char*)field->value, field->length - 1);
218+
}
219+
}
220+
221+
return "";
222+
} // getPayloadByType
223+
206224

207225
/**
208226
* @brief Get the advertised name.
@@ -556,6 +574,14 @@ bool NimBLEAdvertisedDevice::haveURI() {
556574
return findAdvField(BLE_HS_ADV_TYPE_URI) > 0;
557575
} // haveURI
558576

577+
/**
578+
* @brief Does this advertisement have a adv type `type`?
579+
* @return True if there is a `type` present.
580+
*/
581+
bool NimBLEAdvertisedDevice::haveType(uint16_t type) {
582+
return findAdvField(type) > 0;
583+
}
584+
559585

560586
/**
561587
* @brief Does the advertisement contain a target address?

src/NimBLEAdvertisedDevice.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class NimBLEAdvertisedDevice {
5353
uint8_t getManufacturerDataCount();
5454
std::string getManufacturerData(uint8_t index = 0);
5555
std::string getURI();
56+
std::string getPayloadByType(uint16_t type);
5657

5758
/**
5859
* @brief A template to convert the service data to <type\>.
@@ -134,6 +135,7 @@ class NimBLEAdvertisedDevice {
134135
bool haveAdvInterval();
135136
bool haveTargetAddress();
136137
bool haveURI();
138+
bool haveType(uint16_t type);
137139
std::string toString();
138140
bool isConnectable();
139141
bool isLegacyAdvertisement();

0 commit comments

Comments
 (0)