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

时间:2018-08-10 09:03:26

标签: bluetooth bluetooth-lowenergy bluetooth-gatt

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

连接到其他设备后,例如心率监测器和电话或两个HR监测器是否可以同时从每个设备的一项服务接收数据?

编辑:

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

1 个答案:

答案 0 :(得分:1)

1)首先,有一个github python项目可以在Linux的Raspberry Pi上完成该任务:https://github.com/IanHarvey/bluepy

2)其次,此snipet from Anthony Chiu 使用该API通过通知与多个设备进行通信:

  from bluepy.btle import Scanner, DefaultDelegate, Peripheral
    import threading

    class NotificationDelegate(DefaultDelegate):

    def __init__(self, number):
        DefaultDelegate.__init__(self)
        self.number = number

    def handleNotification(self, cHandle, data):
        print 'Notification:\nConnection:'+str(self.number)+ \nHandler:'+str(cHandle)+'\nMsg:'+data

    bt_addrs = ['00:15:83:00:45:98', '00:15:83:00:86:72']
    connections = []
    connection_threads = []
    scanner = Scanner(0)

    class ConnectionHandlerThread (threading.Thread):
        def __init__(self, connection_index):
            threading.Thread.__init__(self)
            self.connection_index = connection_index

        def run(self):
            connection = connections[self.connection_index]
            connection.setDelegate(NotificationDelegate(self.connection_index))
            while True:
                if connection.waitForNotifications(1):
                    connection.writeCharacteristic(37, 'Thank you for the notification!')

    while True:
        print'Connected: '+str(len(connection_threads))
        print 'Scanning...'
        devices = scanner.scan(2)
        for d in devices:
            print(d.addr)
            if d.addr in bt_addrs:
                p = Peripheral(d)
                connections.append(p)
                t = ConnectionHandlerThread(len(connections)-1)
                t.start()
                connection_threads.append(t)

3)我将只用您可能尝试过的bluetoothctl编写手动连接选项。由于未在此处列出,因此我也会添加它:

**与bluetoothctl的手动连接:**要获取特性列表,可以使用“ list-attributes”命令,该命令应打印与上述相同的列表:

list-attributes 00:61:61:15:8D:60

要读取属性,请先使用-您猜对了它的“ select-attribute”命令将其选中:

select-attribute /org/bluez/hci0/dev_00_61_61_15_8D_60/service000a/char000b

之后,您可以发出“ read”命令,而无需任何参数。

要连续读取特征(如果特征支持),请使用“通知”命令:

notify on

PS:这是我对stackoverflow的第一个答案:) 我也是BLE的新手,所以请多多包涵。对您的项目感兴趣,任何链接/联系方式都非常感激:) 您可以在alexandrudancu.com上找到我

相关问题