【问题标题】:How to get Disconnect Event from GATT Server on Bluez/Linux如何在 Bluez/Linux 上从 GATT 服务器获取断开连接事件
【发布时间】:2014-03-07 13:06:10
【问题描述】:

环境:Bluez 5.14、Linux 3.1、USB Plugable BLE 无线电、TI BLE keyfob(CC2541 开发套件) Linux 设备 USB BLE Radio

我们使用 gatttool 在 TI keyfob 上启用按键事件并开始监听事件

gatttool -b [hardware ID] --char-write-req -a [handle] -n [value] --listen 
(gatttool -b 90:59:AF:09:E1:5D --char-write-req -a 0x0048 -n 0100 --listen)

按下钥匙扣上的按钮并查看这些事件

Notification handle = 0x0047 value: 02 
Notification handle = 0x0047 value: 00 
Notification handle = 0x0047 value: 02

因此我们可以通过 Bluez 堆栈从 Keyfob 接收按键事件

目标:

我们需要捕捉 GATT 断开连接事件,即当我们从密钥卡中取出电池时,GATT 连接迟早会断开。我们希望收到来自 Bluez 堆栈的断开连接事件。 Bluez 具有此功能,因为 Android 支持基于 Bluez 构建的 GATT 断开连接事件。

问题:

我们如何使用 Bluez 命令行 hcitool/gatttool 或 Bluez API 接收 GATT Disconnect 事件。

【问题讨论】:

  • 简单地说——我们如何发现 GATT 或蓝牙连接断开了。我们需要这个有很多原因 - 清理资源,重新启动 lescan 等等。

标签: bluetooth embedded-linux bluetooth-lowenergy bluez gatt


【解决方案1】:

注意 G_IO_HUP 并正常关闭。

chan = gatt_connect(opt_src, opt_dst, opt_dst_type, opt_sec_level,
                opt_psm, opt_mtu, connect_cb, &gerr);
if (chan == NULL) {
    log_printf(LOG_LEVEL_ERROR,"%s: chan is NULL\n",__func__);
    log_printf(LOG_LEVEL_ERROR,"%s\n", gerr->message);
    g_error_free(gerr);
    g_main_loop_quit(event_loop);
} else {
    log_printf(LOG_LEVEL_INFO,"Connected to %s\n",opt_dst);
    g_io_add_watch(chan, G_IO_HUP, channel_watcher, NULL);
}

【讨论】:

  • 这就像一个魅力,但是由于检测是在 GATT 层,因此由于电池移除而导致的断开连接需要很长时间才能检测到。在 BLE 层进行检测将是理想的选择。
  • 很高兴知道这是可能的。但是为什么“gatttool --listen”没有注意到断开连接并且永远挂在那里?仅仅是臭名昭著的 bluez bugginess 和用户不友好吗?以防万一,bluez 4.101(随 Ubuntu 14.04LTS 提供)。
猜你喜欢
  • 1970-01-01
  • 2018-02-27
  • 2014-11-16
  • 1970-01-01
  • 2019-08-08
  • 1970-01-01
  • 2014-01-08
  • 1970-01-01
相关资源
最近更新 更多