|
3 | 3 | Ported to Arduino ESP32 by Evandro Copercini
|
4 | 4 | */
|
5 | 5 |
|
6 |
| -/** NimBLE differences highlighted in comment blocks **/ |
7 |
| - |
8 |
| -/*******original******** |
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 |
| - |
18 | 6 | #include <Arduino.h>
|
19 |
| - |
20 | 7 | #include <NimBLEDevice.h>
|
21 | 8 | #include <NimBLEAdvertisedDevice.h>
|
22 |
| -#include "NimBLEEddystoneURL.h" |
23 | 9 | #include "NimBLEEddystoneTLM.h"
|
24 | 10 | #include "NimBLEBeacon.h"
|
25 | 11 |
|
26 |
| -#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00) >> 8) + (((x)&0xFF) << 8)) |
27 |
| - |
28 |
| -int scanTime = 5 * 1000; //In milliseconds |
29 |
| -BLEScan *pBLEScan; |
| 12 | +#define ENDIAN_CHANGE_U16(x) ((((x) & 0xFF00) >> 8) + (((x) & 0xFF) << 8)) |
30 | 13 |
|
31 |
| -class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks |
32 |
| -{ |
33 |
| - /*** Only a reference to the advertised device is passed now |
34 |
| - void onResult(BLEAdvertisedDevice advertisedDevice) { **/ |
35 |
| - void onResult(BLEAdvertisedDevice *advertisedDevice) |
36 |
| - { |
37 |
| - if (advertisedDevice->haveName()) |
38 |
| - { |
39 |
| - Serial.print("Device name: "); |
40 |
| - Serial.println(advertisedDevice->getName().c_str()); |
41 |
| - Serial.println(""); |
42 |
| - } |
| 14 | +int scanTime = 5 * 1000; // In milliseconds |
| 15 | +NimBLEScan* pBLEScan; |
43 | 16 |
|
44 |
| - if (advertisedDevice->haveServiceUUID()) |
45 |
| - { |
46 |
| - BLEUUID devUUID = advertisedDevice->getServiceUUID(); |
47 |
| - Serial.print("Found ServiceUUID: "); |
48 |
| - Serial.println(devUUID.toString().c_str()); |
49 |
| - Serial.println(""); |
50 |
| - } |
51 |
| - else |
52 |
| - { |
53 |
| - if (advertisedDevice->haveManufacturerData() == true) |
54 |
| - { |
55 |
| - std::string strManufacturerData = advertisedDevice->getManufacturerData(); |
56 |
| - |
57 |
| - uint8_t cManufacturerData[100]; |
58 |
| - strManufacturerData.copy((char *)cManufacturerData, strManufacturerData.length(), 0); |
59 |
| - |
60 |
| - if (strManufacturerData.length() == 25 && cManufacturerData[0] == 0x4C && cManufacturerData[1] == 0x00) |
61 |
| - { |
62 |
| - Serial.println("Found an iBeacon!"); |
63 |
| - BLEBeacon oBeacon = BLEBeacon(); |
64 |
| - oBeacon.setData(strManufacturerData); |
65 |
| - Serial.printf("iBeacon Frame\n"); |
66 |
| - 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()); |
67 |
| - } |
68 |
| - else |
69 |
| - { |
70 |
| - Serial.println("Found another manufacturers beacon!"); |
71 |
| - Serial.printf("strManufacturerData: %d ", strManufacturerData.length()); |
72 |
| - for (int i = 0; i < strManufacturerData.length(); i++) |
73 |
| - { |
74 |
| - Serial.printf("[%X]", cManufacturerData[i]); |
75 |
| - } |
76 |
| - Serial.printf("\n"); |
77 |
| - } |
| 17 | +class ScanCallbacks : public NimBLEScanCallbacks { |
| 18 | + void onResult(const NimBLEAdvertisedDevice* advertisedDevice) override { |
| 19 | + if (advertisedDevice->haveName()) { |
| 20 | + Serial.print("Device name: "); |
| 21 | + Serial.println(advertisedDevice->getName().c_str()); |
| 22 | + Serial.println(""); |
78 | 23 | }
|
79 |
| - return; |
80 |
| - } |
81 | 24 |
|
82 |
| - BLEUUID eddyUUID = (uint16_t)0xfeaa; |
83 |
| - |
84 |
| - if (advertisedDevice->getServiceUUID().equals(eddyUUID)) |
85 |
| - { |
86 |
| - std::string serviceData = advertisedDevice->getServiceData(eddyUUID); |
87 |
| - if (serviceData[0] == 0x10) |
88 |
| - { |
89 |
| - Serial.println("Found an EddystoneURL beacon!"); |
90 |
| - BLEEddystoneURL foundEddyURL = BLEEddystoneURL(); |
91 |
| - |
92 |
| - foundEddyURL.setData(serviceData); |
93 |
| - std::string bareURL = foundEddyURL.getURL(); |
94 |
| - if (bareURL[0] == 0x00) |
95 |
| - { |
96 |
| - Serial.println("DATA-->"); |
97 |
| - for (int idx = 0; idx < serviceData.length(); idx++) |
98 |
| - { |
99 |
| - Serial.printf("0x%08X ", serviceData[idx]); |
| 25 | + if (advertisedDevice->haveServiceUUID()) { |
| 26 | + NimBLEUUID devUUID = advertisedDevice->getServiceUUID(); |
| 27 | + Serial.print("Found ServiceUUID: "); |
| 28 | + Serial.println(devUUID.toString().c_str()); |
| 29 | + Serial.println(""); |
| 30 | + } else if (advertisedDevice->haveManufacturerData() == true) { |
| 31 | + std::string strManufacturerData = advertisedDevice->getManufacturerData(); |
| 32 | + if (strManufacturerData.length() == 25 && strManufacturerData[0] == 0x4C && strManufacturerData[1] == 0x00) { |
| 33 | + Serial.println("Found an iBeacon!"); |
| 34 | + NimBLEBeacon oBeacon = NimBLEBeacon(); |
| 35 | + oBeacon.setData(reinterpret_cast<const uint8_t*>(strManufacturerData.data()), strManufacturerData.length()); |
| 36 | + Serial.printf("iBeacon Frame\n"); |
| 37 | + Serial.printf("ID: %04X Major: %d Minor: %d UUID: %s Power: %d\n", |
| 38 | + oBeacon.getManufacturerId(), |
| 39 | + ENDIAN_CHANGE_U16(oBeacon.getMajor()), |
| 40 | + ENDIAN_CHANGE_U16(oBeacon.getMinor()), |
| 41 | + oBeacon.getProximityUUID().toString().c_str(), |
| 42 | + oBeacon.getSignalPower()); |
| 43 | + } else { |
| 44 | + Serial.println("Found another manufacturers beacon!"); |
| 45 | + Serial.printf("strManufacturerData: %d ", strManufacturerData.length()); |
| 46 | + for (int i = 0; i < strManufacturerData.length(); i++) { |
| 47 | + Serial.printf("[%X]", strManufacturerData[i]); |
| 48 | + } |
| 49 | + Serial.printf("\n"); |
100 | 50 | }
|
101 |
| - Serial.println("\nInvalid Data"); |
102 | 51 | return;
|
103 |
| - } |
104 |
| - |
105 |
| - Serial.printf("Found URL: %s\n", foundEddyURL.getURL().c_str()); |
106 |
| - Serial.printf("Decoded URL: %s\n", foundEddyURL.getDecodedURL().c_str()); |
107 |
| - Serial.printf("TX power %d\n", foundEddyURL.getPower()); |
108 |
| - Serial.println("\n"); |
109 | 52 | }
|
110 |
| - else if (serviceData[0] == 0x20) |
111 |
| - { |
112 |
| - Serial.println("Found an EddystoneTLM beacon!"); |
113 |
| - BLEEddystoneTLM foundEddyURL = BLEEddystoneTLM(); |
114 |
| - foundEddyURL.setData(serviceData); |
115 | 53 |
|
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)serviceData[5] + (int)(serviceData[4] << 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"); |
| 54 | + NimBLEUUID eddyUUID = (uint16_t)0xfeaa; |
| 55 | + |
| 56 | + if (advertisedDevice->getServiceUUID().equals(eddyUUID)) { |
| 57 | + std::string serviceData = advertisedDevice->getServiceData(eddyUUID); |
| 58 | + if (serviceData[0] == 0x20) { |
| 59 | + Serial.println("Found an EddystoneTLM beacon!"); |
| 60 | + NimBLEEddystoneTLM foundEddyTLM = NimBLEEddystoneTLM(); |
| 61 | + foundEddyTLM.setData(reinterpret_cast<const uint8_t*>(serviceData.data()), serviceData.length()); |
| 62 | + |
| 63 | + Serial.printf("Reported battery voltage: %dmV\n", foundEddyTLM.getVolt()); |
| 64 | + Serial.printf("Reported temperature from TLM class: %.2fC\n", (double)foundEddyTLM.getTemp()); |
| 65 | + int temp = (int)serviceData[5] + (int)(serviceData[4] << 8); |
| 66 | + float calcTemp = temp / 256.0f; |
| 67 | + Serial.printf("Reported temperature from data: %.2fC\n", calcTemp); |
| 68 | + Serial.printf("Reported advertise count: %d\n", foundEddyTLM.getCount()); |
| 69 | + Serial.printf("Reported time since last reboot: %ds\n", foundEddyTLM.getTime()); |
| 70 | + Serial.println("\n"); |
| 71 | + Serial.print(foundEddyTLM.toString().c_str()); |
| 72 | + Serial.println("\n"); |
| 73 | + } |
126 | 74 | }
|
127 |
| - } |
128 | 75 | }
|
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->setScanCallbacks(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 |
| 76 | +} scanCallbacks; |
| 77 | + |
| 78 | +void setup() { |
| 79 | + Serial.begin(115200); |
| 80 | + Serial.println("Scanning..."); |
| 81 | + |
| 82 | + NimBLEDevice::init("Beacon-scanner"); |
| 83 | + pBLEScan = BLEDevice::getScan(); |
| 84 | + pBLEScan->setScanCallbacks(&scanCallbacks); |
| 85 | + pBLEScan->setActiveScan(true); |
| 86 | + pBLEScan->setInterval(100); |
| 87 | + pBLEScan->setWindow(100); |
142 | 88 | }
|
143 | 89 |
|
144 |
| -void loop() |
145 |
| -{ |
146 |
| - // put your main code here, to run repeatedly: |
147 |
| - BLEScanResults foundDevices = pBLEScan->getResults(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); |
| 90 | +void loop() { |
| 91 | + NimBLEScanResults foundDevices = pBLEScan->getResults(scanTime, false); |
| 92 | + Serial.print("Devices found: "); |
| 93 | + Serial.println(foundDevices.getCount()); |
| 94 | + Serial.println("Scan done!"); |
| 95 | + pBLEScan->clearResults(); // delete results scan buffer to release memory |
| 96 | + delay(2000); |
153 | 97 | }
|
0 commit comments