Bluetooth Low Energy in C - using Bluez to create a GATT server

Alex picture Alex · Mar 18, 2015 · Viewed 27.5k times · Source

I'm attempting to use GATT on my Linux box to set up a custom service with a load of characteristics.

Using this question, and the ones it links to, I was able to identify the code that I would need to write (making use of the gatt_service_add() function).

I've created a separate file called gatt_service.c, and written the code I think that I need. However, I cannot work out how to link my code to the Bluez libraries in order to compile and run my solution. For example this site (whilst not being for BLE development) links against libbluetooth using -lbluetooth as a gcc parameter, and I cannot work out what to link against to make my code work.

I haven't posted any samples because I'm not sure what to post - if there is any that is required, or I've failed to mention something, please let me know.

Thanks in advance.

EDIT - more information:

Following the comments, I've used plugins/time as a base to write my own file for my own "Broadcaster service". Full code located: here (I don't know which bit of the code to put in the answer!).

My compilation command is: gcc gatt_broadcaster_service.c -Wall -o gatt_broadcaster_service -lbluetooth 'pkg-config --cflags --libs glib-2.0' -I/home/alexander/Documents/bluez-5.29/lib (including the glib bit to fix the issue reported here).

The error I get is: gatt_broadcaster_service.c:11:27: fatal error: lib/bluetooth.h: No such file or directory #include "lib/bluetooth.h"

My C file is stored in Documents, and my research tells me that it can't find lib/bluetooth.h because it's not looking in the correct place (this and this talk about using include flags for the compiler when a file isn't in the general locations, but I can't make that work.

Thanks again!

Answer

Johnas Cukier picture Johnas Cukier · Jun 24, 2015

I got the example GATT server running for BlueZ 5.31 (latest as of this post):

My environment:
Vagrant
Virtual Box
Ubuntu Trusty Tahr as a guest OS (v. 14.04 32-bit OS)
Updated to linux kernel 3.19
Installed packages:
* libglib2.0-dev
* libdbus-1-dev
* libudev-dev
* libical-dev
* libreadline-dev

Downloaded BlueZ 5.31 from here: https://www.kernel.org/pub/linux/bluetooth/bluez-5.31.tar.xz
Installation of updated kernel (v. 3.19):
sudo apt-get update
sudo apt-get install --install-recommends linux-generic-lts-vivid

A reboot is necessary. I'm using Vagrant and lost shared folder access. If this happens to you, wait for vagrant to report the error and go into the VM anyway (vagrant ssh). In the VM, issue this command to fix the shared folder problem:
sudo /etc/init.d/vboxadd setup

I would reboot again (probably not necessary), to check that the shared folder is active again.
Once back in the VM, continue the installation of BlueZ 5.31:
cd ~
sudo apt-get install libglib2.0-dev libdbus-1-dev libudev-dev libical-dev libreadline-dev
wget https://www.kernel.org/pub/linux/bluetooth/bluez-5.31.tar.xz
tar xvf bluez-5.31.tar.xz
cd bluez-5.31
./configure --prefix=/usr --mandir=/usr/share/man --sysconfdir=/etc --localstatedir=/var --disable-systemd --enable-experimental --enable-maintainer-mode
make
sudo make install
sudo cp attrib/gatttool /usr/bin

Installation completed. Check it as follows:
hciconfig
You should get the follow response (or something similar):
hci0: Type: BR/EDR Bus: USB
BD Address: 00:1A:7D:DA:71:0C ACL MTU: 310:10 SCO MTU: 64:8
DOWN
RX bytes:15528 acl:126 sco:0 events:683 errors:0
TX bytes:6459 acl:146 sco:0 commands:234 errors:0

Configure the Bluetooth adapter, start advertising, start example GATT server (heart rate service) with verbose on (my adapter is hci0):
cd BlueZ 5.31 directory
sudo tools/btmgmt -i hci0 power off
sudo tools/btmgmt -i hci0 le on
sudo tools/btmgmt -i hci0 connectable on
sudo tools/btmgmt -i hci0 name "some friendly name"
sudo tools/btmgmt -i hci0 advertising on
sudo tools/btmgmt -i hci0 power on
tools/btgatt-server -i hci0 -s low -t public -r -v

Go to another device (I've used an iPod, an Android -- Samsung Galaxy 5S and Nexus tablet -- and another PC running BlueZ) and connect to the service. Here is how I did it on another PC running BlueZ:
gatttool -b MAC address of GATT server -I
connect
primary
characteristics

You can issue other commands to read and write to the GATT server.

I've also created a custom GATT server (your original request) by copying and editing this file: tools/btgatt-server.c. You can edit the Makefile.tools file to include your custom server in the build. You'll have to run automake, make, and sudo make install to get it running.