Bluetooth Low Energy: Use BlueZ stack as a peripheral (with custom services and characteristics) Bluetooth Low Energy: Use BlueZ stack as a peripheral (with custom services and characteristics) linux linux

Bluetooth Low Energy: Use BlueZ stack as a peripheral (with custom services and characteristics)


You can see gatt-example practice, or defined profiles under profile/ directory such as alert/server.c. Basically, you just have to register your service using gatt_service_add() function, following the existing code. For example :

 gatt_service_add(adapter, GATT_PRIM_SVC_UUID, 0xFFFF,    /* Char 1 */    GATT_OPT_CHR_UUID16, 0xAAAA,    GATT_OPT_CHR_PROPS, ATT_CHAR_PROPER_READ,    GATT_OPT_CHR_VALUE_CB, ATTRIB_READ, read_func_callback,    /* Char 2 Define here */    ...    /* Char 3 Define here */    ...    GATT_OPT_INVALID); }

Also, I forgot the details but in order to get alert server working, you need to enable experimental (and maintainer mode?) during configuration by adding "--enable-maintainer-mode" and "--enable-experimental"

To run, run the compiled "bluetoothd" with -n and -d options to debug (also -E for enabling experimental services). You may want to reset your adapter again after running bluetoothd. And then you can connect from remote device using gatttool (also with bluetoothd running on remote device).


1) goto Bluez folder

2) sudo ./configure --prefix=/usr --mandir=/usr/share/man --sysconfdir=/etc --localstatedir=/var --disable-systemd --enable-experimental --enable-maintainer-mode

3) sudo make all

4) Advertise connectable packets

# activate bluetoothsudo hciconfig hci0 up                                             # set advertise data: "hello world"sudo hcitool -i hci0 cmd 0x08 0x0008 48 45 4c 4c 4f 57 4f 52 4c 44# start advertising as connectablesudo hciconfig hci0 leadv 0

5) sudo service bluetooth stop

6) sudo src/bluetoothd -d -n

7) From other PC, type (Change MAC id gatt server mac)

gatttool -b  gatt_server_mac --interactive

step 6 is for in case you want to compile plugins/gatt-example.c

if you want to compile server.c from profile/time or profle/alert(replace with alert in place of time) or anyother file in profile folder replace step 6

sudo src/bluetoothd --plugin=time -n


Another solution now exists with Bluetoothctl command line tool. More details of this can be found here:-

BlueZ: How to set up a GATT server from the command line

I'm not sure when Bluetoothctl was introduced, but it's relatively recent at the time of this writing and therefore it didn't exist in previous versions of BlueZ. I'm using BlueZ v5.50 and this the version that I tested it with.