Created: Mar, 2015;
Last modified: Mar, 2016
Bluez is a popular Bluetooth toolbox for Linux. At the time of writing, I am using Linux Mint 17 and Raspberry Pi.
Not surprisingly, the BlueZ source in Ubuntu and Debian is old. To get the latest features (e.g., those in Bluetooth V4.2), one can compile the whole thing from the latest source code.
First install the dependencies
Note that Bluez uses systemd which is not the default service manager in Ubuntu (it’s upstart). So I just skip installing systemd packages on Linux Mint to avoid potential headaches. Raspberry Pi Debian Linux uses systemd, so it should be okay. Download the latest Bluez source from Linux kernel’s website or Bluez download page. At the time of writing, the latest version is 5.29.
Next, install dependencies. For Ubuntu/Linux Mint
The first flag “–disable-systemd” disable compiling with systemd libraries since we will not use systemd anyway, otherwise you can get error like “configure: error: systemd system unit directory is required”. The second flag “–enable-library” installs Bluetooth library so that it is possible to develop your own BT applications in the future.
Finally, compile and install:
The last command installs gatttool system-wide since it is not automatically done in “make install”.
I am mostly interested in Bluetooth Low Energy (BLE) applications which becomes part of the Bluetooth stack since V4.0. Since my computer does not have BLE capability, I get a CSR4.0 BLE USB adapter from Amazon (here) which works pretty well. Note that this device only implements BT V4.0 stack, thus not having new features in BT V4.1 and V4.2.
Insert the CSR4.0 BLE USB dongle, and make sure you see something similar to the following after “lsusb”:
View all BT devices on the computer by
I see the following:
It turns out hci0 is the native built-in BT module on my computer, and hci1 is the CSR4.0 BLE USB dongle. Right now hci1 is still in “down” mode while hci0 is running. Now bring hci1 up by
After another “hciconfig hci1”, you will see hci1 becomes:
You can also list the active BT devices by
which will return the MAC addresses of active BT devices attached to the computer, like
Now we can do various things with hci1. For example, I have a SensorTag. I can scan the BLE devices by
It will continuously output information about BLE peripheral devices to the console like:
Now I can test a BLE connection to my SensorTag by starting and dropping a connection like
and it will return something like
meaning a connection was successful. Now close the connection:
By default the BLE dongle is inactive after being connected to the computer. Udev can help enable the device upon connection by adding this udev rule:
Note that the path for “hciconfig” may be different on different systems. Be sure to check the path with “which”.
The real fun part is to connect to a BLE peripheral like SensorTag and interact with “gatttool”. Jared Wolff’s post covers some basic usage.
The CSR4.0 BLE USB dongle can also be used to capture the advertizing packet data from other BLE devices. See discussion in this thread.
There is also a Python package for interfacing BLE on the shoulder of Bluez here. The package seems still a little preliminary, but does provide the most needed basic features.
One can make an iBeacon device with this little BLE USB dongle. For example, piBeacon does exactly such thing.
[Update: Mar 2016]
I recommend a NodeJS package called Noble for BLE scripting. I find it much easier to use than existing alternatives.