This is a sample that shows the use of the Bluetooth API to search for nearby Bluetooth devices and check whether they broadcast any services.
You can find the BLEScanner sample project at:
- Start | Programs | Embarcadero RAD Studio Rio | Samples and then navigate to either:
Object Pascal\Multi-Device Samples\Device Sensors and Services\Bluetooth\BLEScanner
CPP\Multi-Device Samples\Device Sensors and Services\Bluetooth\BLEScanner
- 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 application uses the Bluetooth API to search for nearby devices.
The application uses the following controls:
Start Scan: Fires the
STOP Scan: Fires the
ProgressBar1: Shows the progress of the discovery.
ListBox1: Displays the information about discovered devices.
ListBox2: Displays the information about discovered services.
TBluetoothLE: The BluetoothLE component that handles discovery of devices.
Timer1: The timer that fires events used for the progress bar tracking.
How to Use the Sample
- Navigate to one of the locations given above and open:
- Delphi: BLEScanner.dproj.
- C++: BLEScannerCPP.cbproj.
- Press F9 or choose Run > Run.
- Click the Start Scan button to start searching for devices.
- Wait until the discovery completes.
- Choose a discovered device from the list.
- The services that the device broadcasts are displayed in the
ListBox2control. If that device does not broadcast any services, the text "Discover services not allow" is displayed in the
To interrupt a discovery, click the STOP Scan button.
This application uses an instance of TBluetoothLE to:
- Search for nearby Bluetooth Low Energy devices.
- Display names of discovered devices in the
- Search for services on a specific device.
- Display the information about discovered services in the
On initialization, the
FormShow method initializes a Boolean that is used as a control for the discovery interruption and to prevent multiple simultaneous scanning.
- The application defines the following event handlers:
Button1Click: Resets the progress bar, clears the
ListBox1control, enables the timer and initiates discovery.
Button2Click: Resets the timer and cancels discovery.
BluetoothLE1DiscoverLEDevice: Occurs for every discovered device. Adds the name of the discovered device to the
BluetoothLE1EndDiscoverDevices: Occurs when the discovery finishes. Disables the timer.
BluetoothLE1ServicesDiscovered: Occurs for every discovered service. Reads the name and the UUID from the list of characteristics and adds them to the
ListBox1ItemClick: Clears the
ListBox2control and initiates service discovery.
Timer1Timer: Occurs for every timer interval (default is 1 second). Calculates the progress of the discovery based on the maximum discovery time and updates the progress bar accordingly.