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