FMX.Android Beacon Service Sample

From RAD Studio Code Examples
Jump to: navigation, search

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.

Location

You can find the Android Beacon Service sample project at:

Description

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:

  1. Navigate to the location given above.
  2. Open the sample application group project file: BeaconServiceDemo.groupproj.

To create the Android Service:

  1. Select libBeaconService.so on the Project Manager.
  2. 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:

  1. Select BeaconServiceApp on the Project Manager.
  2. Expand the Target Platforms node.
  3. Select the Android device.
  4. Right-click on the Android device, and select Add Android Service.
  5. Select the location to C:\Users\Public\Documents\Embarcadero\Studio\17.0\Samples\Object Pascal\Multi-Device Samples\Device Sensors and Services\AndroidBeaconServiceDemo\BeaconService.
  6. Click Next.
    Note: Check that the location to these files is added: libBeaconService.so, BeaconService.jar, and BeaconServiceUnit.pas.
  7. Click Finish.
  8. 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:

  1. Select the TBeacon component.
  2. In the Object Inspector, go to the MonitorizedRegions property.
  3. Click ProjectOptionsEllipsis.png to see the TBeaconRegionCollection.
  4. Select the existing TBeaconRegionItem : 0-TBeaconRegionItem.
  5. Edit the Major, Minor and UUID properties from the Object Inspector.

Starting the Android Service

  1. To start the service, on the Beacon Service application, click Start Service.
  2. Place the Android device near the beacon. For an immediate proximity the distance must be less than 0.5 meters.
  3. When the beacon is detected, a notification is presented in the screen.
  4. Click the notification to trigger the OnReceiveLocalNotification event of the TNotificationCenter from the main application.
  5. See the message 'Beacon Proximity detected:'+ BeaconName in the TMemo.

Implementation

Beacon Service

The Beacon Service is an Android Service project. The type of the service is local.

The Data Module has a TNotificationCenter component and a TBeacon component.

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;

The MonitorizedRegions property of the TBeacon defines the region to be monitored. A region consists of a collection of TBeacon devices defined by their Major, Minor and UUID parameters.

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;

Uses

The sample uses Built-in Java libraries for Android.

  • Androidapi.JNI.App
  • AndroidApi.JNI.GraphicsContentViewText
  • Androidapi.JNI.Os

See Also