如何在 Linux 上始终连接到同一个 rfcomm 端口?
我尝试在 Linux 中连接多个蓝牙设备。除了一台设备之外,我对任何设备都没有任何问题。不同之处在于,所有其他设备都以(我假设的)标准方式处理其蓝牙连接,即它们等待主机设备(PC)发起连接。另一方面,另一台设备每隔几秒钟尝试启动一次连接,中间有大约一秒钟的睡眠时间。
对于其他设备,我一直这样连接:(
rfcomm connect /dev/rfcommX <deviceMacAddress>
其中 X 是任何未使用的 rfcomm 端口号)
在发出此命令之前,我使用所需的配对密钥运行蓝牙代理。这里一切都运转良好。
对于有问题的设备,在设备配对之前,这第一次效果很好。然而,配对后,rfcomm connect 命令失败的可能性非常高。这是因为设备本身正在尝试初始化连接。当设备处于睡眠状态时,连接失败(“主机关闭”)。
相反,我发现,对于该设备,以下命令就像一个魅力:(
rfcomm listen /dev/rfcommX
我必须首先通过 sdptool add SP 添加串行端口)
由于 PC 将坐在那里等待来自设备的传入连接,这每次都有效。
然而,当我有不止一台设备时,问题就出现了。 rfcomm 监听命令工作得很好,但似乎没有办法控制哪个设备(由 Mac 地址识别)连接到哪个 rfcomm 端口;如果打开了多个设备,则无论如何,第一个尝试连接的设备都会连接。然而,在我们的应用程序中,我们希望用户知道他们正在连接到哪个设备。
以前有人处理过类似的事情吗?我们正在考虑编写 bluez 蓝牙包的自定义版本,因此我们将非常感谢这里的任何帮助:)
I have several bluetooth device that I'm trying to connect to in Linux. I have no problems with any of the devices except for one. The difference is that all the other devices handle their bluetooth connections in (what I assume to be) the standard way, i.e. they wait for the Host device (the PC) to initiate the connection. The other device, on the other hand, tries to initiate the connection itself every few seconds, with a second or so of sleep in between.
For the other devices, I've been connecting like this:
rfcomm connect /dev/rfcommX <deviceMacAddress>
(where X is any unused rfcomm port number)
Before issuing this command, I run the bluetooth-agent with the needed pairing keys. Everything here works fine.
For the device in question, this works great the first time, before the device has been paired. After pairing, however, the rfcomm connect command has a very high likelihood of failing. This is because the device is itself trying to init the connection.. when the device is sleeping, the connection fails ("Host down").
Instead, I've found that, for this device, the following command works like a charm:
rfcomm listen /dev/rfcommX
(I had to add a serial port via sdptool add SP first)
As the PC will sit there and wait for an incoming connection from the device, this works every single time.
However, the problem comes in when I have more than one device. The rfcomm listen command works brilliantly, but there doesn't seem to be a way to control which device (identified by Mac address) connects to which rfcomm port; if more than one device is switched on, then the first one to try and connect will connect, regardless. In our application, however, we want the user to know which device they are connecting to.
Has anyone dealt with anything like this before? We're getting to the point where we're thinking of writing a custom version of the bluez bluetooth package, so any help here would be greatly appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编写一个 udev 规则,每次都会创建一个同名的符号链接,或者遵循适当的路径 < code>/sys 来访问设备。
Either write a udev rule that creates a symlink with the same name each time, or follow the appropriate path through
/sys
to get to the device.虽然 11 年后 OP 不太可能关心,但这就是我为了获得一致的设备名称所做的事情。
我有一个 bash 脚本,它将每个设备绑定到预定义的端口。我一直在想我会用一种更聪明的方式来做到这一点,也许是用Python,但这很有效,所以我继续使用它:
While it is unlikely that the OP cares after 11 years, this is what I do to get consistent device names.
I have a bash script that binds each device to a pre-defined port. I keep thinking I will do this in a more clever way, perhaps in python, but this works so I just keep using it: