Android Bluetooth serial port communication

Tablets and smartphones are everywhere and prices are dropping fast. ¬†A tablet provides a great user interface: it’s inexpensive, has a high resolution color touchscreen and it’s an ideal method to control an embedded system.

The most straightforward way to do this is with an embedded system exposing a web  interface over Wi-Fi. In this case, the tablet only needs a browser to connect. However, smaller embedded systems may not have this luxury. Here we will look at using an Android tablet to connect to a small embedded system using Bluetooth.

We won’t get into the details of the embedded system but for clarity’s sake, let’s say it’s a small Arduino measuring room temperature and connected to a Bluetooth transmitter. It sends a reading automatically once per second.

There is a lot of information online about using Bluetooth with the Android. The problem is that it is fragmented and few sites have all the information in one place. So I figured I’d compile a set of the major points you need to know to get the Bluetooth Serial Port Protocol (SPP) working on an Android app.

First, your app needs the BLUETOOTH and BLUETOOTH_ADMIN permissions. This goes in yourAndroidManifest.xml file.

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

Now that we have given the app permission to access the Bluetooth API, let’s look at the classes that are relevant. We’ll need the BluetoothAdapter, BluetoothDevice and BluetoothSocket classes to connect to the external system.

First, we need to find and pair with the device. The following code snippet builds a collection of Bluetooth devices that were discovered by your Android device.

    BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
    bta.startDiscovery();
    Set deviceList = bta.getBondedDevices();

Now that we have a list of external devices, we can iterate over the collection and extract the name and address of each device like this.

    for (BluetoothDevice i : deviceList)
    {
        String name = i.getName();
        String address = i.getAddress();
    }

You can select which one you need from the list and connect to it. Selecting the device is something you can decide how to handle yourself. Connecting to the device is done using its Bluetooth address. Once you have a selected device, we connect to it by requesting the device through its address. The address is a unique 48-bit ID assigned to each Bluetooth device.
Once we have the device, then we’ll open a socket using the standard UUID for the SPP serial port protocol to indicate that we want to connect to the device as if it were a standard serial port.

    BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
    BluetoothDevice device = bta.getRemoteDevice(address);
    BluetoothSocket socket = null;
    if (device != null)
    {
        UUID serialID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
        try
        {
            socket = device.createRfcommSocketToServiceRecord(serialID);
        }
        catch (java.io.IOException e)
        {
        }
    }

Next, we’ll handle sending and receiving data from the socket.