使用Linux和Bluez 5.0的多个BLE连接 [英] Multiple BLE Connections using Linux and Bluez 5.0

查看:733
本文介绍了使用Linux和Bluez 5.0的多个BLE连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用BlueZ 5.0和Linux连接到多个BLE设备.我有一个主机BLE适配器,并且我修改了gatttool以连接并执行此功能.如果运行修改后的gatttool的实例,则可以成功连接并从BLE设备接收通知数据.如果我运行修改后的gatttool的另一个实例并连接到另一个BLE设备,则此应用程序开始从两个BLE设备接收通知数据,并且初始应用程序不再接收任何数据.我认为这是由于套接字设置而导致的,这两个应用程序都将其套接字配置为相同的地址和PSM(最新的实例接收数据,而另一个实例却处于饥饿状态).有办法防止这种情况吗?理想情况下,我希望一个应用程序连接到多个设备.我假设该应用程序只能有一个套接字,原因是多个套接字将具有与上述多个实例相同的问题.我的BLE设备是充当心率监测器的TI CC2540钥匙扣.

I am currently attempting to connect to multiple BLE devices using BlueZ 5.0 and Linux. I have one host BLE adapter and I have modified the gatttool to connect and perform this function. If I run an instance of the modified gatttool, I successfully connect and receive notification data from the BLE device. If I run another instance of the modified gatttool and connect to another BLE device, this application starts receiving notification data from both BLE devices and the initial application no longer receives any data. I believe this is due to the socket setup, where both applications are configuring their sockets to the same address and PSM (the newest instance receives the data whereas the other is starved). Is there a way to prevent this condition? Ideally, I want one application to connect to multiple devices. I assume that the application can only have one socket for the reason that multiple sockets will have the same issue as the multiple instances above. My BLE device is a TI CC2540 keyfob acting as a heartrate monitor.

推荐答案

我开始回答,这样我可以有更多的空间...

I started an answer so I could have more space...

我使用Python和C的组合来使我的代码正常工作,因此我的代码"可能看起来很有趣,因为它可能来自任何一个.另外,我使用了Bluez 4,因为5不支持我所使用的内核.让我知道是否有问题,我可以澄清.

I'm using a combination of Python and C to get my code to work, so my "code" may look funny because it could be from either. Also, I used Bluez 4 as the 5 didn't support the kernel I was using. Let me know if there's an issue and I can clarify.

似乎有多种处理方法,但是我最终为不同的任务打开了单独套接字.您可以 打开一个套接字,然后将套接字选项设置为关闭过滤,并且应该将所有数据包放在一个位置.但是,这是我最初的做法,我发现我的连接会在几秒钟内消失.

It seems like there's several ways of doing things, but I ended up opening separate sockets for different tasks. You can open a single socket and then set the socket options to take filtering off and you should get all the packets in one place. However, that was my initial way of doing it and I found that my connections would die within seconds.

要扫描连接,我打开了socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI),然后在设备0上做了一个bind. (有一个名为hci_get_route的函数以获取可用的设备号),然后可以调用hci_le_set_scan_parameters来设置选项,调用setsockopt(SOL_HCI, HCI_FILTER, filter)来获取LE扫描事件,然后调用hci_le_set_scan_enable来打开扫描.

To scan for connections I opened a socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI) then did a bind on device 0. (there's a function called hci_get_route to get an available device number) You can then call hci_le_set_scan_parameters to set options, setsockopt(SOL_HCI, HCI_FILTER, filter) to just get LE scan events, and then called hci_le_set_scan_enable to turn on scanning.

每个设备的连接都是通过socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)进行的,然后通过在套接字上调用带有struct sockaddr_l2且具有特定设备地址的connect来告诉您连接到特定设备.在该套接字上,您应该只从该设备获取数据包. (一个警告...我发现在进行主动扫描时,我的加密狗不允许连接..在连接之前,我不得不暂时将其关闭,然后再将其重新打开.否则,我从errno收到了BUSY错误)

Each device connection was made with a socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP) which you then tell to connect to a particular device by calling connect on the socket with a struct sockaddr_l2 that has the particular device address in it. On that socket you should only get packets from that device. (one caveat... I found that my dongle wouldn't allow a connection while active scanning was taking place.. I had to temporarily shut it off just before connecting and then turn it back on. Otherwise I got a BUSY error from errno)

在说完所有这些之后,...我认为您应该在Bluez 5中执行所有操作的方式是使用DBUS.不幸的是,这并不是我所做的选择.我提到的功能是在共享库中的,该库显然不是默认情况下在5中安装的(您必须明确要求将其与configure一起安装).他们停止默认安装共享库,因为他们想鼓励人们改用DBUS.

After saying all that, though... I think the way you're supposed to do everything in Bluez 5 is to use DBUS. Unfortunately that wasn't really an option for what I was doing. The functions I mentioned are in the shared lib that apparently isn't installed by default in 5 (you have to explicitly ask for it to be installed with configure). They stopped installing the shared lib by default because they wanted to encourage people to use DBUS instead.

这篇关于使用Linux和Bluez 5.0的多个BLE连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆