RTL.BeaconsProximity Sample

From RAD Studio Code Examples
Jump to: navigation, search

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.

Location

You can find the BeaconProximityDemo sample project at:

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


BeaconsProximity01.png


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:


BeaconsProximity02.png


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.


BeaconsProximity03.png


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


BeaconsProximity04.png


How to Use the Sample

  1. Navigate to the location given above and open:
    • Delphi: BeaconProximityDemo.dproj.
    • C++: BeaconProximityCPP.cbproj.
  2. 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.
  3. Press F9 or choose Run > Run.
  4. Register the beacons you want to detect.
  5. Click Start to start to detect beacons.
  6. 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:
      1. LvEnteredBeacon
      2. LvExitedBeacon
      3. LvEnteredRegion
      4. LvExitedRegion
      5. 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 LvProximity TListViews 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:
      1. Immediate: Color set to Green.
      2. Near: Color set to Yellow.
      3. Far: Color set to Red.
      4. Away: Color set to Black.
    • BeaconsRectangleMouseUp: Event handler related to the BeaconsRectangle of 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 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 LvMonitoring TListView 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 LvEnteredRegion TListView with the data of the beacon related to the event.
    • ExitRegion: The event handler adds a new item to the LvExitedRegion TListView with the data of the beacon related to the event.
    • BeaconEnter: This event handler adds a new item to the LvEnteredBeacon TListView 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 LvExitedBeacon TListView with the data of the beacon related to the event. The event also updates the FCurrentBeaconList field.

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 the BeaconsRectanglePaint 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 the BeaconsRectangleMouseUp event handler.

Uses

See Also