RPi Bluetooth LE

From eLinux.org
Revision as of 13:32, 17 March 2014 by IanH (talk | contribs)
Jump to: navigation, search

Bluetooth LE on the Raspberry Pi

At the time of writing Bluetooth Low Energy (BLE) is not well supported by the standard Raspberry Pi distributions. Some work is required to get it working, as described below.

Pre-requisites

Software

This page has been tested using the Raspbian Wheezy distribution, in particular the image at:

http://downloads.raspberrypi.org/raspbian/images/raspbian-2014-01-09/2014-01-07-wheezy-raspbian.zip

For reference, uname -a on the test system returned:

Linux raspberrypi 3.10.25+ #622 PREEMPT Fri Jan 3 18:41:00 GMT 2014 armv6l GNU/Linux  

Hardware

You will need a Bluetooth 4.0 compatible USB adapter to use with the Pi. The following devices have been tested:

BlueZ installation

By default, the Wheezy distribution comes without a Bluetooth stack. The bluez package is version 4.99, which has patchy support for Low Energy. You can build and install a more modern version as follows:

BlueZ 5.4 build instructions

Firstly, you'll need a download and install a number of development libraries. From a command prompt run:

sudo apt-get install libdbus-1-dev libglib2.0-dev libdbus-glib-1-dev
sudo apt-get install libusb-dev libudev-dev libreadline-dev

Then, download the source:

mkdir -p work/bluepy
cd work/bluepy
wget https://www.kernel.org/pub/linux/bluetooth/bluez-5.4.tar.xz
xz -d bluez-5.4.tar.xz
tar xvf bluez-5.4

If successful, you'll now have a bluez-5.4 source code directory on disk. To build it, do:

cd bluez-5.4
./configure --disable-systemd
make

This will take half an hour or so. When this is done, do:

sudo make install

First tests

USB Checks

To ensure the USB Bluetooth device is being seen, run

lsusb 

With the Plugable adapter shown above, the result will be similar to this:

Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. 
Bus 001 Device 004: ID 0a5c:21e8 Broadcom Corp. 

The adapter is the one on Bus 001 Device 004. To show more information about the 'Broadcom' device, do:

sudo lsusb -v -d 0a5c:

If this is working, run the hciconfig tool:

pi@raspberrypi ~ $ hciconfig
hci0:	Type: BR/EDR  Bus: USB
	BD Address: 00:02:72:14:27:0E  ACL MTU: 1021:8  SCO MTU: 64:1
	DOWN 
	RX bytes:2715 acl:1 sco:0 events:146 errors:0
	TX bytes:2500 acl:0 sco:0 commands:133 errors:0

Basic BlueZ commands

If the bluetooth adapter (hci0 in the above example) is down as DOWN, you will need to enable it with:

sudo hciconfig hci0 up

If you have any Bluetooth LE devices, you can start scanning for them with:

sudo hcitool lescan



Links: other Bluetooth Low Energy resources