22   Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp 
33   Ported to Arduino ESP32 by Evandro Copercini 
44   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 
56*/ 
67
78#include  < Arduino.h> 
@@ -38,10 +39,10 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
3839
3940      if  (advertisedDevice.haveManufacturerData () == true )
4041      {
41-         std::string  strManufacturerData = advertisedDevice.getManufacturerData ();
42+         String  strManufacturerData = advertisedDevice.getManufacturerData ();
4243
4344        uint8_t  cManufacturerData[100 ];
44-         strManufacturerData.copy (( char  *)cManufacturerData , strManufacturerData.length (),  0 );
45+         memcpy (cManufacturerData,  strManufacturerData.c_str () , strManufacturerData.length ());
4546
4647        if  (strManufacturerData.length () == 25  && cManufacturerData[0 ] == 0x4C  && cManufacturerData[1 ] == 0x00 )
4748        {
@@ -63,62 +64,34 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
6364        }
6465      }
6566
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" 
7775        }
78-         payLoad += *payLoad + 1 ;  //  payLoad[0] has the field Length
76+         Serial.printf (" \n " 
77+         Serial.printf (" Decoded URL: %s\n " getDecodedURL ().c_str ());
78+         Serial.printf (" EddystoneURL.getDecodedURL(): %s\n " getDecodedURL ().c_str ());
79+         Serial.printf (" TX power %d (Raw 0x%02X)\n " getPower (), EddystoneURL.getPower ());
80+         Serial.println (" \n " 
7981      }
8082
81-       if  (payLoad < payLoadEnd)  //  Eddystone Service Data and respective BLE UUID were found 
83+       if  (advertisedDevice. getFrameType () == BLE_EDDYSTONE_TLM_FRAME) 
8284      {
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 " 
98-             }
99-             Serial.println (" \n Invalid Data" 
100-             return ;
101-           }
102- 
103-           Serial.printf (" Found URL: %s\n " getURL ().c_str ());
104-           Serial.printf (" Decoded URL: %s\n " getDecodedURL ().c_str ());
105-           Serial.printf (" TX power %d\n " getPower ());
106-           Serial.println (" \n " 
107-         } 
108-         else  if  (*payLoad == 0x20 )
109-         {
11085          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 " getVolt ());
114-           Serial.printf (" Reported temperature: %.2f°C (raw data=0x%04X)\n " getTemp (), eddystoneTLM.getRawTemp ());
115-           Serial.printf (" Reported advertise count: %lu\n " getCount ());
116-           Serial.printf (" Reported time since last reboot: %lus\n " getTime ());
86+           BLEEddystoneTLM EddystoneTLM (&advertisedDevice);
87+           Serial.printf (" Reported battery voltage: %dmV\n " getVolt ());
88+           Serial.printf (" Reported temperature: %.2f°C (raw data=0x%04X)\n " getTemp (), EddystoneTLM.getRawTemp ());
89+           Serial.printf (" Reported advertise count: %lu\n " getCount ());
90+           Serial.printf (" Reported time since last reboot: %lus\n " getTime ());
11791          Serial.println (" \n " 
118-           Serial.print (eddystoneTLM .toString ().c_str ());
92+           Serial.print (EddystoneTLM .toString ().c_str ());
11993          Serial.println (" \n " 
12094        }
121-       }
12295    }
12396};
12497
0 commit comments