|
| 1 | +#include <ArduinoBLE.h> |
| 2 | + |
| 3 | +void setup() { |
| 4 | + Serial.begin(9600); |
| 5 | + while (!Serial); |
| 6 | + |
| 7 | + // begin initialization |
| 8 | + if (!BLE.begin()) { |
| 9 | + Serial.println("starting BLE failed!"); |
| 10 | + |
| 11 | + while (1); |
| 12 | + } |
| 13 | + |
| 14 | + Serial.println("BLE Central scan"); |
| 15 | + |
| 16 | + // start scanning for peripheral |
| 17 | + BLE.scan(); |
| 18 | +} |
| 19 | + |
| 20 | +void loop() { |
| 21 | + // check if a peripheral has been discovered |
| 22 | + BLEDevice peripheral = BLE.available(); |
| 23 | + |
| 24 | + if (!peripheral) { |
| 25 | + return; |
| 26 | + } |
| 27 | + if (!peripheral.hasManufacturerData()) { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + String manufacturerData = peripheral.manufacturerData(); |
| 32 | + if (manufacturerData.length() < 25 * 2) { |
| 33 | + return; |
| 34 | + } |
| 35 | + if (manufacturerData.substring(0, 8) != "4c000215") { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + // Discovered an iBeacon |
| 40 | + Serial.println("Discovered an iBeacon"); |
| 41 | + Serial.println("-----------------------"); |
| 42 | + |
| 43 | + // UUID |
| 44 | + String id = manufacturerData.substring(8, 16) + "-"; |
| 45 | + id += manufacturerData.substring(16, 20) + "-"; |
| 46 | + id += manufacturerData.substring(20, 24) + "-"; |
| 47 | + id += manufacturerData.substring(24, 28) + "-"; |
| 48 | + id += manufacturerData.substring(28, 40); |
| 49 | + Serial.print("UUID: "); |
| 50 | + Serial.println(id); |
| 51 | + |
| 52 | + // Major ID |
| 53 | + char buf[5]; |
| 54 | + manufacturerData.substring(40, 44).toCharArray(buf, 5); |
| 55 | + int major = (int)strtol(buf, NULL, 16); |
| 56 | + Serial.print("Major ID: "); |
| 57 | + Serial.println(major); |
| 58 | + |
| 59 | + // Minor ID |
| 60 | + manufacturerData.substring(44, 48).toCharArray(buf, 5); |
| 61 | + int minor = (int)strtol(buf, NULL, 16); |
| 62 | + Serial.print("Minor ID: "); |
| 63 | + Serial.println(minor); |
| 64 | + |
| 65 | + // RSSI |
| 66 | + Serial.print("RSSI: "); |
| 67 | + Serial.println(peripheral.rssi()); |
| 68 | + |
| 69 | + Serial.println(); |
| 70 | +} |
0 commit comments