Go Up to Using Bluetooth
A beacon is a Bluetooth Low Energy device including some information in its advertising data, specifically in the "Manufacturer Specific Data" described in "Bluetooth Specification Version 4.1, page 2023" https://www.bluetooth.org/en-us. This information allows any Bluetooth Low Energy device in the surrounding area to identify the device and calculate the distance to it without being paired or establishing a connection.
- 1 Beacon Overview
- 2 How to Use Beacons
- 3 Adding Location Services
- 4 Indoor Limitations
- 5 See Also
Two beacon formats are available: iBeacon and AltBeacon:
- iBeacon: It is the format defined by Apple. For the RTL API, the iBeacon is considered the Standard Mode.
- AltBeacon: It is an open format, known as Alternative beacon. The full specifications can be found in http://altbeacon.org. For the RTL API the Alternative beacon is considered the Alternative Mode.
You need to know the format that is using the beacon you want to monitor.
Bluetooth Low Energy devices in advertising mode send the Advertising Data repeatedly over radio in different AD type structures. One of the AD types in the Advertising Data is the "Manufacturer Specific Data" which is utilized for beacons. The data included in the "Manufacturer Specific Data" which the RTL API provides for beacons is:
- UUID: A unique identifier which identifies a group of beacons, for example, the beacons of a specific company.
- MajorID, MinorID: Identifies regions inside a specific UUID. MajorID identifies a sub-region within a larger region defined by an UUID. The MinorID specifies further subdivision within a MajorID.
- TxPower: The TxPower is the measured power from 1 meter of distance. With TxPower and the Received Signal Strength Indicator (RSSI), the approximate distance to a Beacon is calculated.
Mac OS X
RTL API for beacons is not available for Windows platform because the advertising data is not accessible.
How to Use Beacons
The RTL API for beacons allows you to obtain the Distance between the device that is running your application and the surrounding beacons. The RTL API for beacons provides information only for the beacons within a defined region. Region is a group of beacons specified with their UDID, MajorID and MinorID.
Obtaining an instance of TBeaconManager
To use the RTL API for beacons, you must include the System.Beacon unit in your application, and call TBeaconManager.GetBeaconManager for the TBeaconScanMode you want to use to get an instance of TBeaconManager.
BeaconManager := TBeaconManager.GetBeaconManager(TBeaconScanMode.Standard);
BeaconManager := TBeaconManager.GetBeaconManager(TBeaconScanMode.Alternative);
The ScanMode identifies the type of beacon the system should look for: Standard Mode or Alternative Mode. TBeaconManager is the main class of the beacon implementation of the RTL API for beacons. TBeaconManager is in charge of registering regions, and manages the events to get the information from beacons. The TBeaconManager instance must be created either for Standard Mode or Alternative Mode.
Registering Regions to be Monitored
You must register the regions of the beacons you want to monitor. For registering regions, you must call TBeaconManager.RegisterBeacon.
Regions are specified with the parameters UUID, MajorID and MinorID. For registering a region, only the parameter UUID is required. In case you want to be more specific about the region you want to register, you can also use MajorID, MinorID or both, according to the three overloaded TBeaconManager.RegisterBeacon defined.
Start & Stop to Scan Beacons
Once the beacons you want to monitor are registered, you can start to monitor those beacons calling TBeaconManager.StartScan. In case you want to stop to monitor beacons you must call TBeaconManager.StopScan.
Getting information from incoming beacons
The way of getting the information about the monitored beacons is through the events associated to the TBeaconManager. These events are:
- OnBeaconEnter: Fired every time a new beacon is reachable.
- OnBeaconExit: Fired every time a beacon becomes unreachable.
- OnBeaconProximity: Fired every time the Proximity value changes.
- OnCalcDistance: Fired immediately before the Distance is calculated.
- OnEnterRegion: Fired the first time a beacon is reachable from a registered region.
- OnExitRegion: Fired when all registered beacons on a region are unreachable.
Once the event is fired, you can use the information provided by the event.
- Note: Due to cross platform compatibility requirements, if you register several region items with the same UUID and you wish to monitor them, you can only get events of the last region item that you registered in the TBeaconRegionCollection. In case you want to monitor different regions, you can proceed in two different manners depending on your scenario:
- Register a region with its specific UUID, MajorID=-1 and MinorID=-1 in order to receive events associated to all the beacons with the same UUID.
- Register a region with its specific UUID and design your app to filter MajorID and MinorID according to your custom values and app requirements.
Adding Location Services
Location services allow location-based apps to use information from cellular, Wi-Fi, GPS and beacons to determine your approximate location. For detecting beacons, you must enable the location services.
For Android, you must enable the bluetooth options in Project > Options > Uses Permissions.
For iOS platform on Alternative Mode, you do not need to activate Location services. In Standard Mode (iBeacons), the location activation request must be accepted first time the App is launched.
From iOS 8.0, if you use the Standard Mode, some keys for enabling the application to use the location services are needed for beacon detection. The IDE already includes
The key we need to add manually is:
For the values we just need a text message such as "This app needs access to Location services to monitor beacons". The system shows the text message when it accesses the app configuration.
The key can be added in the IDE, Project > Options > Version Info and then right-click on the grid and Add Key.
The radio signal that beacons broadcast is highly sensitive to noise effects such as diffraction, absorption, multipath reflections, or interferences that come from other devices that emit in the same radio frequency band. In particular, these harmful effects critically affect the signal of the beacon in indoor environments. As a result, the estimates extracted from power measurements, such as Rssi, Distance or Proximity, become considerably unstable and tend to fluctuate. Thus the accuracy of these estimations diminishes.
The RTL provides two different algorithms to make the signal of a beacon and the estimates extracted from that signal more stable:
- RssiToDistance introduces a model to get the distance estimate derived from Rssi measurements. Note that a calibrated power parameter,
ATxPower, enhances the robustness of the algorithm.
- TBeaconManager implements an optional differential filter algorithm. This algorithm stabilizes the Rssi and distance measurements. To enable this algorithm, select the Stabilized calculation mode on the CalcMode property. This mode is especially useful for proximity estimation and movement detection.
You can also embed further signal-processing algorithms, which may be either general-purpose algorithms or algorithms designed by yourself. The following list contains some example filters that you can implement to mitigate noise effects:
- Moving average filter
- Weighted average filer
- Curve fitting filter