FMX.Heart Rate Monitor Sample
The sample shows how to use the TBluetoothLE component in a client application to connect to a Bluetooth LE sensor that implements the Heart Rate standard profile.
To have this sample working and showing values, you need a heart rate monitor sensor with Bluetooh LE or Smart Bluetooth technology.
Location
You can find the HeartRate Monitor sample project at:
- Start | Programs | Embarcadero RAD Studio Athens | Samples and navigate to:
Object Pascal\Multi-Device Samples\Device Sensors and Services\Bluetooth\HeartRateMonitor
CPP\Multi-Device Samples\Device Sensors and Services\Bluetooth\HeartRateMonitor
- 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 is a sample of a client application that searches for a server publishing a particular standard service. In this specific case, the Heart Rate service.
The majority of the functionality is implemented using the TBluetoothLE component instead of using the framework directly.
How to Use the Sample
- Navigate to the location given above and open HeartRateMonitor.dproj.
- Select as a platform a device with a bluetooth sensor.
- Note: See Using Bluetooth for more information about client platform support for Bluetooh LE.
- Press F9 or choose Run > Run.
- Click Scan to discover server devices publishing the Heart Rate service.
- Click Stop monitoring to stop receiving information from the server.
Implementation
The Scan button starts searching for Bluetooth LE devices implementing the Heart Rate service. It uses the DiscoverDevices method with a timeout period and an array of TBluetoothUUID's, in this case, BluetoothLE1.DiscoverDevices(2500, [HRSERVICE])
where HRSERVICE is the standard service for the heart rate measurement: HRSERVICE: TBluetoothUUID = '{0000180D-0000-1000-8000-00805F9B34FB}'
.
After the timeout period the OnEndDiscoverDevices event is triggered. When there is no available BluetoothLE sensor the 0 devices discovered:
appears on the TMemo, otherwise the number of devices is shown and the application starts searching for the services on the device.
To discover the services on a device it uses the GetServices method. If no service is found, the message 'No services found!'
appears on the TMemo, otherwise the application starts getting some specific characteristics for the service using the GetCharacteristic method:
- HRMEASUREMENT_CHARACTERISTIC:
HRMEASUREMENT_CHARACTERISTIC: TBluetoothUUID = '{00002A37-0000-1000-8000-00805F9B34FB}'
- BODY_SENSOR_LOCATION_CHARACTERISTIC:
BODY_SENSOR_LOCATION_CHARACTERISTIC: TBluetoothUUID = '{00002A38-0000-1000-8000-00805F9B34FB}'
After getting the service and the characteristic, the monitoring starts and the values are shown. When the application successfully gets a value for the characteristic, it calls the SubscribeToCharacteristic method to start receiving the values periodically. Click Stop monitoring to call the UnSubscribeToCharacteristic method to stop receiving values from the device.
The OnCharacteristicRead event is triggered after reading the value of the characteristic.
The server device sends a package with raw data to the client. To extract the information in order to display the bmp's (beats per minute) the sample follows the specification on the GATT profile, extracting the flags from the first byte and then the value. For more information, see Heart Rate Measurement Data Package Structure.
Uses
- System.Bluetooth.Components.TBluetoothLE
- System.Bluetooth.Components.TBluetoothLE.DiscoverDevices
- System.Bluetooth.Components.TBluetoothLE.OnEndDiscoverDevices
- System.Bluetooth.Components.TBluetoothLE.GetServices
- System.Bluetooth.Components.TBluetoothLE.GetCharacteristic
- FMX.Memo.TMemo