With the Bluetooth API, you can use Bluetooth functionalities, such as managing the Bluetooth service, pairing, and exchanging messages between Bluetooth-enabled devices.
The Bluetooth standard provides a peer-to-peer (P2P) data exchange functionality over short distance between compliant devices. The data that can be exchanged between 2 or more devices includes text, images, videos, and requests and responses for a service.
The main features of Bluetooth API include:
- Managing Bluetooth
You can manage the Bluetooth service using the BluetoothAdapter interface.
To use the Bluetooth functionality of the device, you must switch the Bluetooth adapter on.
- Discovering devices
You can discover other Bluetooth devices within a range of 10 meters.
A known device is a paired device or a device that has been found in a prior discovery process. The known device information is stored in the local Bluetooth adapter. When a Bluetooth discovery process is started, you can speed up the process and enhance the usability of your application by displaying a list of known devices while continuing the search for new devices.
The device discovery process can retrieve multiple types of Bluetooth devices, such as printers, mobile phones, and headphones. To ensure you discover the kind of devices you want to communicate with, the BluetoothClass interface is used to define characteristics and capabilities of a Bluetooth device. The BluetoothClassDeviceMajor and BluetoothClassDeviceMinor interfaces specify the identifiers for major and minor Class of Device (CoD).
- Pairing devices
You can pair a device with another device retrieved through the discovery process by creating a bond between them. The pairing enables secure data exchange between 2 devices, authentication of the Bluetooth device, and allows the 2 devices to establish a connection as soon as they are in range.
Once the Bluetooth devices are bonded, the authentication step is not required for subsequent connections.
- Connecting to devices and exchanging data with them
You can connect to a device and exchange data with them using the BluetoothSocket interface. When you attempt to open a connection to another device, a Service Discovery Protocol (SDP) look-up is performed on the device, and the protocol and channel to be used for the connection are determined. If the connections is established and the socket opening succeeds, a BluetoothSocket instance with an open state is returned. The socket is subsequently used for exchanging data between the connected devices.
The device can also act as an Internet modem to provide a connection to multiple Bluetooth-enabled devices simultaneously using the Radio Frequency Communication (RFCOMM) channel. If a device allows other devices to use its functionalities through this kind of connection, it is said to provide a service.