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 Athens | 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.
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
ComoBox1
control is used for configuring the ScanMode. - GUID: The
EdGuid
control is used for configuring the GUID. - Major ID: the
sbMajor
control is used for configuring the MajorID. - Minor ID: the
sbMinor
control is used for configuring the MinorID. - Add Region: the
BtnAddRegion
control fires theBtnAddRegionClick
event handler. - Delete Region: the
BtnDeleteRegion
control fires theBtnDeleteRegionClick
event handler. - Start: the
Button1
control fires theButton1Click
event handler. - Stop: the
btnStop
control fires thebtnStopClick
event handler. - Clear Previous: the
Button2
control fires theButton2Click
event 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, theBeaconsRectangleMouseUp
event 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 theCheckManager
method and registers a beacon with the parameters specified inEdGuid
,sbMajor
, andsbMinor
.BtnDeleteRegionClick
: calls theCheckManager
method and unregisters the selected beacon in theListBox1
. The event handler also deletes the item from theListBox1
.BtnStopClick
: calls theCheckManager
method and stops to scan beacons and disable theTimer1
whose event handler isTimer1Timer
.Button1Click
: calls theCheckManager
method, executes the StartScan method and enables theTimer1
in case the StartScan execution success.Button2Click
: Event handler for the Clear Previous control button. This event handler clears the following TListViews:LvEnteredBeacon
LvExitedBeacon
LvEnteredRegion
LvExitedRegion
LvMonitoring
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 theLvProximity
TListViews and updates the color of the beacon representation which is shown in theBeaconsRectangle
of the Live View. The colors are set according to the Proximity value:BeaconsRectangleMouseUp
: Event handler related to theBeaconsRectangle
of 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
, andLbRssi
with the values of theFSelectedBeacon
.ComboBox1Change
: This event handler stops to scan beacons, release the TBeaconManager instance and calls theCheckManager
to 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
,sbMajor
andsbMinor
with the values of the selected item of theListBox1
.SpinBox1Click
: The event handler sets the value of theSpinBox1
to theFMaxDistance
.Timer1Timer
: This event handler clears theLvMonitoring
TListView and adds an item to theLvMonitoring
for every beacon which is currently reachable, including the information related to each beacon. This event handler also updates theFList
and repaints theBeaconsRectangle
.EnterRegion
: The event handler adds a new item to theLvEnteredRegion
TListView with the data of the beacon related to the event.ExitRegion
: The event handler adds a new item to theLvExitedRegion
TListView with the data of the beacon related to the event.BeaconEnter
: This event handler adds a new item to theLvEnteredBeacon
TListView with the data of the beacon related to the event. The event also updates theFCurrentBeaconList
field.BeaconExit
: This event handler adds a new item to theLvExitedBeacon
TListView with the data of the beacon related to the event. The event also updates theFCurrentBeaconList
field.
- 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 theBeaconsRectanglePaint
event 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 theBeaconsRectangleMouseUp
event 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