Question

I am trying to use the BlueZ stack on a Linux machine to create a GATT server with custom services and characteristics. The final goal is to use any central device (e.g. iOS or Android device) to connect to the GATT server, discover the services and characteristics, and manipulate the data in the characteristics.

Example:

  • Peripheral with 1 service which contains 3 characteristics.
  • Service uuid = 0xFFFF
  • Char 1 uuid = 0xAAAA, properties = readable
  • Char 2 uuid = 0xBBBB, properties = readable & writable
  • Char 3 uuid = 0xCCCC, properties = notifiable

From the central device, I should see the the peripheral device, connect to it and discover one service (0xFFFF) which has three characteristics (0xAAAA, 0xBBBB, 0xCCCC). I should then be able to read the value of 0xAAAA, read and write to the value of 0xBBBB, and enable notifications on 0xCCCC.

Please note that I am aware that a similar question exists, but it only explains how to use the peripheral as an advertiser. Another solved question explains how to create a GATT server, but does not explain how to play with the properties of the characteristics (e.g. readable, notifiable, etc.), or maybe I'm missing something.

Thank you in advance.

Was it helpful?

Solution

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).

OTHER TIPS

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 bluetooth
sudo 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 connectable
sudo 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top