FMX.Android Beacon Service Sample
This demo shows an Android application using an Android Service to present a notification when a beacon with immediate proximity (below 0.5 meters) is detected. The Android Service works as a background task, even if the main application is not running. The service stops itself after performing the task.
You can find the Android Beacon Service sample project at:
- Start | Programs | Embarcadero RAD Studio Sydney | Samples, and then navigate to:
Object Pascal\Multi-Device Samples\Device Sensors and Services\AndroidBeaconServiceDemo
- Subversion Repository:
- You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
This sample demonstrates how to create and use a local Android service to present a notification.
- The Beacon Service project consists of a Data Module. A TNotificationCenter component creates and presents a notification when a beacon with immediate proximity is detected. The parameters for a specific beacon are configured using the TBeacon component.
- The Beacon Service Application project starts the Android Service to present a notification on the Android device. Once the beacon is detected, a notification is presented. When the user handles it, a message is sent to the TMemo. To handle notifications, the application uses a TNotificationCenter component.
How to Use the Sample
To open the projects:
- Navigate to the location given above.
- Open the sample application group project file: BeaconServiceDemo.groupproj.
To create the Android Service:
- Select libBeaconService.so on the Projects Window.
- Right-click to select Compile.
- Note: Compiling the project generates the files that you need to add to the main application.
To add the Android Service to the main application:
- Select BeaconServiceApp on the Projects Window.
- Expand the Target Platforms node.
- Select the Android device.
- Right-click on the Android device, and select Add Android Service.
- Select the location to
C:\Users\Public\Documents\Embarcadero\Studio\21.0\Samples\Object Pascal\Multi-Device Samples\Device Sensors and Services\AndroidBeaconServiceDemo\BeaconService.
- Click Next.
- Note: Check that the location to these files is added: libBeaconService.so, BeaconService.jar, and BeaconServiceUnit.pas.
- Click Finish.
- Press F9 or choose Run > Run to deploy the application to an Android device.
Configuring the Beacon
You need to set the specific parameters for the Beacon device that you want to use with this demo.
Configure the TBeacon component from the service Data Module:
- Select the TBeacon component.
- In the Object Inspector, go to the MonitorizedRegions property.
- Click to see the TBeaconRegionCollection.
- Select the existing TBeaconRegionItem : 0-TBeaconRegionItem.
- Edit the Major, Minor and UUID properties from the Object Inspector.
Starting the Android Service
- To start the service, on the Beacon Service application, click Start Service.
- Place the Android device near the beacon. For an immediate proximity the distance must be less than 0.5 meters.
- When the beacon is detected, a notification is presented in the screen.
- Click the notification to trigger the OnReceiveLocalNotification event of the TNotificationCenter from the main application.
- See the message 'Beacon Proximity detected:'+ BeaconName in the TMemo.
The Beacon Service is an Android Service project. The type of the service is local.
The OnStartCommand event from the Data Module triggers when the service starts. OnStartCommand enables the TBeacon to start scaning regions automatically.
function TBeaconServiceDM.AndroidServiceStartCommand(const Sender: TObject; const Intent: JIntent; Flags, StartId: Integer): Integer; begin Beacon1.Enabled := True; //Enables the TBeacon component to start scanning regions. Result := TJService.JavaClass.START_STICKY; //The '''OnStartCommand''' event is defined by default as START_NOT_STICKY, this line of code sets it to START_STICKY. end;
- Note: When defined as START_STICKY, the system tries to re-create the service when killed.
The OnBeaconProximity event of the TBeacon triggers every time the Proximity value changes. When the proximity is Immediate, it calls the NotifyBeaconProximity procedure that presents a notification on the Android device.
procedure TBeaconServiceDM.Beacon1BeaconProximity(const Sender: TObject; const ABeacon: IBeacon; Proximity: TBeaconProximity); begin if Proximity = TBeaconProximity.Immediate then //Checks if the proximity to the Beacon device is below 0.5 meters. NotifyBeaconProximity(ABeacon.GUID.ToString + ':' + ABeacon.Major.ToString + ',' + ABeacon.Minor.ToString); //It calls '''NotifyBeaconProximity''' with some parameters of the detected Beacon device. end;
Beacon Service App
The Beacon Service application is a Multi-Device application project.
The OnClick event-handler of the Start Service button calls the StartService method of the TLocalServiceConnection class.
- Note: TLocalServiceConnection is the helper class that manages the connection to an Android Local Service.
procedure TForm1.Button1Click(Sender: TObject); begin TLocalServiceConnection.StartService('BeaconService'); //Initialize the TLocalServiceConnection variable to a specific service, and starts it. end;
- Note: After starting a service, you do not need to refer to the service name for the rest of methods.
The OnReceiveLocalNotification event of the TNotification component triggers once the user handles the notification on the Android device, that means clicking on the notification, not dismissing it.
procedure TForm1.NotificationCenter1ReceiveLocalNotification(Sender: TObject; ANotification: TNotification); begin Memo1.Lines.Add(ANotification.AlertBody); //It shows the alert body of the notification defined on the Android Service project. end;
The sample uses Built-in Java libraries for Android.