Pin Me

How to work with Bluetooth in Android: Basics

written by: Jbeerdev•edited by: Simon Hill•updated: 1/17/2011

This a development article where I will show you the basics of working with Bluetooth in Android.

  • slide 1 of 2

    Bluetooth in Android

    Android developers had to wait until SDK 2.0 to start working natively with Bluetooth, before this version, released on October 2009, we had to work with external unofficial libraries, useful indeed, but not supported by Google. It's important to have in mind that not all Android devices support the Bluetooth stack API, and not all Android devices that support it, have all the Bluetooth profiles. All this depends on the manufacturer.

    Since SDK 2.0, we can perform the following actions with an “ideal Android device” (one that supports Bluetooth and has all the Bluetooth profiles).

    • Switch on/off the Bluetooth.
    • Set device discoverable or hidden.
    • Discover Bluetooth devices.
    • Discover Bluetooth services.
    • Transfer data.

    Hint! - It's not the same discover devices that discover services. A Bluetooth device may have different services we can connect. For example, a printer Bluetooth device can have a service to send data to print black and white and another to send data to print in color. When you discover a Bluetooth device, you scan its services and choose which one you want to connect.

  • slide 2 of 2

    Basic Code

    First of all we have to ensure that we are working with Android SDK 2.0 or newer. When you create a new Android project you need to set the SDK target you want to work with.


    We cannot forget to add the user permission in the AndroidManifest.xml file.

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

    Switching on-off local Bluetooth code.

    Let's create a function to switch on your Bluetooth device:

    import android.bluetooth.BluetoothAdapter;


    public static void StartBluetooth(){

    BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();



    The BluetoothAdapter class is the representation of your Bluetooth device. Enabling Bluetooth is as simple as calling the “enable()” method.

    Disabling Bluetooth is not more complex:

    public static void StopBluetooth(){

    BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();



    Both methods (enable() and disable()) return a boolean value. If the returned value is true, the device is being enabled/disabled successfully, on the other hand, a false value means a failure in the process.

    Hint- Android documentation suggests not to use enable/disable methods without direct user consent.

    Checking Bluetooth status is simple too:

    public static boolean isBluetoothEnabled(){

    BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

    return btAdapter.isEnabled();


    If the device is enabled you get a true, if not a false.

    Hint- If you work with both an emulator and a real device, you will have problems with this function, you will get a null when BluetoothAdapter.getDefaultAdapter(); so it will be wise to place a try-catch:


    BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();


    }catch(NullPointerException ex){

    Log.e("Bluetooth","Device not available");


    Discovering devices, services and transferring data is a more complex matter. I will research, create code and write some articles about this in the near future.

    Although the code is really simple, I have placed the functions and more Bright Hub code in the following git repository:

    BrightHubCode Git Repository