C语言中的低功耗蓝牙-使用Bluez创建GATT服务器 [英] Bluetooth Low Energy in C - using Bluez to create a GATT server

查看:1133
本文介绍了C语言中的低功耗蓝牙-使用Bluez创建GATT服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Linux机器上使用GATT来设置具有许多特征的自定义服务.

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

使用这个问题及其链接的内容,我能够识别出需要编写的代码(使用gatt_service_add()函数).

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

我创建了一个名为gatt_service.c的单独文件,并编写了我认为需要的代码.但是,我无法解决如何将我的代码链接到Bluez库以编译和运行我的解决方案.例如,此站点(虽然不是为了BLE开发)使用-lbluetooth作为gcc参数链接到libbluetooth,我无法确定要链接的内容以使我的代码正常工作.

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.

谢谢.

编辑-更多信息:

在评论之后,我以plugins/time为基础为我自己的"Broadcaster服务"编写了自己的文件.完整代码位于:此处(我不知道要在回答!).

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

我的编译命令是: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(包括用于修复报告的问题的glib位

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

我得到的错误是:gatt_broadcaster_service.c:11:27:致命错误:lib/bluetooth.h:没有这样的文件或目录#include"lib/bluetooth.h"

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"

我的C文件存储在Documents中,我的研究告诉我它找不到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.

再次感谢!

推荐答案

我获得了运行在BlueZ 5.31上的示例GATT服务器(截至本文的最新内容):

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

我的环境:
流浪汉
虚拟盒子
Ubuntu Trusty Tahr作为来宾操作系统(v.14.04 32位操作系统)
更新到Linux内核3.19
已安装的软件包:
* libglib2.0-dev
* libdbus-1-dev
* libudev-dev
* libical-dev
* libreadline-dev

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

从此处下载BlueZ 5.31: https://www.kernel.org/pub /linux/bluetooth/bluez-5.31.tar.xz
安装更新的内核(3.19版):
sudo apt-get更新
sudo apt-get install --install-recommends linux-generic-lts-vivid

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

必须重新启动.我正在使用Vagrant,并且失去了共享文件夹的访问权限.如果您遇到这种情况,请等待流浪者报告错误并无论如何都要进入VM(流浪ssh).在VM中,发出以下命令来解决共享文件夹问题:
sudo/etc/init.d/vboxadd设置

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

我将再次重新启动(可能没有必要),以检查共享文件夹是否再次处于活动状态.
回到虚拟机后,继续安装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
焦油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
制作
sudo make install
sudo cp attrib/gatttool/usr/bin

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

安装完成.进行如下检查:
hciconfig
您应该得到以下响应(或类似响应):
hci0:类型:BR/EDR总线:USB
BD地址:00:1A:7D:DA:71:0C ACL MTU:310:10 SCO MTU:64:8
DOWN
RX字节:15528 acl:126 sco:0事件:683错误:0
TX字节:6459 acl:146 sco:0命令:234错误:0

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

配置蓝牙适配器,启动广告,启动示例GATT服务器(心率服务)并启用详细信息(我的适配器为hci0):
cd BlueZ 5.31目录
sudo工具/btmgmt -i hci0关机
sudo工具/btmgmt -i hci0 le on
sudo工具/btmgmt -i hci0可连接在
sudo tools/btmgmt -i hci0名称"一些友好的名称"
sudo工具/btmgmt -i hci0在
上投放广告 sudo工具/btmgmt -i hci0开机
工具/btgatt服务器-i hci0 -s低-t公共-r -v

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

转到另一台设备(我使用了iPod,Android(三星Galaxy 5S和Nexus平板电脑)以及另一台运行BlueZ的PC)并连接到该服务.这是我在另一台运行BlueZ的PC上完成此操作的方法:
gatttool -b GATT服务器的MAC地址 -I
连接
小学
特征

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

您可以发出其他命令来读写GATT服务器.

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

我还通过复制和编辑以下文件创建了一个自定义GATT服务器(您的原始请求): tools/btgatt-server.c .您可以编辑Makefile.tools文件,以将自定义服务器包括在构建中.您必须运行automake,make和sudo make install才能使其运行.

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.

这篇关于C语言中的低功耗蓝牙-使用Bluez创建GATT服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆