bluetooth low energy stack for linux
How is bluetooth low energy implemented at a device driver level in linux? Is there a new bluetooth stack code for every new bluetooth version ?
3 Answers 3
Bluez is the stack for Linux Bluetooth. New version of Bluetooth have resulted in changes in the code, but it’s not a complete rewrite or anything drastic like that. Newer versions of Bluetooth are typically extensions of the existing protocol with the exception of Low Energy. Low Energy is completely supported at the kernel level in Linux, but there aren’t a lot of convenience libraries for higher level interaction with LE.
Due to the incompleteness of Bluez with LE I’ve had to interact with the kernel code directly in order to implement functionality with LE. Also, I had to use older kernels that aren’t supported by Bluez 5.x and had to use Bluez 4.x.
Already Tim and jhonnash have provided comprehensive information to your question. However, I would like to add one more information from Android Mobile point of view.
New version of Android uses Bluedroid as Bluetooth stack. This is quite different from Bluez.
Bluez has functionality distributed both in user space and kernel space. All the protocols like RFCOMM, HCI and L2CAP are implemented at kernel level and Profiles are implemented at User space. As Tim mentioned, to support Low energy, Bluez has to provide support at kernel level as well as at user space. This some times get difficult to track and understand. Any new changes may require modification in kernel as well as in user space code.
On the other hand, Bluedroid has some advantage over Bluez. Bluedroid has implemented its complete functionality at user space. Due to which any new functionality only needs to be added at user level code. No dependency on Kernel code.
Also, to answer your question of «Is there a new bluetooth stack code for every new bluetooth version ?» No complete rewrite is required for new version of Bluetooth as Bluetooth new versions are backward compatible, so only those portion of codes are added to the already existing stack which is new.
How to Get Started with Bluetooth Low Energy on Linux
Bluetooth Low Energy is awesome. The problem, even in 2019, it’s still can be tough to work with.
In this article, you’ll learn the in’s and outs of Bluez. It’s Linux’s answer to connecting and using Bluetooth devices from the command line. It’s open source and it’s easy to use.
First, lets discuss the most important tools that are included with the Bluez stack.
Sidenote: things have changed a bunch since I first wrote this. It’s becoming much easier to create Bluetooth based hardware. I’ll show you how much in this handy how-to.
Get Acquainted With The Tools
hcitool
hcitool is the swiss army knife for Bluetooth in Linux. It is aptly named hcitool as it communicates via a common HCI (Host Controller Interface) port to your bluetooth devices. You can utilize the utility to scan for devices and send commands/data for standard Bluetooth and Bluetooth Low Energy.
gatttool
gatttool is used to access the “services” running on your bluetooth device. In my case, it was instrumental in accessing the Bluetooth Low Energy services running on my nrf51822. The Bluetooth SIG has already spec’d out several services for general use. More information on these services can be found here.
Install The Tools
In order to use Bluez, I needed to install Linux on a virtual machine. If you need a helping hand installing Debian may I suggest this step-by-step guide?
- Get the latest Bluez source:
wget https://www.kernel.org/pub/linux/bluetooth/bluez-5.18.tar.xz
Note: make sure you uninstall Bluez if it is already installed. (apt-get doesn’t have a very recent version) To check if it’s installed run this command:
dpkg --get-selections | grep -v deinstall | grep bluez
sudo apt-get install libglib2.0-dev libdbus-1-dev libusb-dev libudev-dev libical-dev systemd libreadline-dev
make -j8 && sudo make install
sudo cp attrib/gatttool /usr/local/bin/
Scan For Bluetooth Low Energy Devices
After installing Bluez (and tools) lets see if we can find a Bluetooth Low Energy device.
Note: because my Mac does not have built in Bluetooth Low Energy I am using a cheap Bluetooth Low Energy dongle. These dongles can usually be picked up for around $8.
- Attach your Bluetooth device to your VM. Note: if you get a Failed to attach USB device error, you may need to create a device filter that will autoconnect. More information on creating a device filter here.
- Check if hcitool can see your device:
Devices: hci0 00:15:83:6B:CC:EB
Connect To Your Bluetooth Low Energy Device
Now that we can see the intended Bluetooth device let’s connect to it.
The command above will generate a list of all the available “services” running on the low energy device. This could give you an idea of what type of functionality the device has (if you have no idea how it works). An example output is:
attr handle: 0x0001, end grp handle: 0x0007 uuid: 00001800-0000-1000-8000-00805f9b34fb attr handle: 0x0008, end grp handle: 0x000b uuid: 00001801-0000-1000-8000-00805f9b34fb attr handle: 0x000c, end grp handle: 0xffff uuid: 0000180f-0000-1000-8000-00805f9b34fb
This command generates a list of all the available handles. Handles are the “connection points” where you can read and write access data. For instance, my device has a handle to access battery level data and another to enable automatic reporting of the battery level data. An example output is:
handle: 0x0001, uuid: 2800 handle: 0x0002, uuid: 2803 handle: 0x0003, uuid: 2a00 handle: 0x0004, uuid: 2803 handle: 0x0005, uuid: 2a01 handle: 0x0006, uuid: 2803 handle: 0x0007, uuid: 2a04 handle: 0x0008, uuid: 2800 handle: 0x0009, uuid: 2803 handle: 0x000a, uuid: 2a05 handle: 0x000b, uuid: 2902 handle: 0x000c, uuid: 2800 handle: 0x000d, uuid: 2803 handle: 0x000e, uuid: 2a19 handle: 0x000f, uuid: 2902 Discover descriptors finished: No attribute found within the given range
Characteristic value/descriptor: 64
So, when I run char-write-req 0x000f 0100 I start receiving battery level messages (if the level has changed) like the following:
Characteristic value was written successfully Notification handle = 0x000e value: 64 Notification handle = 0x000e value: 64 Notification handle = 0x000e value: 64
More resources
As you can imagine, there’s more to learn when it comes to BLE. Here are some links:
Moving forward
We have only reached the tip of the iceberg in terms of information regarding Bluetooth (and Bluetooth Low Energy). Stay tuned for more Bluetooth Low Energy related posts in the future!
In the meantime subscribe below to my list if you haven’t already and i’ll see you on the next one!