Using Classic Bluetooth
Go Up to Using Bluetooth
Because the Bluetooth Core Specification defines since version 4.0 different types of "Bluetooth" protocols, such as Bluetooth Low Energy, the original Bluetooth protocol is now known as "Classic Bluetooth". Classic Bluetooth provides a much higher transfer rate than Bluetooth Low Energy, at the cost of a higher energy consumption.
To implement Classic Bluetooth support in your applications:
- Add a TBluetooth component to your application.
- Discover remote devices and pair with them.
- Connect to the paired devices.
- Exchange data with the connected devices.
Contents
Platform Support
Platform | Supported? |
---|---|
Windows* |
|
macOS |
|
iOS |
|
Android |
|
*Windows Server does not support Bluetooth (see: General Bluetooth Support in Windows).
Classic Bluetooth on iOS
RTL does not support Classic Bluetooth on iOS. The reason is that only hardware manufacturers are given access to the SDK for Classic Bluetooth. For more information, see Apple Developers: MFi Program.
Adding a TBluetooth Component to Your Application
The RTL provides a component, TBluetooth, that gives you access to all the Classic Bluetooth features of the RTL. Drag a TBluetooth
component from the Tool Palette onto a form or data module of your application.
Discovering and Pairing with Remote Devices
Before your application can connect to a remote application using Classic Bluetooth, the devices running each application must be paired with each other using Bluetooth.
Making a Device Discoverable
One of the two devices must be discoverable, that is, other Bluetooth-enabled devices must be able to detect the device using Classic Bluetooth.
On most platforms you can request your user to make the device that is running your application discoverable. Call TBluetooth.StartDiscoverable to start a request, and handle the TBluetooth.OnDiscoverableEnd event. OnDiscoverableEnd
occurs if your user accepts to make the device discoverable.
Discovering Remote Devices
Once one of the devices is discoverable, the other device must discover the discoverable device.
Call TBluetooth.DiscoverDevices on the other device to start a discovery operation to get a list of discoverable remote devices in range. Handle the TBluetooth.OnDiscoveryEnd event to get the list of discovered remote devices as soon as the discovery operation finishes. You can access the last list of discovered devices at any time using the TBluetooth.LastDiscoveredDevices property.
Pairing with a Remote Device
Once your application obtains a list of remote devices, you can start a pairing request with any of those devices (TBluetoothDevice).
To start a pairing request, call TBluetooth.Pair. The actual pairing operation is handled by the operating system, which guides your user through the pairing process.
To know whether or not the pairing operation succeeded, check the value of TBluetoothDevice.IsPaired.
Establishing a Connection to a Paired Remote Device
Once the device that is running your application is paired with a remote device, you can connect to applications running on the remote device using Classic Bluetooth.
For two applications running on paired devices to connect using Classic Bluetooth:
- One of the applications (server) must publish a service. To publish a service, call TBluetooth.CreateServerSocket and save the instance of TBluetoothServerSocket that it returns.
- The other application (client) must discover the service of the server application and connect to it:
- Locate in TBluetooth.PairedDevices the remote device (TBluetoothDevice) that is running the server application.
- Locate in TBluetoothDevice.LastServiceList the service (TBluetoothService) that you want to use.
- Call TBluetoothDevice.CreateClientSocket to obtain a socket (TBluetoothSocket) to connect to the remote service. To specify the remote service, provide the UUID property of the service to
TBluetoothDevice.CreateClientSocket
. - Call TBluetoothSocket.Connect to start the connection.
- Back to the server application, call TBluetoothServerSocket.Accept to accept the incoming connection.
TBluetoothServerSocket.Accept
returns a regular Classic Bluetooth socket (TBluetoothSocket) already connected to the socket of the remote application.
Sending and Receiving Data
Once you have an instance of TBluetoothSocket connected to the remote application, either as a client or as a server, you can send or receive any data from the remote application as an array of bytes. To send and receive data from the remote application, use TBluetoothSocket.SendData and TBluetoothSocket.ReadData respectively.