Building Proximity Aware Application with RAD Studio XE8

by Apr 9, 2015

RAD Studio XE8 provides you with the tools to rapidly design, build, and deploy connected apps that deliver innovative IoT solutions.

The new Beacon component makes it easy to add proximity awareness to your applications.

Beacons Proximity Stadium Large

Two beacon formats are available: iBeacon and AltBeacon.

  • iBeacon is a format defined by Apple. When working with iBeacons, select the Standard Mode available for the TBeacon component. 
  • AltBeacon is an open format, known as Alternative beacon. The full specifications can be found on http://altbeacon.org. When working with AltBeacons, select the Alternative Mode for the TBeacon component.  

Bluetooth Low Energy devices in advertising mode send the Advertising Data repeatedly over radio in different AD type structures. 

One of the AD types in the Advertising Data is the "Manufacturer Specific Data" which is utilized for beacons. The data included in the "Manufacturer Specific Data" is:

  • UUID: A unique identifier which identifies a group of beacons, for example, the beacons of a specific company.
  • MajorID, MinorID: Identifies regions inside a specific UUID. MajorID identifies a sub-region within a larger region defined by an UUID. The MinorID specifies further subdivision within a MajorID.
  • TxPower: The TxPower is the measured power from 1 meter of distance. With TxPower and the Received Signal Strength Indicator (RSSI), the approximate distance to a Beacon is calculated.

iBeacon requires both major and minor numbers to be defined. 

Major numbers are designed to identify a group while minor numbers are designed to identify an individual beacon.

TBeacon has a property called MonitorizedRegions that lets you define the UUID, minor and major numbers for your beacons. Generally speaking, all your beacons should use the same UUID. You would then change the Major and Minor values based on your needs. 

MonitorizedRegionsProperty

If you set the Major and Minor values to -1, it indicates that you want to track all beacons using that same UUID. You can then programmatically define the Major and Minor numbers and execute actions depending on which beacon was detected.

A good practice would be to store the beacon information in a database. Then, the client app running on the user's smartphone or tablet can connect to the same database.

Upon detecting the beacon in your application, you will receive an event. In the event, you can read the major and minor values and then fire another action, such as displaying a location specific message as the user approaches a specific beacon.

Below is a sample use case showing how major and minor numbers for beacons can be used in a real world scenario to identify them. 

Tradeshow Beacon Use Case:

   4 exhibit halls, so major numbers of 1 – 4 are being used to identify the 4 exhibit halls

–  Each exhibit has 20 booths, so each beacon is assigned a minor number of 1 to 20

–   This means that there are a total of 80 beacons used in this scenario 

 BeaconDataTable1

 

  
Beacons are supported on Android, iOS and OS X. Beacons are not supported on Windows since
the beacon advertising data is not accessible on Windows.
 
Detailed information on setting permissions to use beacons etc. can be found on our docwiki:

Here is a quick code example that finds nearby beacons:

unit BeaconForm; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.Beacon, System.Beacon.Components, FMX.StdCtrls, FMX.Controls.Presentation, FMX.ListBox, FMX.Layouts; type TBeaconDiscoverForm = class(TForm) StartDiscoveryBtn: TButton; CancelDiscoveryBtn: TButton; Timer1: TTimer; Label1: TLabel; BeaconDistance: TLabel; ToolBar1: TToolBar; Label5: TLabel; BeaconDiscoveryList: TListBox; ListBoxGroupHeader1: TListBoxGroupHeader; ListBoxItem1: TListBoxItem; ListBoxItem2: TListBoxItem; ToolBar2: TToolBar; Beacon1: TBeacon; procedure StartDiscoveryBtnClick(Sender: TObject); procedure CancelDiscoveryBtnClick(Sender: TObject); procedure Beacon1BeaconProximity(const Sender: TObject; const ABeacon: IBeacon; Proximity: TBeaconProximity); procedure Timer1Timer(Sender: TObject); procedure Beacon1BeaconEnter(const Sender: TObject; const ABeacon: IBeacon; const CurrentBeaconList: TBeaconList); private { Private declarations } FBeacon : IBeacon; public { Public declarations } end; var BeaconDiscoverForm: TBeaconDiscoverForm; implementation {$R *.fmx} {$R *.LgXhdpiPh.fmx ANDROID} {$R *.iPhone47in.fmx IOS} {$R *.Macintosh.fmx MACOS} procedure TBeaconDiscoverForm.Beacon1BeaconEnter(const Sender: TObject; const ABeacon: IBeacon; const CurrentBeaconList: TBeaconList); begin if ABeacon <> nil then; end; procedure TBeaconDiscoverForm.Beacon1BeaconProximity(const Sender: TObject; const ABeacon: IBeacon; Proximity: TBeaconProximity); begin if Proximity = TBeaconProximity.Near then begin FBeacon := ABeacon; Label1.Text := 'Beacon: '+ FBeacon.GUID.ToString+' Major:'+FBeacon.Major.ToString+' Minor:'+FBeacon.Minor.ToString; end; end; procedure TBeaconDiscoverForm.StartDiscoveryBtnClick(Sender: TObject); begin Beacon1.StartScan; Timer1.Enabled := True; end; procedure TBeaconDiscoverForm.CancelDiscoveryBtnClick(Sender: TObject); begin Beacon1.StopScan; Timer1.Enabled := False; end; procedure TBeaconDiscoverForm.Timer1Timer(Sender: TObject); begin if FBeacon <> nil then BeaconDistance.Text := FBeacon.Distance.ToString + 'm'; end; end.

BeaconDiscoveryDemo

You can download the demo here. This is the same demo that I showed during the XE8 launch webinar today. It has UUIDs predefined for the following manufacturers:

  • Estimote 
  • Radius Networks
  • BLE Home
  • EM Microelectronic

If you are using beacons by a different manufacturer or are using custom UUIDs, you can add/edit them via the MonitorizedRegions property on TBeacon. 

You can also watch the beacon proximity skill sprint here: https://www.youtube.com/watch?v=_VWFk3RTW8A 

Several different beacon focused demo projects for Delphi and C++ are also installed with RAD Studio XE8. You can access them by going to:

C:UsersPublicDocumentsEmbarcaderoStudio16.0SamplesObject PascalMobile SamplesDevice Sensors and ServicesBluetoothBeacons

C:UsersPublicDocumentsEmbarcaderoStudio16.0SamplesCPPMobile SamplesDevice Sensors and ServicesBluetoothBeacons

 

Download a Free RAD Studio XE8 Trial today: http://www.embarcadero.com/downloads