Bluetooth Low Energy: Read-Write to Sensor Network from Raspberry Pi

In this tutorial, I am using a RedBearLab Bluetooth Low Energy (BLE) shield connected to Arduino UNO to demonstrate the BLE communication from Raspberry Pi.

  • Install hard-float Raspberryian OS using this link.
  • Install Bluez using step 1, 2, 3 of this link.
  • I used CSR 4.0 dongle in the Raspberry Pi for BLE connection.
  • Make sure you have gatttool in the path using
    $which gatttool
    If it is not in the path, copy it from bluez/bluez-5.11/attrib
    $cp bluez/bluez-5.11/attrib/gatttool /usr/local/bin/
  • Check if the Bluetooth dongle is up and running
    ~/bluez/bluez-5.11 $ tools/hciconfig
    If not (you will see DOWN written in the result), use below command to get it up
    ~/bluez/bluez-5.11 $ tools/hciconfig hci0 up
  • Now scan for BLE devices. The command below might continue scanning forever, so type CTRL+C when you have the device address
    $ sudo hcitool lescan
    Assume you get the BLE device address xx:xx:xx:xx:xx:xx
  • Now connect with the device using gatttool
    $ sudo gatttool -b xx:xx:xx:xx:xx:xx -t random –interactive
    You might not need to use -t random. Also if you get device busy error, remove the dongle and reconnect it.  You will see an interactive prompt. Type connect like below
    [xx:xx:xx:xx:xx:xx][LE]> connect
    Attempting to connect to xx:xx:xx:xx:xx:xx
    Connection successful

Once it is connected, we want to write to a LED to turn in ON/OFF, and read the sensor data. We are using FSR as the sensor in this example. We will use gatttool to do the read & write.

Before we begin, we need to understand how this works fundamentally. You can go ahead and read this book or any other good tutorial that I am not aware of. But basically, you need to have some code on your Arduino that sets up a profile in the nRF8001 component on your RedBearLab shield that defines “pipes” which is the link between a characteristic exposed by the radio and a function from which you can read data or to which you can send data.

I am using SimpleControl sketch from RedBearLab as the code that runs on Arduino.  To do read-write, we need to know which address to write and what value to write.

Find the Address to Read & Write: 
I look at RedBearLab’s Android source code to figure out the TX, RX address.  In the, you will see

public static String BLE_SHIELD_TX = “713d0003-503e-4c75-ba94-3148f18d941e”;
public static String BLE_SHIELD_RX = “713d0002-503e-4c75-ba94-3148f18d941e”;

Now, if you type below in the gatttool interactive prompt, you can figure our the address (handle) of those TX, RX

[xx:xx:xx:xx:xx:xx][LE]> char-desc
handle: 0x000b, uuid: 713d0003-503e-4c75-ba94-3148f18d941e
handle: 0x000c, uuid: 2803
handle: 0x000d, uuid: 713d0002-503e-4c75-ba94-3148f18d941e
handle: 0x000e, uuid: 2902

So you basically see that the handle of TX, RX are 0x000b and 0x000d respectively.

How to Turn ON/OFF the LED:
From the, you can see that you need to write 010100 to the TX handle to turn the LED ON and 010000 to turn it OFF.

digitalOutBtn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
             byte buf[] = new byte[] { (byte) 0x01, (byte) 0x00, (byte) 0x00 };
             if (isChecked == true)
                  buf[1] = 0x01;
                  buf[1] = 0x00;

Now type below to turn the LED ON
[xx:xx:xx:xx:xx:xx][LE]>char-write-cmd 0x000b 010100

To Read Analog Data: 
Similarly from the, you can see that you need to write A00100 to the handle 0x000b to enable the analog reading capability through the RedBearLab shield. You also need to enable listening to the 0x000e handle. The 0x000e is the handle of the Client Characteristic Configuration (CCC) uuid 2902. You can find this 0x000e handle by typing char-read-uuid 2902 in the gatttool interactive prompt.

[xx:xx:xx:xx:xx:xx][LE]>char-write-cmd 0x000b A00100
[xx:xx:xx:xx:xx:xx][LE]>char-write-req 0x000e 0100 –listen

You will see sensor values in coming the the RX handle (0x000d)
Notification handle = 0x000d value: 0b 00 c0
Notification handle = 0x000d value: 0b 00 d0
Notification handle = 0x000d value: 0b 00 e4
Notification handle = 0x000d value: 0b 00 f0



12 thoughts on “Bluetooth Low Energy: Read-Write to Sensor Network from Raspberry Pi

  1. Pingback:
  2. Hi, I am using your tutorial to connect a raspberry pi to blend micro, but when I am in the step “Now connect with the device using gatttool”, I get the following error: Error: connect: connection refused (111). I have verified that the MAC address for the blend micro is correct, and that the bluetooth dongle on the raspberry pi supports BLE 4.0. What might be the problem here? Thanks!

  3. @nahidalam, Thanks for your response! I did what you suggested with no luck still… I was using bluez version 5.34 previously and then I switched to version 5.11 just to be consistent with what you did in this post, but I still get the error message: Error: connect: Connection refused (111). I have also searched online and it seemed that not many people are experiencing such problem… Do you think the problem might be with my bluetooth dongle on the raspberry pi? I am using a Inateck USB bluetooth 4.0 dongle (

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s