Bluetooth is a way to send or receive data between two different devices. Android platform includes support for the Bluetooth framework that allows a device to wirelessly exchange data with other Bluetooth devices.

Android provides Bluetooth API to perform these different operations.

  • Scan for other Bluetooth devices
  • Get a list of paired devices
  • Connect to other devices through service discovery

Android provides BluetoothAdapter class to communicate with Bluetooth. Create an object of this calling by calling the static method getDefaultAdapter(). Its syntax is given below.

private BluetoothAdapter BA;

BA = BluetoothAdapter.getDefaultAdapter();

In order to enable the Bluetooth of your device, call the intent with the following Bluetooth constant ACTION_REQUEST_ENABLE. Its syntax is.

Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(turnOn, 0);      

Sr.NoConstant & description
1ACTION_REQUEST_DISCOVERABLEThis constant is used for turn on discovering of bluetooth
2ACTION_STATE_CHANGEDThis constant will notify that Bluetooth state has been changed
3ACTION_FOUNDThis constant is used for receiving information about each device that is discovered

Once you enable the Bluetooth , you can get a list of paired devices by calling getBondedDevices() method. It returns a set of bluetooth devices. Its syntax is.

private Set<BluetoothDevice>pairedDevices;

pairedDevices = BA.getBondedDevices();

Apart from the parried Devices , there are other methods in the API that give more control over Bluetooth. They are listed below.

Sr.NoMethod & description
1enable()This method enables the adapter if not enabled
2isEnabled()This method returns true if adapter is enabled
3disable()This method disables the adapter
4getName()This method returns the name of the Bluetooth adapter
5setName(String name)This method changes the Bluetooth name
6getState()This method returns the current state of the Bluetooth Adapter.
7startDiscovery()This method starts the discovery process of the Bluetooth for 120 seconds.