RTL.Proximity Client and Server Sample

From RAD Studio Code Examples
Jump to: navigation, search

This sample shows how to implement a client-server connection using the Bluetooth Low Energy framework. The sample demonstrates how to discover devices, get services and get their characteristics. The sample includes a ProximityClient and a ProximityServer.

Location

You can find the ProximityClient sample project at:

  • Start | Programs | Embarcadero RAD Studio Athens | Samples and navigate to:
    • Object Pascal\Multi-Device Samples\Device Sensors and Services\Bluetooth\ProximityClientServer
    • CPP\Multi-Device Samples\Device Sensors and Services\Bluetooth\ProximityClientServer
  • Subversion Repository:
    • You can find Delphi and C++ code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.

Description

This sample consists of a ProximityClient application that connects with a ProximityServer using Bluetooth LE.

The client searches for Bluetooth LE sensor devices. If the server is active, the client allows you to connect to the server and get proximity information. The client application displays the Tx Power, RSSI, Path Loss values as well as the Distance to the server device. During the connection, the server and the client applications display in a TMemo the information regarding the status of the connection, the events and some values for a characteristic, such as the UID.

The following table shows the different behaviors of the client-server connection having different distances between the client-server devices. When the devices are close, the client displays a Near message and the server a green flag meaning No Alert. If the distance increases, the client displays Far and the server a yellow flag meaning Mild Alert. Finally, if the devices are far enough, the client displays So Far and the server a red flag meaning High Alert.

Near Far So Far

ProximityClient

NearClient.png

FarClient.png

SoFarClient.png

ProximityServer

NearServer.png

FarServer.png

SoFarServer.png

How to Use the Sample

  1. Navigate to the location given above and open ProximityClientServer.groupproj. It includes the ProximityServer and ProximityClient projects.
  2. Build the project group.
  3. To run the server:
    1. In the ProximityServer target platform, select a device with Bluetooth.
      Note: See Using Bluetooth for more information about client platform support for Bluetooh LE.
    2. Run the application by clicking Run > Run in the IDE or by pressing F9.
    3. Click Start announce to start the server and create the services.
  4. To run the client:
    1. In the ProximityClient target platform, select a device with Bluetooth.
      Note: See Using Bluetooth for more information about client platform support for Bluetooh LE.
    2. Run the application by clicking Run > Run in the IDE or by pressing F9.
    3. Click Scan to start discovering devices.
    4. Click Connect to connect the server device and get proximity information.

Implementation

To use the RTL Bluetooth feature, you must include the System.Bluetooth unit in your application.

Scanning Devices

To have this application running, the first thing needed is to find devices with Bluetooth LE sensor.

The Start Annnounce button in the server application calls TBluetoothLEManager.Current to get an instance of TBluetoothLEManager. TBluetoothManager is the main class of the Bluetooth Low Energy implementation of the RTL.

In the client application, the Scan button calls the StartDiscovery method using a single argument <Timeout> to start discovering devices. The <Timeout> specifies how much time you want to spend discovering remote devices using Bluetooth Low Energy.

The OnServicesDiscovered event is triggered when the discovery operation finishes. The btnConnect button is enabled and you can connect with the BluetoothLE service.

Getting Services and Characteristics

When you press the btnConnect button in the client interface, it calls a GetServiceAndCharacteristics method that access to TBluetoothGattServiceList to get the UIDDName of the Characteristic and the Service. This information is displayed in the TMemo.

Reading and Writing Characteristics

The OnCharacteristicRead/OnCharacteristicWrite events fire after reading/writing the characteristic.

You can also write on a characteristic when this option is supported.

Uses

See Also