Show: Delphi C++
Display Preferences

Using Classic Bluetooth

From RAD Studio
Jump to: navigation, search

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:

Platform Support

Platform Supported?

Windows*

YesC++11Feature.png

OS X

YesC++11Feature.png

iOS

Android

YesC++11Feature.png

*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.

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.

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 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:

  1. 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.
  2. The other application (client) must discover the service of the server application and connect to it:
    1. Locate in TBluetooth.PairedDevices the remote device (TBluetoothDevice) that is running the server application.
    2. Locate in TBluetoothDevice.LastServiceList the service (TBluetoothService) that you want to use.
    3. 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.
    4. Call TBluetoothSocket.Connect to start the connection.
  3. 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.

See Also

Personal tools
In other languages
Previous Versions
Assistance