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.
You can find the ScanFilterServicesAdvData sample project at:
- Start | Programs | Embarcadero RAD Studio Alexandria | Samples and navigate to:
Object Pascal\Multi-Device Samples\Device Sensors and Services\Bluetooth\ScanFilterServicesAdvData
- Subversion Repository:
- You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
This application uses the Bluetooth API to search for nearby devices.
The application uses the following controls:
Start Scan: Fires the
STOP Scan: Fires the
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
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.
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
On initialization, the
FormShow method initializes a list that acts as a filter for services. This list contains the following services:
- The application defines the following event handlers:
Button1Click: Initializes a list of devices, clears the
ListBox1control, 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, the
PrintLinemethod 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 the
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 the
AddDeviceToList: Adds the specified device to the provided device list.