在 python 中将蓝牙设备与密码/密码配对 - RFCOMM (Linux) [英] Pairing bluetooth devices with Passkey/Password in python - RFCOMM (Linux)
问题描述
我正在编写一个 Python 脚本来搜索蓝牙设备并使用 RFCOMM 连接它们.此设备有密码/密码.我正在使用 PyBlueZ,据我所知,这个库无法处理密码/密码连接(Python PyBluez 连接到受密钥保护的设备).
I am working on a Python script to search for bluetooth devices and connect them using RFCOMM. This devices has Passkey/Password. I am using PyBlueZ and, as far as I know, this library cannot handle Passkey/Password connections (Python PyBluez connecting to passkey protected device).
我能够发现设备并检索它们的名称和地址:
I am able to discover the devices and retrieve their names and addresses:
nearby_devices = bluetooth.discover_devices(duration=4,lookup_names=True,
flush_cache=True, lookup_class=False)
但是如果尝试使用以下方法连接到特定设备:
But if tried to connect to a specific device using:
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.connect((addr,port))
我收到一个错误'设备或资源繁忙 (16)'
.
我使用 hcitool 和 bluetooth-agent 尝试了一些 bash 命令,但我需要以编程方式进行连接.我能够使用此处描述的步骤连接到我的设备:如何在 Linux 上从命令行配对蓝牙设备.
I tried some bash commands using the hcitool and bluetooth-agent, but I need to do the connection programmatically. I was able to connect to my device using the steps described here: How to pair a bluetooth device from command line on Linux.
我想问一下是否有人使用 Python 连接到带有 Passkey/Password 的蓝牙设备.我正在考虑使用 subprocess.call()
在 Python 中使用 bash 命令,但我不确定这是否是个好主意.
I want to ask if someone has connected to a bluetooth device with Passkey/Password using Python. I am thinking about to use the bash commands in Python using subprocess.call()
, but I am not sure if it is a good idea.
感谢您的帮助.
推荐答案
我终于可以使用 PyBlueZ 连接到设备了.我希望这个答案将来能帮助其他人.我尝试了以下方法:
Finally I am able to connect to a device using PyBlueZ. I hope this answer will help others in the future. I tried the following:
首先,导入模块并发现设备.
First, import the modules and discover the devices.
import bluetooth, subprocess
nearby_devices = bluetooth.discover_devices(duration=4,lookup_names=True,
flush_cache=True, lookup_class=False)
当您发现要连接的设备时,您需要知道端口、地址和密码.使用该信息执行下一步操作:
When you discover the device you want to connect, you need to know port, the address and passkey. With that information do the next:
name = name # Device name
addr = addr # Device Address
port = 1 # RFCOMM port
passkey = "1111" # passkey of the device you want to connect
# kill any "bluetooth-agent" process that is already running
subprocess.call("kill -9 `pidof bluetooth-agent`",shell=True)
# Start a new "bluetooth-agent" process where XXXX is the passkey
status = subprocess.call("bluetooth-agent " + passkey + " &",shell=True)
# Now, connect in the same way as always with PyBlueZ
try:
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.connect((addr,port))
except bluetooth.btcommon.BluetoothError as err:
# Error handler
pass
现在,您已连接!您可以将套接字用于您需要的任务:
Now, you are connected!! You can use your socket for the task you need:
s.recv(1024) # Buffer size
s.send("Hello World!")
官方 PyBlueZ 文档可用 这里
Official PyBlueZ documentation is available here
这篇关于在 python 中将蓝牙设备与密码/密码配对 - RFCOMM (Linux)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!