In android, Bluetooth is a communication network protocol, which allows devices to connect wirelessly to exchange the data with other Bluetooth devices.
Generally, in android applications by using Bluetooth API’s we can implement Bluetooth functionalities, such as searching for the available Bluetooth devices, connecting with the devices and managing the data transfer between devices within the range.
By using android Bluetooth API’s in android applications, we can perform the following functionalities.
To transfer the data between two Bluetooth devices first, they must establish a communication channel using the pairing process. The devices which we are going to pair must be discoverable and should accept the incoming connection requests. Generally, the devices will find discoverable devices using a service discovery process. Once the device accepts the pairing request, the two devices will exchange security keys to complete the bonding process and the devices will cache these security keys for later use.
Once the pairing and bonding process completes, the devices are ready to exchange the required information. When the session is complete, the device that initiated the pairing request will release the channel that linked to the discoverable device. The two devices remain bonded, so they can reconnect automatically during a future session as long as they're in the range of each other.
To use Bluetooth features in our android applications, we must need to add multiple permissions, such as BLUETOOTH and ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION in our manifest file.
|We need this permission to perform any Bluetooth communication, such as requesting a connection, accepting a connection, and transferring data.
|We need this permission because the Bluetooth scans can be used to gather the information about the location of user.
In case, if we want to discover the available Bluetooth devices or manipulate Bluetooth settings from our app, we need to define BLUETOOTH_ADMIN permission.
Following is the example of defining the Bluetooth permissions in android manifest file.
<manifest ... >
In android, we can perform Bluetooth related activities by using BluetoothAdapter class in our applications.
By using BluetoothAdapter object, we can interact with device’s Bluetooth adapter to perform Bluetooth related operations. In case, if device does not contain any Bluetooth adapter, then it will return null.
Following is the code snippet to initialize BluetoothAdapter class and to know whether the Bluetooth is supported on the device or not.
If you observe above code snippet, we used the getDefaultAdapter() method of BluetoothAdapter class, which will return whether the device contains Bluetooth adapter or not.
In case if getDefaultAdapter() method returns NULL, then the device does not support Bluetooth and we can disable all Bluetooth features.
If Bluetooth is supported but disabled, then the isEnabled() method will return false and we can request the user to enable Bluetooth without leaving our application by using startActivityForResult() method with ACTION_REQUEST_ENABLE intent action parameter.
Following is the code snippet to enable a Bluetooth by using BluetoothAdapter parameter ACTION_REQUEST_ENABLE.
Intent eintent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
If you observe above code snippet, we used startActivityForResult() method with ACTION_REQUEST_ENABLE intent action parameter to enable a Bluetooth.
The second parameter intVal in startActivityForResult() method is a locally defined integer that must be greater than 0 and the system will return this parameter back to us during onActivityResult() implementation as a requestCode parameter.
To know more about to TURN ON / OFF Bluetooth in android applications, check this Android Bluetooth Turn ON / OFF with Examples.
Following is the code snippet to enable the system’s discoverable mode to make sure that the device discoverable to other devices.
If you observe above code snippet, we are making sure our device discoverable to other devices using ACTION_REQUEST_DISCOVERABLE. By default, the device becomes discoverable for 120 seconds. We can extend the device discoverable duration up to 3600 seconds (1 hour), by adding the EXTRA_DISCOVERABLE_DURATION extra.
To know more about device discoverability, check this Android Bluetooth Device Discoverability with Examples.
By using the BluetoothAdapter method getBondedDevices(), we can get the Bluetooth paired devices list.
Following is the code snippet to get all paired devices with name and MAC address of each device.
If you observe above code, we are getting the Bluetooth paired devices name and mac address by using BluetoothDevice object.
To know more about Bluetooth pair devices, check this Android Bluetooth List Pair Devices with Examples.
This is how we can use Bluetooth in android applications to allow a devices to connect wirelessly to exchange the data with other Bluetooth devices.