笔者目前做linux系统下bluez蓝牙项目开发,发现网上关于bluez开发的资料很少。对于刚开始接触bluez蓝牙的开发人员来说是非常痛苦的。通过调试bluez源码自带的应用例子和文档说明,对BlueZ5.45 D-Bus总线 GATT API有了一些感悟。笔者不才,愿意将所了解到的一些知识分享给大家,希望能带给大家一些帮助。
一、首先看一下gatt-api说明文档,路径如下:Bluez-5.45/doc/gatt-api.txt
1、Service hierarchy
外部应用首先要用GattManager1中的注册方法注册该服务才能使本地服务生效,而且使这些方法和属性生效,还要在GattService1 接口声明,以下是gatt-api.txt文档中关于Service hierarchy说明
Service org.bluez
Interface org.bluez.GattService1
Object path [variable prefix]/{hci0,hci1,...}/dev_XX_XX_XX_XX_XX_XX/serviceXX
Properties string UUID [read-only]
128-bit service UUID.
boolean Primary [read-only]
Indicates whether or not this GATT service is a
primary service. If false, the service is secondary.
object Device [read-only, optional]
Object path of the Bluetooth device the service
belongs to. Only present on services from remote
devices.
array{object} Includes [read-only]: Not implemented
Array of object paths representing the included
services of this service.
为了便于理解以gatt-service.c文档为例说明:
程序开始定义了以下接口:
#define GATT_MGR_IFACE "org.bluez.GattManager1"
#define GATT_SERVICE_IFACE "org.bluez.GattService1"
#define GATT_CHR_IFACE "org.bluez.GattCharacteristic1"
#define GATT_DESCRIPTOR_IFACE "org.bluez.GattDescriptor1"
其中GATT_SERVICE_IFACE为org.bluez.GattService1接口
然后定义了服务uuid和对应的特征值uuid
/* Immediate Alert Service UUID */
#define IAS_UUID "ea816b5a-129c-4941-8a20-1c2ae3239f02"
#define ALERT_LEVEL_CHR_UUID "7df357bb-4f53-49c0-861e-84aa2be57f65"
#define UPDATE_CHR_UUID "b5635b6e-02cb-4fe8-a4a2-3c0279f53755"
其中#define IAS_UUID "ea816b5a-129c-4941-8a20-1c2ae3239f02"为服务uuid
然后创建和注册该服务:
create_services()->service_path = register_service(IAS_UUID)->g_dbus_register_interface(connection, path, GATT_SERVICE_IFACE,
NULL, NULL, service_properties,
g_strdup(uuid), g_free)
为该服务注册两个特征值:
/* Add Alert Level Characteris