Get $500+ of the best After Effects files, video templates and music for only $20!
Android Quick Look: BluetoothAdapter

Android Quick Look: BluetoothAdapter

Tutorial Details
  • Technology: Android SDK + Eclipse
  • Difficulty: Beginner
  • Completion Time: 20 - 30 Minutes

The Android SDK ships with powerful Bluetooth APIs capable of managing the local Bluetooth adapter, scanning for nearby Bluetooth devices, transferring data between bluetooth devices, and more. This Bluetooth quick look will show you the most fundamental steps necessary to begin programming Bluetooth applications on the Android SDK.

Step 1: Import the Bluetooth Package

The first thing to do when you want to work with Bluetooth in an Android application is to import the Bluetooth API package. To do so, add the following line of code to the class you want to use the Bluetooth APIs:

import android.bluetooth.*;

Step 2: Set Bluetooth Permissions

In order to work with Bluetooth in your app, you’ll need to set the following permission:

<uses-permission android:name="android.permission.BLUETOOTH" />

For more advanced Bluetooth tasks, such as setting the name of the local device as we will do in this tutorial, you’ll need to configure the Bluetooth Admin permission. Add the following line to do so:

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

Step 3: Access the Bluetooth Adapter

The Android Bluetooth APIs include a BluetoothAdapter class. This class is used as a singleton to access the Bluetooth radio on the Android device. This class must be instantiated in order to perform any Bluetooth related tasks in the program.

BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

If the device the above is run on does not support Bluetooth, the returned value will be equal to “null”. In a practical application, you should check for this condition and update the app if needed. You could check for this with a simple if statement:

BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

if(bluetooth != null)
{
    // Continue with bluetooth setup.
}

Step 4: Check if Bluetooth is Enabled

Like I pointed to above, when you first call getDefaultAdapter() null will be returned if Bluetooth is not supported on the device. However, even if Bluetooth is supported on the device, it may not be enabled by the user. This is the next thing to check.

if (bluetooth.isEnabled()) {
    // Enabled. Work with Bluetooth.
}
else
{
    // Disabled. Do something else.
}

Step 5: Display Device Name and Address

Using the code above, let’s next display a message to the user to report on the Bluetooth status. If Bluetooth is enabled on the device, we will display the device name and address. If Bluetooth is disabled, we will display a message to tell the user.

String status;
if (bluetooth.isEnabled()) {
    String mydeviceaddress = bluetooth.getAddress();
    String mydevicename = bluetooth.getName();
    status = mydevicename + ” : ” + mydeviceaddress;
}
else
{
    status = “Bluetooth is not Enabled.”;
}

Toast.makeText(this, status, Toast.LENGTH_LONG).show();

This code snippet uses the getName() method to report the name of the Bluetooth device. If you set the BLUETOOTH_ADMIN permission earlier, you could also change the device name to something else, like this:

bluetooth.setName("AndroidCoder");

Step 6: Display Bluetooth State

The BluetoothAdapter.getState() method is used in order to get a detailed description of the bluetooth state. This method will return one of the four things:

  • STATE_TURNING_ON
  • STATE_ON
  • STATE_TURNING_OFF
  • STATE_OFF

The Bluetooth adapter is off by default to conserve battery life. The code above could have been modified to include the state like this:

    String state = bluetooth.getState();
    status = mydevicename + ” : ” + mydeviceaddress + " : " + state;

Conclusion

This has been a very quick look at the BluetoothAdapter class and some of the fundamental methods used when just getting started with Bluetooth on Android. The code included above will check if Bluetooth is enabled and display diagnostic information if it is and an error message if it isn’t.

NOTE: In order to test Bluetooth applications, you must run this code on an actual Android device. It will not work in the emulator.

Add Comment

Discussion 7 Comments

  1. Josh Mathews says:

    Thanks a lot for stepwise Bluetooth tutorial. I was about to create an application that would be having Bluetooth and Wifi interaction.

  2. keith says:

    Great tutorial.

    Maybe you should display the finish code at the end and maybe display the entire manifest file?

  3. manisha says:

    Gud tutorial,

    i want to know something?
    is Emulater support Bluetooth?

  4. Maddy J says:

    @ Manisha – Hey Manisha, Emulator doesn’t support Bluetooth, Only way is to test it in Mobile.

    @ Keith – Let’s see, if i can share code. Will get back to you shortly.

  5. phan2om says:

    Maddy J,

    Thanks for this tutorial. Some feedback, the getState method returns an int, so I had to change the state variable from String to an int in order to successfully compile the code. Works great!

  6. suhasini says:

    if bluetooth is not enabled in mobile device , then how could i get the name of that mobile device without enabling bluetooth?

  7. Dennis G. says:

    Great tutorial! Big Thanks!

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.