【全志T113-S3_100ask】12-3 Linux蓝牙通信实战(基于BlueZ的C语言BLE蓝牙编程
背景
(温馨提示:全文约2700字)
前面我们在【全志T113-S3_100ask】12-2 Linux蓝牙通信实战(BLE初窥)通过 bluetoothctl 添加了蓝牙服务,并且通过BLE调试助手读写数据,但是我们没有办法
使用程序处理我们收发的数据。
在网上找了若干篇关于BLE的C语言编程,都不尽人意,可能是分析一下函数、结构体,可能是研究一下启动的流程,可能是用命令行去操作,可能是说一下dbus啥啥啥的。因为确实没有相关的官方教程(可能是我不会找),只能硬着头皮去看源码。
经过一段时间的摸(diào)索(fà),成功实现 基于BlueZ的C语言BLE蓝牙编程
能够自定义编写GATT服务,自定义特征值,并且可以通过安卓程序读写数据,并且根据上传的数据进行解析实现控制。
以下内容是我研究的过程,如有不对请多多指教。
(一)获取BlueZ源码
本教程是基于全志T113芯片进行操作,其他芯片所存在差异,本教程仅作参考。
1、在buildroot目录下通过find 命令查找bluez的包在哪里
find ./* -name "blue*"
root@DESKTOP-6HAHL0U:~/buildroot-100ask_t113-pro/buildroot# find ./* -name "blue*"
./dl/bluez5_utils
./dl/bluez5_utils/bluez-5.63.tar.xz
./dl/linux/git/net/bluetooth
./dl/linux/git/arch/powerpc/configs/44x/bluestone_defconfig
./dl/linux/git/arch/powerpc/boot/dts/bluestone.dts
./dl/bluez5_utils/bluez-5.63.tar.xz
就是我们想要的软件包。
2、将它复制到某个目录下,然后解压
root@DESKTOP-6HAHL0U:~/buildroot-100ask_t113-pro/buildroot# cd
root@DESKTOP-6HAHL0U:~# mkdir bluez
root@DESKTOP-6HAHL0U:~# ls
bluez
root@DESKTOP-6HAHL0U:~# cd bluez
root@DESKTOP-6HAHL0U:~/bluez# cp buildroot-100ask_t113-pro/buildroot/dl/bluez5_utils/bluez-5.63.tar.xz ./
root@DESKTOP-6HAHL0U:~/bluez# ls
bluez-5.63.tar.xz
root@DESKTOP-6HAHL0U:~/bluez# xz -d bluez-5.63.tar.xz
root@DESKTOP-6HAHL0U:~/bluez# ls
bluez-5.63.tar
root@DESKTOP-6HAHL0U:~/bluez# tar -xvf bluez-5.63.tar
bluez-5.63/
bluez-5.63/Makefile.plugins
bluez-5.63/emulator/
bluez-5.63/emulator/bthost.h
........
root@DESKTOP-6HAHL0U:~/bluez# ls
bluez-5.63 bluez-5.63.tar
root@DESKTOP-6HAHL0U:~/bluez# cd bluez-5.63/
root@DESKTOP-6HAHL0U:~/bluez/bluez-5.63# ls
AUTHORS ChangeLog Makefile.in Makefile.plugins README aclocal.m4 btio completion config.sub depcomp emulator install-sh mesh obexd profiles test-driver
COPYING INSTALL Makefile.mesh Makefile.tools TODO android client config.guess configure doc gdbus lib missing peripheral src tools
COPYING.LIB Makefile.am Makefile.obexd NEWS acinclude.m4 attrib compile config.h.in configure.ac ell gobex ltmain.sh monitor plugins test unit
虽然东西有点多,但是本次我们使用的是以下这个目录 peripheral
,译为 外围设备
里面的这几个文件可以直接编译出一个可执行文件,但是没有Makefile
(二)首次编译
在操作之前,需要保证你的环境是正常的。正常来说,你所获取的源码可以编译出一个读取设备名称的功能。
2-1 编写Makefile
vi Makefile
以下是在我的环境下编写的Makefile,主要是链接库文件,当然有更好的写法,仅供参考
。
# 以下是指定编译器
CC = $(CROSS_COMPILE)gcc
# 以下是指定编译需要的头文件
CFLAGS = -g -Wall -O0 -I./ \
-I/root/buildroot-100ask_t113-pro/buildroot/output/build/bluez5_utils-5.63/
# 以下是源⽂件
SRCS = main.c ./attach.c ./efivars.c ./gap.c ./gatt.c ./log.c
# 以下是指定需要的库文件.o
LIBS = -L/root/buildroot-100ask_t113-pro/buildroot/output/build/bluez5_utils-5.63/lib/.libs/ \
-lbluetooth-internal -lbluetooth
# 以下是指 当所有.c文件变成.o文件
OBJS = $(SRCS:.c=.o)
# 以下是生成可执行文件
EXECUTABLE = bt_app
O_DIR = /root/buildroot-100ask_t113-pro/buildroot/output/build/bluez5_utils-5.63/src/shared/.libs/
MY_O = $(O_DIR)libshared_glib_la-hci.o $(O_DIR)libshared_glib_la-gap.o \
$(O_DIR)libshared_glib_la-queue.o $(O_DIR)libshared_mainloop_la-mainloop.o $(O_DIR)libshared_glib_la-gatt-helpers.o \
$(O_DIR)libshared_mainloop_la-io-mainloop.o $(O_DIR)libshared_glib_la-hci-crypto