Skip to content

Commit d03c74c

Browse files
committed
Refactor advertising raw data
Add raw data class. When an AdvertisingData object has a rawData parameter set, the only advertised data will be the rawData parameter itself.
1 parent f79e492 commit d03c74c

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/BLEAdvertisingData.cpp

+20-9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
BLEAdvertisingData::BLEAdvertisingData() :
2525
_dataLength(0),
26+
_rawData(NULL),
2627
_rawDataLength(0),
2728
_advertisedServiceUuid(NULL),
2829
_manufacturerData(NULL),
@@ -162,12 +163,22 @@ bool BLEAdvertisingData::setLocalName(const char *localName)
162163

163164
bool BLEAdvertisingData::setRawData(const uint8_t* data, int length)
164165
{
165-
bool success = updateRemainingLength(length);
166-
if (success) {
167-
_rawData = data;
168-
_rawDataLength = length;
166+
if (length > MAX_AD_DATA_LENGTH) {
167+
length = MAX_AD_DATA_LENGTH;
169168
}
170-
return success;
169+
_rawData = data;
170+
_rawDataLength = length;
171+
return true;
172+
}
173+
174+
bool BLEAdvertisingData::setRawData(const BLEAdvertisingRawData& rawData)
175+
{
176+
_rawData = rawData.data;
177+
_rawDataLength = rawData.length;
178+
if (_rawDataLength > MAX_AD_DATA_LENGTH) {
179+
_rawDataLength = MAX_AD_DATA_LENGTH;
180+
}
181+
return true;
171182
}
172183

173184
bool BLEAdvertisingData::setFlags(uint8_t flags)
@@ -186,6 +197,10 @@ bool BLEAdvertisingData::updateData()
186197
bool success = true;
187198
// Reset data
188199
_dataLength = 0;
200+
// If rawData is present, then only rawData is inserted in the advertising packet
201+
if (_rawData && _rawDataLength) {
202+
return addRawData(_rawData, _rawDataLength);
203+
}
189204
// Try to add flags into the current advertising packet
190205
if (_hasFlags) {
191206
success &= addFlags(_flags);
@@ -210,10 +225,6 @@ bool BLEAdvertisingData::updateData()
210225
if (_localName) {
211226
success &= addLocalName(_localName);
212227
}
213-
// Try to add Raw data
214-
if (_rawDataLength) {
215-
success &= addRawData(_rawData, _rawDataLength);
216-
}
217228
return success;
218229
}
219230

src/BLEAdvertisingData.h

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ enum BLEAdField {
4646
BLEAdFieldLast
4747
};
4848

49+
struct BLEAdvertisingRawData {
50+
uint8_t data[MAX_AD_DATA_LENGTH];
51+
int length;
52+
};
53+
4954
class BLEAdvertisingData {
5055
public:
5156
BLEAdvertisingData();
@@ -63,6 +68,7 @@ class BLEAdvertisingData {
6368
bool setLocalName(const char *localName);
6469
bool setAdvertisedServiceData(uint16_t uuid, const uint8_t data[], int length);
6570
bool setRawData(const uint8_t* data, int length);
71+
bool setRawData(const BLEAdvertisingRawData& data);
6672
bool setFlags(uint8_t flags);
6773

6874
protected:

0 commit comments

Comments
 (0)