2
2
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
3
3
Ported to Arduino ESP32 by Evandro Copercini
4
4
Changed to a beacon scanner to report iBeacon, EddystoneURL and EddystoneTLM beacons by beegee-tokyo
5
+ Upgraded Eddystone part by Tomas Pilny on Feb 20, 2023
5
6
*/
6
7
7
8
#include < Arduino.h>
@@ -38,10 +39,10 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
38
39
39
40
if (advertisedDevice.haveManufacturerData () == true )
40
41
{
41
- std::string strManufacturerData = advertisedDevice.getManufacturerData ();
42
+ String strManufacturerData = advertisedDevice.getManufacturerData ();
42
43
43
44
uint8_t cManufacturerData[100 ];
44
- strManufacturerData.copy (( char *)cManufacturerData , strManufacturerData.length (), 0 );
45
+ memcpy (cManufacturerData, strManufacturerData.c_str () , strManufacturerData.length ());
45
46
46
47
if (strManufacturerData.length () == 25 && cManufacturerData[0 ] == 0x4C && cManufacturerData[1 ] == 0x00 )
47
48
{
@@ -63,62 +64,34 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
63
64
}
64
65
}
65
66
66
- uint8_t *payLoad = advertisedDevice.getPayload ();
67
- // search for Eddystone Service Data in the advertising payload
68
- // *payload shall point to eddystone data or to its end when not found
69
- const uint8_t serviceDataEddystone[3 ] = {0x16 , 0xAA , 0xFE }; // it has Eddystone BLE UUID
70
- const size_t payLoadLen = advertisedDevice.getPayloadLength ();
71
- uint8_t *payLoadEnd = payLoad + payLoadLen - 1 ; // address of the end of payLoad space
72
- while (payLoad < payLoadEnd) {
73
- if (payLoad[1 ] == serviceDataEddystone[0 ] && payLoad[2 ] == serviceDataEddystone[1 ] && payLoad[3 ] == serviceDataEddystone[2 ]) {
74
- // found!
75
- payLoad += 4 ;
76
- break ;
67
+ if (advertisedDevice.getFrameType () == BLE_EDDYSTONE_URL_FRAME)
68
+ {
69
+ Serial.println (" Found an EddystoneURL beacon!" );
70
+ BLEEddystoneURL EddystoneURL = BLEEddystoneURL (&advertisedDevice);
71
+ Serial.printf (" URL bytes: 0x" );
72
+ String url = EddystoneURL.getURL ();
73
+ for (auto byte : url){
74
+ Serial.printf (" %02X" , byte);
77
75
}
78
- payLoad += *payLoad + 1 ; // payLoad[0] has the field Length
76
+ Serial.printf (" \n " );
77
+ Serial.printf (" Decoded URL: %s\n " , EddystoneURL.getDecodedURL ().c_str ());
78
+ Serial.printf (" EddystoneURL.getDecodedURL(): %s\n " , EddystoneURL.getDecodedURL ().c_str ());
79
+ Serial.printf (" TX power %d (Raw 0x%02X)\n " , EddystoneURL.getPower (), EddystoneURL.getPower ());
80
+ Serial.println (" \n " );
79
81
}
80
82
81
- if (payLoad < payLoadEnd) // Eddystone Service Data and respective BLE UUID were found
83
+ if (advertisedDevice. getFrameType () == BLE_EDDYSTONE_TLM_FRAME)
82
84
{
83
- if (*payLoad == 0x10 )
84
- {
85
- Serial.println (" Found an EddystoneURL beacon!" );
86
- BLEEddystoneURL foundEddyURL = BLEEddystoneURL ();
87
- uint8_t URLLen = *(payLoad - 4 ) - 3 ; // Get Field Length less 3 bytes (type and UUID)
88
- foundEddyURL.setData (std::string ((char *)payLoad, URLLen));
89
- std::string bareURL = foundEddyURL.getURL ();
90
- if (bareURL[0 ] == 0x00 )
91
- {
92
- // dumps all bytes in advertising payload
93
- Serial.println (" DATA-->" );
94
- uint8_t *payLoad = advertisedDevice.getPayload ();
95
- for (int idx = 0 ; idx < payLoadLen; idx++)
96
- {
97
- Serial.printf (" 0x%02X " , payLoad[idx]);
98
- }
99
- Serial.println (" \n Invalid Data" );
100
- return ;
101
- }
102
-
103
- Serial.printf (" Found URL: %s\n " , foundEddyURL.getURL ().c_str ());
104
- Serial.printf (" Decoded URL: %s\n " , foundEddyURL.getDecodedURL ().c_str ());
105
- Serial.printf (" TX power %d\n " , foundEddyURL.getPower ());
106
- Serial.println (" \n " );
107
- }
108
- else if (*payLoad == 0x20 )
109
- {
110
85
Serial.println (" Found an EddystoneTLM beacon!" );
111
- BLEEddystoneTLM eddystoneTLM;
112
- eddystoneTLM.setData (std::string ((char *)payLoad, 14 ));
113
- Serial.printf (" Reported battery voltage: %dmV\n " , eddystoneTLM.getVolt ());
114
- Serial.printf (" Reported temperature: %.2f°C (raw data=0x%04X)\n " , eddystoneTLM.getTemp (), eddystoneTLM.getRawTemp ());
115
- Serial.printf (" Reported advertise count: %lu\n " , eddystoneTLM.getCount ());
116
- Serial.printf (" Reported time since last reboot: %lus\n " , eddystoneTLM.getTime ());
86
+ BLEEddystoneTLM EddystoneTLM (&advertisedDevice);
87
+ Serial.printf (" Reported battery voltage: %dmV\n " , EddystoneTLM.getVolt ());
88
+ Serial.printf (" Reported temperature: %.2f°C (raw data=0x%04X)\n " , EddystoneTLM.getTemp (), EddystoneTLM.getRawTemp ());
89
+ Serial.printf (" Reported advertise count: %lu\n " , EddystoneTLM.getCount ());
90
+ Serial.printf (" Reported time since last reboot: %lus\n " , EddystoneTLM.getTime ());
117
91
Serial.println (" \n " );
118
- Serial.print (eddystoneTLM .toString ().c_str ());
92
+ Serial.print (EddystoneTLM .toString ().c_str ());
119
93
Serial.println (" \n " );
120
94
}
121
- }
122
95
}
123
96
};
124
97
0 commit comments