Skip to content

Commit 1f406c6

Browse files
authored
Extacts correct Eddyston Service Data
1 parent a42a513 commit 1f406c6

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

libraries/BLE/examples/BLE_Beacon_Scanner/BLE_Beacon_Scanner.ino

+25-11
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
#include <BLEEddystoneTLM.h>
1515
#include <BLEBeacon.h>
1616

17-
#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00) >> 8) + (((x)&0xFF) << 8))
18-
1917
int scanTime = 5; //In seconds
2018
BLEScan *pBLEScan;
2119

@@ -69,23 +67,35 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
6967
}
7068

7169
uint8_t *payLoad = advertisedDevice.getPayload();
70+
// search for Eddystone Service Data in the advertising payload
71+
// *payload shall point to eddystone data or to its end when not found
72+
const uint8_t serviceDataEddystone[3] = {0x16, 0xAA, 0xFE}; // it has Eddystone BLE UUID
73+
const size_t payLoadLen = advertisedDevice.getPayloadLength();
74+
uint8_t *payLoadEnd = payLoad + payLoadLen - 1; // address of the end of payLoad space
75+
while (payLoad < payLoadEnd) {
76+
if (payLoad[1] == serviceDataEddystone[0] && payLoad[2] == serviceDataEddystone[1] && payLoad[3] == serviceDataEddystone[2]) {
77+
// found!
78+
payLoad += 4;
79+
break;
80+
}
81+
payLoad += *payLoad + 1; // payLoad[0] has the field Length
82+
}
7283

73-
BLEUUID checkUrlUUID = (uint16_t)0xfeaa;
74-
75-
if (advertisedDevice.getServiceUUID().equals(checkUrlUUID))
84+
if (payLoad < payLoadEnd) // Eddystone Service Data and respective BLE UUID were found
7685
{
77-
if (payLoad[11] == 0x10)
86+
if (*payLoad == 0x10)
7887
{
7988
Serial.println("Found an EddystoneURL beacon!");
8089
BLEEddystoneURL foundEddyURL = BLEEddystoneURL();
81-
std::string eddyContent((char *)&payLoad[11]); // incomplete EddystoneURL struct!
90+
std::string eddyContent((char *)payLoad); // incomplete EddystoneURL struct!
8291

8392
foundEddyURL.setData(eddyContent);
8493
std::string bareURL = foundEddyURL.getURL();
8594
if (bareURL[0] == 0x00)
8695
{
87-
size_t payLoadLen = advertisedDevice.getPayloadLength();
96+
// dumps all bytes in advertising payload
8897
Serial.println("DATA-->");
98+
uint8_t *payLoad = advertisedDevice.getPayload();
8999
for (int idx = 0; idx < payLoadLen; idx++)
90100
{
91101
Serial.printf("0x%08X ", payLoad[idx]);
@@ -98,16 +108,20 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
98108
Serial.printf("Decoded URL: %s\n", foundEddyURL.getDecodedURL().c_str());
99109
Serial.printf("TX power %d\n", foundEddyURL.getPower());
100110
Serial.println("\n");
101-
}
102-
if (advertisedDevice.getPayloadLength() >= 22 && payLoad[22] == 0x20)
111+
}
112+
else if (*payLoad == 0x20)
103113
{
104114
Serial.println("Found an EddystoneTLM beacon!");
115+
105116
BLEEddystoneTLM eddystoneTLM;
106-
eddystoneTLM.setData(std::string((char*)payLoad+22, advertisedDevice.getPayloadLength() - 22));
117+
eddystoneTLM.setData(std::string((char*)payLoad, 14));
107118
Serial.printf("Reported battery voltage: %dmV\n", eddystoneTLM.getVolt());
108119
Serial.printf("Reported temperature: %.2f°C (raw data=0x%04X)\n", eddystoneTLM.getTemp(), eddystoneTLM.getRawTemp());
109120
Serial.printf("Reported advertise count: %d\n", eddystoneTLM.getCount());
110121
Serial.printf("Reported time since last reboot: %ds\n", eddystoneTLM.getTime());
122+
Serial.println("\n");
123+
Serial.print(eddystoneTLM.toString().c_str());
124+
Serial.println("\n");
111125
}
112126
}
113127
}

0 commit comments

Comments
 (0)