Home:ALL Converter>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)

Ask Time:2014-01-29T18:37:07         Author:Youssif Saeed

Json Formatter

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.

Author:Youssif Saeed,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/21428446/bluetooth-low-energy-use-bluez-stack-as-a-peripheral-with-custom-services-and
Isa A :

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 :\n\n gatt_service_add(adapter, GATT_PRIM_SVC_UUID, 0xFFFF,\n /* Char 1 */\n GATT_OPT_CHR_UUID16, 0xAAAA,\n GATT_OPT_CHR_PROPS, ATT_CHAR_PROPER_READ,\n GATT_OPT_CHR_VALUE_CB, ATTRIB_READ, read_func_callback,\n\n /* Char 2 Define here */\n ...\n /* Char 3 Define here */\n ...\n GATT_OPT_INVALID);\n }\n\n\nAlso, 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\"\n\nTo 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).",
2014-01-29T11:34:05
Jagdish :

1) goto Bluez folder\n\n2) sudo ./configure --prefix=/usr --mandir=/usr/share/man --sysconfdir=/etc --localstatedir=/var --disable-systemd --enable-experimental --enable-maintainer-mode\n\n3) sudo make all\n\n4) Advertise connectable packets\n\n# activate bluetooth\nsudo hciconfig hci0 up \n# set advertise data: \"hello world\"\nsudo hcitool -i hci0 cmd 0x08 0x0008 48 45 4c 4c 4f 57 4f 52 4c 44\n# start advertising as connectable\nsudo hciconfig hci0 leadv 0\n\n\n5) sudo service bluetooth stop\n\n6) sudo src/bluetoothd -d -n\n\n7) From other PC, type (Change MAC id gatt server mac)\n\ngatttool -b gatt_server_mac --interactive\n\n\nstep 6 is for in case you want to compile plugins/gatt-example.c \n\nif 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 \n\nsudo src/bluetoothd --plugin=time -n ",
2015-03-20T07:14:00
yy