RTL.BLEScanner Sample
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.
Contents
Location
You can find the BLEScanner sample project at:
- Start | Programs | Embarcadero RAD Studio Athens | 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.
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.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
ListBox2
control. If that device does not broadcast any services, the text "Discover services not allow" is displayed in theListBox2
control.
To interrupt a discovery, click the STOP Scan button.
Implementation
This application uses an instance of TBluetoothLE to:
- Search for nearby Bluetooth Low Energy devices.
- Display names of discovered devices in the
ListBox1
control. - Search for services on a specific device.
- Display the information about discovered services in the
ListBox2
control.
Main Form
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 theListBox1
control, 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 theListBox1
control.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 theListBox2
control.ListBox1ItemClick
: Clears theListBox2
control 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.