Bluetooth Low Energy in C - Using Bluez to Build a GATT Server

I am trying to use GATT on my Linux box to configure a custom service with a load of features.

Using this question and the ones it refers to, I was able to identify the code I need to write (using the gatt_service_add() function).

I created a separate gatt_service.c file and wrote the code that I think I need. However, I cannot figure out how to link my code with the Bluez libraries to compile and run my solution. For example, this site (not yet for BLE development) links to libbluetooth using -lbluetooth as the gcc parameter, and I cannot figure out what to link this to make my code work.

I did not post any samples because I am not sure what to do if there is any that is required, or I did not say anything, please let me know.

Thanks in advance.

EDIT - Additional Information:

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

My compilation team: 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 problem reported here ).

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

My C file is stored in documents, and my research tells me that it cannot find lib / bluetooth.h because it does not look in the right place ( this and this talk about using include flags for the compiler when the file is not in common folders, but I can not do this work.

Thanks again!

+5
source share
5 answers

I got a GATT server example for BlueZ 5.31 (the last of this post):

My environment:
Vagrant
Virtual box
Ubuntu Trusty Tahr as a guest OS (version 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
Installing the updated kernel (version 3.19):
sudo apt-get update
sudo apt-get install --install-recommends linux-generic-lts-vivid

Reboot required. I am using Vagrant and have lost access to shared folders. If this happens to you, wait until the tramp reports an error and still enters the virtual machine (roaming ssh). In the virtual machine, run this command to fix the shared folder problem:
Configuring sudo / etc / init.d / vboxadd

I rebooted again (maybe not needed) to check if the shared folder is active.
Returning to the virtual machine, continue with 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
to do
sudo make install
sudo cp attrib / gatttool / usr / bin

Installation completed. Check it out as follows:
hciconfig
You should get an answer (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 instructions: 234 errors: 0

Set up a Bluetooth adapter, start advertising, run an example of a GATT server (heart rate service) with a detailed one (my adapter is hci0):
cd BlueZ catalog 5.31
sudo tools / btmgmt -i hci0 power off
sudo tools / btmgmt -i hci0 le

sudo tools / btmgmt -i hci0 to connect to
sudo tools / btmgmt -i hci0 name " friendly name "
sudo tools / btmgmt -i hci0 ads on
sudo tools / btmgmt -i hci0 power on
tools / btgatt-server -i hci0 -s low -t public -r -v

Switch to another device (I used an iPod, Android - Samsung Galaxy 5S and a Nexus tablet - and another computer with BlueZ) and connected to the service. Here's how I did it on another PC running BlueZ:
gatttool -b GATT server MAC -I
to plug
primary
specifications

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

I also created my own 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 own server in the assembly. You will need to run automake, make and sudo make install to run it.

+11
source

1) go to the 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) Advertising plugins

activate bluetooth

sudo hciconfig hci0 up

set advertising data: "hello world"

sudo hcitool -i hci0 cmd 0x08 0x0008 48 45 4c 4c 4f 57 4f 52 4c 44

start advertising as a plugin

sudo hciconfig hci0 leadv 0

5) sudo service bluetooth stop

6) sudo src / bluetoothd -d -n

7) On the other PC, enter (change the MAC address of the gatt-server mac)

gatttool -b gatt_server_mac --interactive

step 6 is if you want to compile plugins / gatt -example.c

if you want to compile server.c from a profile / time or a profile / notification (replace with an alert instead of time) or any other file in the profile folder replace step 6

sudo src / bluetoothd --plugin = time -n

+2
source

I am creating a time server, good for a start. profile / time has a good user example, clears the code and tries to maintain minimal code and run with gatttool as a client. If you want both a user client and a custom server, than you can see tools / btgatt-client.c and tools / btgatt-server.c .

Run tools / btgatt-server.c on one PC with sudo./btgatt-server on one PC

and sudo./btgatt-client -d server_mac in another PC

+1
source

Forget bluez for the BTLE GATT server. Use Bunget for Linux.

0
source

I landed this question several times in search of a standalone GATT server for C. The answers here require creating your GATT server using the BlueZ package. BlueZ added D-Bus support to provide standalone GATT servers and even includes a standalone GATT server sample for Python, but not for C.

I still need a server, I started working on one with the intention of releasing the sample, but even a minimal implementation is even more code than it would be convenient for me to call the sample.

The initial implementation is now in progress and has been released under the GPL-3:

https://github.com/nettlep/gobbledegook

If it helps someone, cool. If this helps someone who released the code that helped me, even better.

0
source

Source: https://habr.com/ru/post/1215614/


All Articles