RTL.BeaconsProximity Sample
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.
Contents
Location
You can find the BeaconProximityDemo sample project at:
- Start | Programs | Embarcadero RAD Studio 12 Athens | Samples and navigate to:
Object Pascal\Multi-Device Samples\Device Sensors and Services\Bluetooth\Beacons\Beacons ProximityCPP\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.
Description
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.
Register Beacons
The Register Beacons TTabItem provides the possibility of registering/unregistering beacons. For registering a beacon, the Register Beacons TTabItem includes the following controls:
- 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 theBtnAddRegionClickevent handler. - Delete Region: the
BtnDeleteRegioncontrol fires theBtnDeleteRegionClickevent handler. - Start: the
Button1control fires theButton1Clickevent handler. - Stop: the
btnStopcontrol fires thebtnStopClickevent handler. - Clear Previous: the
Button2control fires theButton2Clickevent handler.
Detected Beacon
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.
Monitoring
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 Timer1Timer event handler of the TTimer updates the information of the Monitoring TTabItem.
Live View
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 theBeaconsRectangle.LbMajor: Shows the MajorID of the beacon selected in theBeaconsRectangle.LbMinor: Shows the MinorID of the beacon selected in theBeaconsRectangle.LbDistance: Shows the Distance of the beacon selected in theBeaconsRectangle.LbRssi: Shows the Rssi of the beacon selected in theBeaconsRectangle.BeaconsRectangle: Shows a representation of the proximity and the distance of the detected beacons. When you select a beacon, theBeaconsRectangleMouseUpevent 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.
Implementation
As a initialization, the FormCreate event handler calls the CheckManager method and registers some beacons by default which you may modify.
Main Unit
- The application defines the following event handlers:
BtnAddRegionClick: calls theCheckManagermethod and registers a beacon with the parameters specified inEdGuid,sbMajor, andsbMinor.BtnDeleteRegionClick: calls theCheckManagermethod and unregisters the selected beacon in theListBox1. The event handler also deletes the item from theListBox1.BtnStopClick: calls theCheckManagermethod and stops to scan beacons and disable theTimer1whose event handler isTimer1Timer.Button1Click: calls theCheckManagermethod, executes the StartScan method and enables theTimer1in case the StartScan execution success.Button2Click: Event handler for the Clear Previous control button. This event handler clears the following TListViews:LvEnteredBeaconLvExitedBeaconLvEnteredRegionLvExitedRegionLvMonitoring
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 theLvProximityTListViews and updates the color of the beacon representation which is shown in theBeaconsRectangleof the Live View. The colors are set according to the Proximity value:BeaconsRectangleMouseUp: Event handler related to theBeaconsRectangleof the Live View TTabItem. The event handler sets the selected beacon representation as theFSelectedBeacon. The event handler repaints the TRectangle.BeaconsRectanglePaint: Event handler called when repaints is performed. This event handler sets theLbUUID,LbMajor,LbMinor,LbDistance, andLbRssiwith the values of theFSelectedBeacon.ComboBox1Change: This event handler stops to scan beacons, release the TBeaconManager instance and calls theCheckManagerto create a new instance for the new selected beacon type. The event handler also register all the beacons included in theListBox1.ListBox1Click: Event handler which updates theEdGuid,sbMajorandsbMinorwith the values of the selected item of theListBox1.SpinBox1Click: The event handler sets the value of theSpinBox1to theFMaxDistance.Timer1Timer: This event handler clears theLvMonitoringTListView and adds an item to theLvMonitoringfor every beacon which is currently reachable, including the information related to each beacon. This event handler also updates theFListand repaints theBeaconsRectangle.EnterRegion: The event handler adds a new item to theLvEnteredRegionTListView with the data of the beacon related to the event.ExitRegion: The event handler adds a new item to theLvExitedRegionTListView with the data of the beacon related to the event.BeaconEnter: This event handler adds a new item to theLvEnteredBeaconTListView with the data of the beacon related to the event. The event also updates theFCurrentBeaconListfield.BeaconExit: This event handler adds a new item to theLvExitedBeaconTListView with the data of the beacon related to the event. The event also updates theFCurrentBeaconListfield.
- The application also defines the following methods:
CheckManager: In case there is no instance inFBeaconManager, the method gets an instance of the TBeaconManager for the beacon type specified inComboBox1. 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.
Render Unit
The application uses the BeaconsRender unit created for this sample. This unit creates the TBeaconGraphicInfo record and the TRender class. TRender class includes the following two methods:
Render: Called from theBeaconsRectanglePaintevent 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 theBeaconsRectangleMouseUpevent handler.
Uses
- FMX.TabControl.TTabControl
- FMX.TabControl.TTabItem
- System.Beacon.IBeacon
- System.Beacon.IBeacon.GUID
- System.Beacon.IBeacon.Major
- System.Beacon.IBeacon.Minor
- System.Beacon.IBeacon.Distance
- System.Beacon.TBeaconManager.ScanMode
- System.Beacon.IBeacon.Proximity
- System.Beacon.IBeacon.Rssi
- System.Beacon.TBeaconManager
- System.Beacon.TBeaconManager.OnBeaconEnter
- System.Beacon.TBeaconManager.OnBeaconExit
- System.Beacon.TBeaconManager.OnEnterRegion
- System.Beacon.TBeaconManager.OnExitRegion
- System.Beacon.TBeaconManager.OnBeaconProximity
- System.Beacon.TBeaconManager.StartScan
- System.Beacon.TBeaconManager.StopScan
- System.Beacon.TBeaconManager.UnregisterBeacon
- FMX.Types.TTimer
- FMX.Graphics.TCanvas
- FMX.ListView.TListView
- System.Beacon.TBeaconProximity
- System.UITypes.TAlphaColorRec.
- FMX.Controls.TControl.Repaint
- FMX.Objects.TRectangle