xref: /btstack/port/arduino/examples/iBeaconScanner/iBeaconScanner.ino (revision 8caefee39d444df6d8908a96a844825f10fbdaa4)
1*8caefee3SMatthias Ringwald
2*8caefee3SMatthias Ringwald#include <BTstack.h>
3*8caefee3SMatthias Ringwald#include <SPI.h>
4*8caefee3SMatthias Ringwald
5*8caefee3SMatthias Ringwald/* EXAMPLE_START(iBeaconScanner): iBeacon Scanner
6*8caefee3SMatthias Ringwald *
7*8caefee3SMatthias Ringwald * @section Setup
8*8caefee3SMatthias Ringwald *
9*8caefee3SMatthias Ringwald * @text After BTstack.setup(), BTstack is configured to call
10*8caefee3SMatthias Ringwald * advertisementCallback whenever an Advertisement was received.
11*8caefee3SMatthias Ringwald * Then, a device discovery is started
12*8caefee3SMatthias Ringwald */
13*8caefee3SMatthias Ringwald
14*8caefee3SMatthias Ringwald/* LISTING_START(iBeaconSetup): iBeacon Scanner Setup */
15*8caefee3SMatthias Ringwaldvoid setup(void){
16*8caefee3SMatthias Ringwald    Serial.begin(9600);
17*8caefee3SMatthias Ringwald    BTstack.setup();
18*8caefee3SMatthias Ringwald    BTstack.setBLEAdvertisementCallback(advertisementCallback);
19*8caefee3SMatthias Ringwald    BTstack.bleStartScanning();
20*8caefee3SMatthias Ringwald}
21*8caefee3SMatthias Ringwald/* LISTING_END(iBeaconSetup): iBeacon Scanner Setup */
22*8caefee3SMatthias Ringwald
23*8caefee3SMatthias Ringwaldvoid loop(void){
24*8caefee3SMatthias Ringwald    BTstack.loop();
25*8caefee3SMatthias Ringwald}
26*8caefee3SMatthias Ringwald
27*8caefee3SMatthias Ringwald/*
28*8caefee3SMatthias Ringwald * @section Advertisment Callback
29*8caefee3SMatthias Ringwald *
30*8caefee3SMatthias Ringwald * @text Whenever an Advertisement is received, isIBeacon() checks if
31*8caefee3SMatthias Ringwald * it contains an iBeacon. If yes, the Major ID, Minor ID, and UUID
32*8caefee3SMatthias Ringwald * is printed.
33*8caefee3SMatthias Ringwald * If it's not an iBeacon, only the BD_ADDR and the received signal strength
34*8caefee3SMatthias Ringwald * (RSSI) is shown.
35*8caefee3SMatthias Ringwald */
36*8caefee3SMatthias Ringwald/* LISTING_START(iBeaconCallback): iBeacon Scanner Callback */
37*8caefee3SMatthias Ringwaldvoid advertisementCallback(BLEAdvertisement *adv) {
38*8caefee3SMatthias Ringwald    if (adv->isIBeacon()) {
39*8caefee3SMatthias Ringwald        Serial.print("iBeacon found ");
40*8caefee3SMatthias Ringwald        Serial.print(adv->getBdAddr()->getAddressString());
41*8caefee3SMatthias Ringwald        Serial.print(", RSSI ");
42*8caefee3SMatthias Ringwald        Serial.print(adv->getRssi());
43*8caefee3SMatthias Ringwald        Serial.print(", UUID ");
44*8caefee3SMatthias Ringwald        Serial.print(adv->getIBeaconUUID()->getUuidString());
45*8caefee3SMatthias Ringwald        Serial.print(", MajorID ");
46*8caefee3SMatthias Ringwald        Serial.print(adv->getIBeaconMajorID());
47*8caefee3SMatthias Ringwald        Serial.print(", MinorID ");
48*8caefee3SMatthias Ringwald        Serial.print(adv->getIBecaonMinorID());
49*8caefee3SMatthias Ringwald        Serial.print(", Measured Power ");
50*8caefee3SMatthias Ringwald        Serial.println(adv->getiBeaconMeasuredPower());
51*8caefee3SMatthias Ringwald    } else {
52*8caefee3SMatthias Ringwald        Serial.print("Device discovered: ");
53*8caefee3SMatthias Ringwald        Serial.print(adv->getBdAddr()->getAddressString());
54*8caefee3SMatthias Ringwald        Serial.print(", RSSI ");
55*8caefee3SMatthias Ringwald        Serial.println(adv->getRssi());
56*8caefee3SMatthias Ringwald    }
57*8caefee3SMatthias Ringwald}
58*8caefee3SMatthias Ringwald/* LISTING_END(iBeaconCallback): iBeacon Scanner Callback */
59*8caefee3SMatthias Ringwald
60