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.

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 BeaconsTTabItem provides the possibility of registering/unregistering beacons. For registering a beacon, the Register BeaconsTTabItem includes the following controls:

Beacon Type: The ComoBox1 control is used for configuring the ScanMode.

Add Region: the BtnAddRegion control fires the BtnAddRegionClick event handler.

Delete Region: the BtnDeleteRegion control fires the BtnDeleteRegionClick event handler.

Start: the Button1 control fires the Button1Click event handler.

Stop: the btnStop control fires the btnStopClick event handler.

Clear Previous: the Button2 control fires the Button2Click event handler.

Detected Beacon

The Detected BeaconTTabItem 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:

Monitoring

The MonitoringTTabItem 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 MonitoringTTabItem.

Live View

The Live ViewTTabItem 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 BeaconsRectangle.

LbMajor: Shows the MajorID of the beacon selected in the BeaconsRectangle.

LbMinor: Shows the MinorID of the beacon selected in the BeaconsRectangle.

LbDistance: Shows the Distance of the beacon selected in the BeaconsRectangle.

LbRssi: Shows the Rssi of the beacon selected in the BeaconsRectangle.

BeaconsRectangle: Shows a representation of the proximity and the distance of the detected beacons. When you select a beacon, the BeaconsRectangleMouseUp 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 the CheckManager method and registers a beacon with the parameters specified in EdGuid, sbMajor, and sbMinor.

BtnDeleteRegionClick: calls the CheckManager method and unregisters the selected beacon in the ListBox1. The event handler also deletes the item from the ListBox1.

BtnStopClick: calls the CheckManager method and stops to scan beacons and disable the Timer1 whose event handler is Timer1Timer.

Button1Click: calls the CheckManager method, executes the StartScan method and enables the Timer1 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 the LvProximityTListViews and updates the color of the beacon representation which is shown in the BeaconsRectangle of the Live View. The colors are set according to the Proximity value:

BeaconsRectangleMouseUp: Event handler related to the BeaconsRectangle of the Live ViewTTabItem. 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 LbUUID, LbMajor, LbMinor, LbDistance, and LbRssi with the values of the FSelectedBeacon.

ComboBox1Change: This event handler stops to scan beacons, release the TBeaconManager instance and calls the CheckManager to create a new instance for the new selected beacon type. The event handler also register all the beacons included in the ListBox1.

ListBox1Click: Event handler which updates the EdGuid, sbMajor and sbMinor with the values of the selected item of the ListBox1.

SpinBox1Click: The event handler sets the value of the SpinBox1 to the FMaxDistance.

Timer1Timer: This event handler clears the LvMonitoringTListView and adds an item to the LvMonitoring for every beacon which is currently reachable, including the information related to each beacon. This event handler also updates the FList and repaints the BeaconsRectangle.

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 FCurrentBeaconList field.

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 FCurrentBeaconList field.

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: