Detect iBeacons using Spark Core and BLE Mini
Here is a sample application I worked after BLE Mini HCI experiment. This sample uses BLE Mini with HCI Firmware to scan iBeacons. I tested with RFDuino with iBeacon example and Apple AirLocate example. The AirLocate application turns your iOS 7.0 or later device into an iBeacon transmitter.
To detect a BLE Device/iBeacon, we have to:
- Initialize HCI
- Start device discovery
- Wait for GAP_DeviceInformation packet (event code 0x060D). We will receive this for each device scanned.
- Parse the GAP_DeviceInformation packet which contains RSSI, iBeacon UUID, Major, Minor and Measured Power
- Wait for the scan to complete and repeat step 2 onwards
Here I am using BLE Mini as the BLE Central device to scan surrounding BLE device and communicate with Spark Core using Serial. The application initialize and start device discovery, waits for scanned devices and then publish the result using
Spark.publish. After scanning is started, we will get GAP_DeviceInformation for every device present in the surrounding area. The device information contains RSSI, iBeacon UUID, Major, Minor and Measured Power.
Using the RSSI value we get from the Device Information packet, we can find the distance of the device using the formula described here. This is only an approximate distance, we can use this one to determine whether the device is too close or too far, etc… The Apple iBeacon framework uses some other formula that is not public. Radius Networks derived the above formula and is used in the iBeacon library.
The application looks for a beacon, when found one and if the distance is less than 0.5m then turn on an LED. We can test this by placing an iBeacon close to the Spark Core. If it is very close the LED turns on and when we move away from Spark Core the LED turns off.
Here is a demo video of the sample application in action.
You can also use the companion Web Application to see the nearby iBeacons. To use the Web Page, you should replace the tags deviceid and accesstoken.
- BLE Mini Vin to 5V
- BLE GND to GND
- BLE Mini Tx to Rx
- BLE Mini Rx to Tx
- LED to Spark Core D2