The BeaconProximityDemo is a sample application for monitoring beacons. This sample provides a tool to register/unregister beacons, monitoring beacons events and checking the proximity of surrounding beacons.
You can find the BeaconProximityDemo sample project at:
- Start | Programs | Embarcadero RAD Studio Rio | Samples and navigate to:
Object Pascal\Multi-Device Samples\Device Sensors and Services\Bluetooth\Beacons\Beacons Proximity
CPP\Multi-Device Samples\Device Sensors and Services\Bluetooth\Beacons\Beacons Proximity
- Subversion Repository:
- You can find Delphi and C++ code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
This sample provides a FireMonkey application which is based on TTabControl with four different TTabItem for four different functionalities: Register Beacons, Detected Beacons, Monitoring and Live View.
- Beacon Type: The
ComoBox1control is used for configuring the ScanMode.
- GUID: The
EdGuidcontrol is used for configuring the GUID.
- Major ID: the
sbMajorcontrol is used for configuring the MajorID.
- Minor ID: the
sbMinorcontrol is used for configuring the MinorID.
- Add Region: the
BtnAddRegioncontrol fires the
- Delete Region: the
BtnDeleteRegioncontrol fires the
- Start: the
Button1control fires the
- Stop: the
btnStopcontrol fires the
- Clear Previous: the
Button2control fires the
The Detected Beacon TTabItem provides information about the different events which occur during the scanning. For monitoring the different event, the application includes another TTabControl on it. This TTabControl includes 5 TTabItems, one for each of the possible beacon events:
- Proximity Changed: This TTabItem registers the OnBeaconProximity events.
- Exit Region: This TTabItem registers the OnExitRegion events.
- Exited Beacon: This TTabItem registers the OnBeaconExit events.
- Entered Beacon: This TTabItem registers the OnBeaconEnter events.
- Entered Region: This TTabItem registers the OnEnterRegion events.
The Monitoring TTabItem provides the current information of every detected beacon. Detected beacons are those which were registered before the Start scanning button is pressed. Information provided for every beacon is:
The Live View TTabItem provides a live view representation of the distance and proximity of the beacons related to the device position. It distributes the detected beacons along a straight line representing the distance and proximity to the device. This representation is done through a TCanvas. This TTabItem also defines the following controls:
LbUUID: Shows the GUID of the beacon selected in the
LbMajor: Shows the MajorID of the beacon selected in the
LbMinor: Shows the MinorID of the beacon selected in the
LbDistance: Shows the Distance of the beacon selected in the
LbRssi: Shows the Rssi of the beacon selected in the
BeaconsRectangle: Shows a representation of the proximity and the distance of the detected beacons. When you select a beacon, the
BeaconsRectangleMouseUpevent handler is fired.
How to Use the Sample
- Navigate to the location given above and open:
- Delphi: BeaconProximityDemo.dproj.
- C++: BeaconProximityCPP.cbproj.
- Select as a platform, a device with a Bluetooth LE sensor
- Note: See Using Bluetooth for more information about client platform support for Bluetooth LE.
- Press F9 or choose Run > Run.
- Register the beacons you want to detect.
- Click Start to start to detect beacons.
- Click Stop to stop to detect beacons.
How to register a beacon
As it is explained in Using Beacons, the beacons must be registered before being monitored. To register a beacon, the GUID is mandatory while MajorID and MinorID are optional. If you set MajorID or MinorID with a value
<=-1, you register all the possible values for this parameter.
As a initialization, the
FormCreate event handler calls the
CheckManager method and registers some beacons by default which you may modify.
- The application defines the following event handlers:
BtnAddRegionClick: calls the
CheckManagermethod and registers a beacon with the parameters specified in
BtnDeleteRegionClick: calls the
CheckManagermethod and unregisters the selected beacon in the
ListBox1. The event handler also deletes the item from the
BtnStopClick: calls the
CheckManagermethod and stops to scan beacons and disable the
Timer1whose event handler is
Button1Click: calls the
CheckManagermethod, executes the StartScan method and enables the
Timer1in case the StartScan execution success.
Button2Click: Event handler for the Clear Previous control button. This event handler clears the following TListViews:
BeaconProximity: Event handler which occurs when the proximity property of any of the registered beacons changes. The event handler adds the beacon whose proximity parameter has changed to the
LvProximityTListViews and updates the color of the beacon representation which is shown in the
BeaconsRectangleof the Live View. The colors are set according to the Proximity value:
BeaconsRectangleMouseUp: Event handler related to the
BeaconsRectangleof the Live View TTabItem. The event handler sets the selected beacon representation as the
FSelectedBeacon. The event handler repaints the TRectangle.
BeaconsRectanglePaint: Event handler called when repaints is performed. This event handler sets the
LbRssiwith the values of the
ComboBox1Change: This event handler stops to scan beacons, release the TBeaconManager instance and calls the
CheckManagerto create a new instance for the new selected beacon type. The event handler also register all the beacons included in the
ListBox1Click: Event handler which updates the
sbMinorwith the values of the selected item of the
SpinBox1Click: The event handler sets the value of the
Timer1Timer: This event handler clears the
LvMonitoringTListView and adds an item to the
LvMonitoringfor every beacon which is currently reachable, including the information related to each beacon. This event handler also updates the
FListand repaints the
EnterRegion: The event handler adds a new item to the
LvEnteredRegionTListView with the data of the beacon related to the event.
ExitRegion: The event handler adds a new item to the
LvExitedRegionTListView with the data of the beacon related to the event.
BeaconEnter: This event handler adds a new item to the
LvEnteredBeaconTListView with the data of the beacon related to the event. The event also updates the
BeaconExit: This event handler adds a new item to the
LvExitedBeaconTListView with the data of the beacon related to the event. The event also updates the
- The application also defines the following methods:
CheckManager: In case there is no instance in
FBeaconManager, the method gets an instance of the TBeaconManager for the beacon type specified in
ComboBox1. If a new instance is got, the method also registers the following events:
StringToRegion: This method parses the GUID, Major, and Minor from the input string.
The application uses the BeaconsRender unit created for this sample. This unit creates the
TBeaconGraphicInfo record and the
TRender class includes the following two methods:
Render: Called from the
BeaconsRectanglePaintevent handler. This method draws the representation of the different beacons in the Live View tab.
GetObjectUnderMouse: Method which returns the object located under the mouse. The method is called from the