|
| 1 | +#include <BTstackLib.h> |
| 2 | +#include <SPI.h> |
| 3 | + |
| 4 | +/* |
| 5 | + EXAMPLE_START(LEDeviceScanner): LE Device Scanner |
| 6 | +
|
| 7 | + @text The LE Device Scanner monitors BLE device advertisements, |
| 8 | + keeping track of one or more known devices as they appear. |
| 9 | +*/ |
| 10 | + |
| 11 | +// Application state |
| 12 | +int counter[2] = {0, 0}; |
| 13 | +BD_ADDR known_devices[2] = {BD_ADDR("DB:88:B6:70:9E:EB"), |
| 14 | + BD_ADDR("C9:60:BD:F2:B4:9D") |
| 15 | + }; |
| 16 | + |
| 17 | +/* |
| 18 | + @section Setup |
| 19 | +
|
| 20 | + @text After BTstack.setup(), BTstack is configured to call |
| 21 | + advertisementCallback whenever an Advertisement was received. |
| 22 | +*/ |
| 23 | +/* LISTING_START(LEDeviceScannerSetup): LE Device Scanner Setup */ |
| 24 | +void setup(void) { |
| 25 | + Serial.begin(9600); |
| 26 | + BTstack.setBLEAdvertisementCallback(advertisementCallback); |
| 27 | + BTstack.setup(); |
| 28 | + BTstack.bleStartScanning(); |
| 29 | +} |
| 30 | +/* LISTING_END(LEDeviceScannerSetup): LE Device Scanner Setup */ |
| 31 | + |
| 32 | +/* |
| 33 | + @section Loop |
| 34 | +
|
| 35 | + @text In the standard Arduino loop() function, BTstack's loop() is called. |
| 36 | +*/ |
| 37 | +/* LISTING_START(LEDeviceScannerLoop): Loop */ |
| 38 | +void loop(void) { |
| 39 | + BTstack.loop(); |
| 40 | +} |
| 41 | +/* LISTING_END(LEDeviceScannerLoop): Loop */ |
| 42 | + |
| 43 | +/* |
| 44 | + @section Advertisement Callback |
| 45 | +
|
| 46 | + @text Whenever an Advertisement is received, isIBeacon() checks if |
| 47 | + it contains an iBeacon. |
| 48 | +
|
| 49 | + If it's not an iBeacon, the BD_ADDR is compared to the address we are |
| 50 | + looking for and the counter is incremented. |
| 51 | +*/ |
| 52 | +/* LISTING_START(LEDeviceScannerAdvertisementCallback): Advertisement Callback |
| 53 | +*/ |
| 54 | +void advertisementCallback(BLEAdvertisement *bleAdvertisement) { |
| 55 | + if (!(bleAdvertisement->isIBeacon())) { |
| 56 | + Serial.print("Device discovered: "); |
| 57 | + Serial.println(bleAdvertisement->getBdAddr()->getAddressString()); |
| 58 | + for (size_t i = 0; i < sizeof(counter) / sizeof(int); i++) { |
| 59 | + if (memcmp(bleAdvertisement->getBdAddr()->getAddress(), |
| 60 | + known_devices[i].getAddress(), sizeof(known_devices[i])) == 0) { |
| 61 | + counter[i]++; |
| 62 | + Serial.printf("Known device: %s, has been discovered %d times.\n", |
| 63 | + known_devices[i].getAddressString(), counter[i]); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | +/* LISTING_END(LEDeviceScannerAdvertisementCallback): Advertisement Callback */ |
0 commit comments