用于来自多个设备的 BLE 通知的 Bluetoothctl 或 gatttool

Lui*_*uis 2 bluetooth bluetooth-lowenergy bluetooth-gatt

我已经阅读了有关该主题的许多问题,但没有找到有关如何最好地(或者甚至可能)使用任何库或 API(最好是命令行或 Python 库)一次接收来自多个设备的通知的信息。

连接到不同的设备(例如心率监测器和电话,或两个心率监测器)后,是否有办法同时从每个设备接收 1 项服务的数据?

编辑:

我已经设法连接具有相同特征的不同设备,并且只要我不请求相同的服务,就能够使用Bluetoothctl(Bluez的一部分)从它们获取通知,这部分回答了我的问题;不过,有人知道更好的方法吗?

小智 6

1)首先,有一个 github python 项目可以在 Raspberry Pi 上的 Linux 中执行此操作: https: //github.com/IanHarvey/bluepy

\n\n

2) 其次,Anthony Chiu 的这个片段使用该 API 通过通知与多个设备进行通信:

\n\n
  from bluepy.btle import Scanner, DefaultDelegate, Peripheral\n    import threading\n\n    class NotificationDelegate(DefaultDelegate):\n\n    def __init__(self, number):\n        DefaultDelegate.__init__(self)\n        self.number = number\n\n    def handleNotification(self, cHandle, data):\n        print \'Notification:\\nConnection:\'+str(self.number)+ \\nHandler:\'+str(cHandle)+\'\\nMsg:\'+data\n\n    bt_addrs = [\'00:15:83:00:45:98\', \'00:15:83:00:86:72\']\n    connections = []\n    connection_threads = []\n    scanner = Scanner(0)\n\n    class ConnectionHandlerThread (threading.Thread):\n        def __init__(self, connection_index):\n            threading.Thread.__init__(self)\n            self.connection_index = connection_index\n\n        def run(self):\n            connection = connections[self.connection_index]\n            connection.setDelegate(NotificationDelegate(self.connection_index))\n            while True:\n                if connection.waitForNotifications(1):\n                    connection.writeCharacteristic(37, \'Thank you for the notification!\')\n\n    while True:\n        print\'Connected: \'+str(len(connection_threads))\n        print \'Scanning...\'\n        devices = scanner.scan(2)\n        for d in devices:\n            print(d.addr)\n            if d.addr in bt_addrs:\n                p = Peripheral(d)\n                connections.append(p)\n                t = ConnectionHandlerThread(len(connections)-1)\n                t.start()\n                connection_threads.append(t)\n
Run Code Online (Sandbox Code Playgroud)\n\n

3)我将只使用您可能尝试过的bluetoothctl 编写手动连接选项。由于这里没有列出,我也将其添加:

\n\n

**使用bluetoothctl手动连接:**要获取特征列表,您可以在建立连接并通过bluetoothctl进入通用属性子菜单后使用\xe2\x80\x9clist-attributes\xe2\x80\x9d命令,menu gatt应该打印与上面相同的列表:

\n\n
list-attributes 00:61:61:15:8D:60\n
Run Code Online (Sandbox Code Playgroud)\n\n

要读取属性,您首先要使用 - 您猜对了 - \xe2\x80\x9cselect-attribute\xe2\x80\x9d 命令选择它:

\n\n
select-attribute /org/bluez/hci0/dev_00_61_61_15_8D_60/service000a/char000b\n
Run Code Online (Sandbox Code Playgroud)\n\n

之后,您可以发出 \xe2\x80\x9cread\xe2\x80\x9d 命令,无需任何参数。

\n\n

要连续读取特征(如果特征支持),请使用 \xe2\x80\x9cnotify\xe2\x80\x9d 命令:

\n\n
notify on\n
Run Code Online (Sandbox Code Playgroud)\n\n

PS:这是我在 stackoverflow 上的第一个答案:) \n我也是 BLE 新手,所以请耐心等待。我对您的项目感兴趣,感谢任何链接/联系方式:) \n您可以在 alexandrudancu.com 上找到我

\n