Android Bluetooth

Android Bluetooth

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 –

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

Apart from this constant, there are other constants provided the API , that supports different tasks. They are listed below –

ConstantDescription
ACTION_REQUEST_DISCOVERABLEThis constant is used for turn on discovering of bluetooth.
ACTION_STATE_CHANGEDThis constant will notify that Bluetooth state has been changed.
ACTION_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 –

Apart form the paired Devices , there are other methods in the API that gives more control over Bluetooth. They are listed below –

MethodDescription
enable()This method enables the adapter if not enabled.
isEnabled()This method returns true if adapter is enabled.
disable()This method disables the adapter.
getName()This method returns the name of the Bluetooth adapter.
setName(String name)This method changes the Bluetooth name.
getState()This method returns the current state of the Bluetooth Adapter.
startDiscovery()This method starts the discovery process of the Bluetooth for 120 seconds.

Example

In this tutorial, we are going to learn how to Enable, Disable, Enable Discovery, search nearby Bluetooth devices and Pair with other Bluetooth devices.

Video Demo

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is MainActivity.java.

2. Open res ⇒ values ⇒ strings.xml and add below string values. This are some strings that i am going to use in this tutorial.

3. To work on Bluetooth we have to give certain permission in Manifest file and add this line to your manifest.

4. Now, create a xml layout naming activity_main.xml. In this layout i put some buttons to work on Bluetooth and a ListView to show paired and new searched devices.

5. We will display Bluetooth device name with device MAC address. So I am using custom listview for that. This is customview.xml i.e custom layout.

If you are new to Custom ListView then refer this tutorial.

6. For pairing two Bluetooth devices I am using this Thread class. This class can be used to pair any bluetooth devices. Here BluetoothSocket is used to pair devices by connect() method. Before connecting we have to unregister all broadcast receivers and bluetooth discovery.

7. Finally come to your MainActivity.java  and add the following code. In this java class i used above discussed methods of Bluetooth to work over Bluetooth and a Thread class to pair devices.

8. Now, you are all done, run your app and you will get the output as shown in video.

Thanks. :)

Download Source Code – >

 

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)