Using Linux as a BLE peripheral for testing. Check out the source code in this GitHub repo.

If you’ve written a BLE gateway program you know how difficult testing can get. The Linux Kernel controls all the BLE communications and you cannot communicate with your program from an external process. You can (and should) do some old-school integration tests with an actual BLE peripheral, but sometimes you want to test your code with a mock device instead. Tests that are using a mock peripheral are independent of device bugs and enable pre-hardware testing. At the age of continuous-integration and test automation you need to have independent tests for your gateway program.

This post discusses mocking of a GATT server (peripheral). The suggested method is adding an extra BLE dongle for testing, and then using it with a GATT server application. The Linux bluetooth drivers, Bluez, are not very well documented and creating a GATT server in Linux is not a very common task. If you’re looking for a way to create a Linux GATT server (in Python), this post is for you.

How it All Began

During the development of Jumper’s emulator, we needed a way to test the BLE emulator. We decided we would initiate two VMs, one as a GATT peripheral and one as a server. Next, we connected them to the virtual BLE medium and run tests on every edge case we could think about. Creating a GATT client was easy, there are quite a few open-source Python packages for that. As for the server, we couldn’t find any projects that did this right. My colleague, Dima Gilburd, ended up finding some code used for testing the BlueZ drivers, that was exactly what we were looking for! Dima made some minor changes and it was all set.

How it Works

Most Linux BLE applications utilize an HCI socket to communicate with the BLE controller. This is a low-level way of communicating (almost) directly with the controller. The good news: BlueZ already supports the GATT stack for you. By registering some basic functions to the D-Bus, you can easily implement your GATT service in Python (or any other common language), the BlueZ drivers will take care of advertising and discovering characteristics for you.

*note: if you are not familiar with the D-Bus, I recommend this post for getting started.

Get Your Hands Dirty

If you want to get started and create your own Linux GATT server, take a look at this GitHub repo. It has the same code we used, which is based on the tests written for the BlueZ drivers.

Feel free to post any questions, comments and thought in the comment section below. Let us know how are you testing your BLE applications.

Call for Input

If you find this interesting — signup to get updates from Jumper and join our closed beta.