RTL.ScanFilterServicesAdvData
This is a sample that shows the use of the Bluetooth API to search for nearby Bluetooth devices and filter them by services.
- Note: This sample application works on Windows, but only on Windows 10 and later versions.
Contents
Location
You can find the ScanFilterServicesAdvData sample project at:
- Start | Programs | Embarcadero RAD Studio 10.2 Tokyo | Samples and navigate to:
Object Pascal\Multi-Device Samples\Device Sensors and Services\Bluetooth\ScanFilterServicesAdvData
- Subversion Repository:
Description
This application uses the Bluetooth API to search for nearby devices.
The application uses the following controls:
Start Scan
: Fires theButton1Click
event handler.STOP Scan
: Fires theButton2Click
event handler.CheckBox1
: Specifies whether to force the search for non-broadcast services.ListBox1
: Displays the information about discovered devices.TBluetoothLE
: The BluetoothLE component that handles discovery of devices.
How to Use the Sample
- Navigate to the location given above and open:
- Delphi: ScanFilterServicesAdvData.dproj.
- Press F9 or choose Run > Run.
- Click the Start Scan button to start searching for devices.
- Wait until the discovery completes.
- All the devices that broadcast any service from the filter are listed in the
ListBox1
control.
To interrupt a discovery, click the STOP Scan button. If you wish to force the discovery on non-broadcast services, check the Force Connection to Devices check box and restart the discovery.
Implementation
This application uses an instance of TBluetoothLE to:
- Search for nearby Bluetooth Low Energy devices.
- Filter discovered devices by services.
- Display the information about discovered devices in the
ListBox1
control.
Main Form
On initialization, the FormShow
method initializes a list that acts as a filter for services. This list contains the following services:
- HeartRate
- HealthThermometer
- DeviceInformation
- The application defines the following event handlers:
Button1Click
: Initializes a list of devices, clears theListBox1
control, and initiates discovery.Button2Click
: Cancels discovery.BluetoothLE1DiscoverLEDevice
: Occurs for every discovered device. This method checks the services that the device advertises against the filter list. If any service is a match, thePrintLine
method adds the device name to the list. If the Force Connection to Devices check box is checked, this method checks all services (including non-broadcast).BluetoothLE1EndDiscoverDevices
: Occurs when the discovery finishes. Adds any matching non-broadcast services to theListBox1
control.FormClose
: Occurs on application exit. Destroys any remaining lists of devices.
- The application also defines the following methods:
PrintLine
: Gets the name and identifier of the device and adds them to theListBox1
control.AddDeviceToList
: Adds the specified device to the provided device list.
Uses
- System.Bluetooth
- System.Bluetooth.Components
- FMX.ListBox
- System.Generics.Collections
- System.StrUtils