RTL.Explore BluetoothLE Devices Sample
This sample shows how to use the Bluetooth Low Energy framework to discover devices, get services and get their characteristics with the corresponding values and available options.
To have this sample working and showing values, you need a heart rate monitor sensor with Bluetooh LE or Smart Bluetooth technology.
You can find the Explore Devices LE sample project at:
- Start | Programs | Embarcadero RAD Studio Rio | Samples and navigate to:
Object Pascal\Multi-Device Samples\Device Sensors and Services\Bluetooth\ExploreDevicesLE
CPP\Multi-Device Samples\Device Sensors and Services\Bluetooth\ExploreDevicesLE
- 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.
This sample is a client application that searches for Bluetooth LE sensor devices.
The application allows selecting a device to see all the services on the device and also all the characteristics for a particular service.
On the right-hand panel you can see some values for a characteristic, such as the name, UID, and current value. On the Available operations panel the available options for the current characteristic are selected. The application allows writing on the values for the characteristics that allow writing.
The functionality is implemented using the System.Bluetooth framework directly.
How to Use the Sample
- Navigate to the location given above and open ExploreDevicesLE.dproj.
- Select as a platform a device with Bluetooth.
- Note: See Using Bluetooth for more information about client platform support for Bluetooh LE.
- Press F9 or choose Run > Run.
- Click Find devices to start discovering devices.
- After the timeout period you can select one of the found devices from the drop-down menu and then click Select.
- Click Get services to get all the services for the selected device.
- Select a characteristic from a service to see on the right-hand panel the details including the UUID and the available operations for the selected characteristic.
To use the RTL Bluetooth feature, you must include the System.Bluetooth unit in your application.
To have this application running the first thing needed is to find devices with Bluetooth LE sensor. The Find devices button calls TBluetoothLEManager.Current to get an instance of TBluetoothLEManager. TBluetoothManager is the main class of the Bluetooth Low Energy implementation of the RTL.
The StartDiscovery method is called with 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 OnDiscoveryEnd event is triggered when the discovery operation finishes. The tmAnimateFindDevices timer is set to False and the TProgressBar is completed. This timer is used to increment the toolbar using the specified time on the Interval property of the timer.
Finding and Getting Services
After selecting a discovered device from the drop-down menu, click the Select button to fill the text property of the EdCurrentDevice label.
The Get Services button gets the current device using the EdCurrentDevice label. The DiscoverServices method is called to search for the available services on the device. The OnServicesDiscovered event fires when the discovery operation finishes and the TTreeView shows all the characteristics for all the services on the devices in a hierarchical way.
The tmAnimateFindServices timer is used to increment the toolbar while searching the services for the current device.
Reading and Writing Characteristics
Click the Get all Values... button to get the information for the selected characteristic on the TTreeView.
The OnCharacteristicRead event fires after reading the characteristic. The TBluetoothGattService.Characteristics is called with the current characteristic as a parameter to fill the values and the available operations on the right-hand side of the application.
You can also write on a characteristic when this option is supported. The Write button calls the WriteCharacteristic method for this purpose.
The Subscribe button calls SetCharacteristicNotification to enable notifications when the characteristics supports it, otherwise the
"This characteristic doesn't allow notifications" message is shown.