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:
- Obtain an instance of TBluetoothManager.
- Discover remote devices and pair with them.
- Connect to the paired devices.
- Exchange data with the connected devices.
- 1 Platform Support
- 2 Obtaining an Instance of TBluetoothManager
- 3 Discovering and Pairing with Remote Devices
- 4 Establishing a Connection to a Paired Remote Device
- 5 Sending and Receiving Data
- 6 See Also
Mac OS X
- Note: iOS Device does not support Classic Bluetooth, it only supports Bluetooth Low Energy.
Obtaining an Instance of TBluetoothManager
To use the RTL Bluetooth feature, you must include the System.Bluetooth unit in your application, and call TBluetoothManager.Current to get an instance of TBluetoothManager.
TBluetoothManager is the main class of the Classic Bluetooth implementation of the RTL.
TBluetoothManager.Currentalways returns the same instance of
TBluetoothManager. You do not need more than one instance of
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 TBluetoothManager.StartDiscoverable to start a request, and handle the TBluetoothManager.OnDiscoverableEnd event.
OnDiscoverableEnd occurs if your user accepts to make the device discoverable.
- Note: You can only request to make a device discoverable for a certain amount of time, with limits in some platforms. If that time passes and you have not yet performed the discovery and pairing steps below, you might need to request to make the device discoverable again.
Mac OS X does not allow applications to request users to make the device discoverable. You must ask your user to make the device discoverably manually.
Discovering Remote Devices
Once one of the devices is discoverable, the other device must discover the discoverable device.
Call TBluetoothManager.StartDiscovery on the other device to start a discovery operation to get a list of discoverable remote devices in range. Handle the TBluetoothManager.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 TBluetoothManager.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 TBluetoothManager.CurrentAdapter.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 TBluetoothManager.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 TBluetoothManager.GetPairedDevices 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
- 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.