|
| 1 | +/* |
| 2 | + Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp |
| 3 | + Ported to Arduino ESP32 by Evandro Copercini |
| 4 | + Changed to a beacon scanner to report iBeacon, EddystoneURL and EddystoneTLM beacons by beegee-tokyo |
| 5 | +*/ |
| 6 | + |
| 7 | +#include <Arduino.h> |
| 8 | + |
| 9 | +#include <BLEDevice.h> |
| 10 | +#include <BLEUtils.h> |
| 11 | +#include <BLEScan.h> |
| 12 | +#include <BLEAdvertisedDevice.h> |
| 13 | +#include <BLEEddystoneURL.h> |
| 14 | +#include <BLEEddystoneTLM.h> |
| 15 | +#include <BLEBeacon.h> |
| 16 | + |
| 17 | +#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00) >> 8) + (((x)&0xFF) << 8)) |
| 18 | + |
| 19 | +int scanTime = 5; //In seconds |
| 20 | +BLEScan *pBLEScan; |
| 21 | + |
| 22 | +class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks |
| 23 | +{ |
| 24 | + void onResult(BLEAdvertisedDevice advertisedDevice) |
| 25 | + { |
| 26 | + if (advertisedDevice.haveName()) |
| 27 | + { |
| 28 | + Serial.print("Device name: "); |
| 29 | + Serial.println(advertisedDevice.getName().c_str()); |
| 30 | + Serial.println(""); |
| 31 | + } |
| 32 | + |
| 33 | + if (advertisedDevice.haveServiceUUID()) |
| 34 | + { |
| 35 | + BLEUUID devUUID = advertisedDevice.getServiceUUID(); |
| 36 | + Serial.print("Found ServiceUUID: "); |
| 37 | + Serial.println(devUUID.toString().c_str()); |
| 38 | + Serial.println(""); |
| 39 | + } |
| 40 | + else |
| 41 | + { |
| 42 | + if (advertisedDevice.haveManufacturerData() == true) |
| 43 | + { |
| 44 | + std::string strManufacturerData = advertisedDevice.getManufacturerData(); |
| 45 | + |
| 46 | + uint8_t cManufacturerData[100]; |
| 47 | + strManufacturerData.copy((char *)cManufacturerData, strManufacturerData.length(), 0); |
| 48 | + |
| 49 | + if (strManufacturerData.length() == 25 && cManufacturerData[0] == 0x4C && cManufacturerData[1] == 0x00) |
| 50 | + { |
| 51 | + Serial.println("Found an iBeacon!"); |
| 52 | + BLEBeacon oBeacon = BLEBeacon(); |
| 53 | + oBeacon.setData(strManufacturerData); |
| 54 | + Serial.printf("iBeacon Frame\n"); |
| 55 | + Serial.printf("ID: %04X Major: %d Minor: %d UUID: %s Power: %d\n", oBeacon.getManufacturerId(), ENDIAN_CHANGE_U16(oBeacon.getMajor()), ENDIAN_CHANGE_U16(oBeacon.getMinor()), oBeacon.getProximityUUID().toString().c_str(), oBeacon.getSignalPower()); |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + Serial.println("Found another manufacturers beacon!"); |
| 60 | + Serial.printf("strManufacturerData: %d ", strManufacturerData.length()); |
| 61 | + for (int i = 0; i < strManufacturerData.length(); i++) |
| 62 | + { |
| 63 | + Serial.printf("[%X]", cManufacturerData[i]); |
| 64 | + } |
| 65 | + Serial.printf("\n"); |
| 66 | + } |
| 67 | + } |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + uint8_t *payLoad = advertisedDevice.getPayload(); |
| 72 | + |
| 73 | + BLEUUID checkUrlUUID = (uint16_t)0xfeaa; |
| 74 | + |
| 75 | + if (advertisedDevice.getServiceUUID().equals(checkUrlUUID)) |
| 76 | + { |
| 77 | + if (payLoad[11] == 0x10) |
| 78 | + { |
| 79 | + Serial.println("Found an EddystoneURL beacon!"); |
| 80 | + BLEEddystoneURL foundEddyURL = BLEEddystoneURL(); |
| 81 | + std::string eddyContent((char *)&payLoad[11]); // incomplete EddystoneURL struct! |
| 82 | + |
| 83 | + foundEddyURL.setData(eddyContent); |
| 84 | + std::string bareURL = foundEddyURL.getURL(); |
| 85 | + if (bareURL[0] == 0x00) |
| 86 | + { |
| 87 | + size_t payLoadLen = advertisedDevice.getPayloadLength(); |
| 88 | + Serial.println("DATA-->"); |
| 89 | + for (int idx = 0; idx < payLoadLen; idx++) |
| 90 | + { |
| 91 | + Serial.printf("0x%08X ", payLoad[idx]); |
| 92 | + } |
| 93 | + Serial.println("\nInvalid Data"); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + Serial.printf("Found URL: %s\n", foundEddyURL.getURL().c_str()); |
| 98 | + Serial.printf("Decoded URL: %s\n", foundEddyURL.getDecodedURL().c_str()); |
| 99 | + Serial.printf("TX power %d\n", foundEddyURL.getPower()); |
| 100 | + Serial.println("\n"); |
| 101 | + } |
| 102 | + else if (payLoad[11] == 0x20) |
| 103 | + { |
| 104 | + Serial.println("Found an EddystoneTLM beacon!"); |
| 105 | + BLEEddystoneTLM foundEddyURL = BLEEddystoneTLM(); |
| 106 | + std::string eddyContent((char *)&payLoad[11]); // incomplete EddystoneURL struct! |
| 107 | + |
| 108 | + eddyContent = "01234567890123"; |
| 109 | + |
| 110 | + for (int idx = 0; idx < 14; idx++) |
| 111 | + { |
| 112 | + eddyContent[idx] = payLoad[idx + 11]; |
| 113 | + } |
| 114 | + |
| 115 | + foundEddyURL.setData(eddyContent); |
| 116 | + Serial.printf("Reported battery voltage: %dmV\n", foundEddyURL.getVolt()); |
| 117 | + Serial.printf("Reported temperature from TLM class: %.2fC\n", (double)foundEddyURL.getTemp()); |
| 118 | + int temp = (int)payLoad[16] + (int)(payLoad[15] << 8); |
| 119 | + float calcTemp = temp / 256.0f; |
| 120 | + Serial.printf("Reported temperature from data: %.2fC\n", calcTemp); |
| 121 | + Serial.printf("Reported advertise count: %d\n", foundEddyURL.getCount()); |
| 122 | + Serial.printf("Reported time since last reboot: %ds\n", foundEddyURL.getTime()); |
| 123 | + Serial.println("\n"); |
| 124 | + Serial.print(foundEddyURL.toString().c_str()); |
| 125 | + Serial.println("\n"); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +}; |
| 130 | + |
| 131 | +void setup() |
| 132 | +{ |
| 133 | + Serial.begin(115200); |
| 134 | + Serial.println("Scanning..."); |
| 135 | + |
| 136 | + BLEDevice::init(""); |
| 137 | + pBLEScan = BLEDevice::getScan(); //create new scan |
| 138 | + pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); |
| 139 | + pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster |
| 140 | + pBLEScan->setInterval(100); |
| 141 | + pBLEScan->setWindow(99); // less or equal setInterval value |
| 142 | +} |
| 143 | + |
| 144 | +void loop() |
| 145 | +{ |
| 146 | + // put your main code here, to run repeatedly: |
| 147 | + BLEScanResults foundDevices = pBLEScan->start(scanTime, false); |
| 148 | + Serial.print("Devices found: "); |
| 149 | + Serial.println(foundDevices.getCount()); |
| 150 | + Serial.println("Scan done!"); |
| 151 | + pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory |
| 152 | + delay(2000); |
| 153 | +} |
0 commit comments