要求:我需要连接到一个远程蓝牙设备和端口,并使用设备文件发送数据。1.首先扫描最近的蓝牙设备2.连接到远程BT地址通道并使用设备文件(/dev/rfcomm0)进行通信(&C)
我被困在了第二步。我可以通过linux shell来做到这一点
sudo rfcomm connect /dev/rfcomm0 00:11:22:33:44:55 1 &这是可行的,然后我打开我的python解释器并使用rfcomm0设备文件与远程设备通信。
但我的要求是设备地址可以更改。所以我想通过python程序来连接和释放连接。
我尝试使用python子进程。但问题是它立即返回返回码0,然后在一定的延迟后建立连接。
import subprocess
host = '00:11:22:33:44:55'
port = "1"
subprocess.call(["rfcomm connect",host,port,"&"],shell=True)我正在寻找是否有任何pyBluez或任何其他python替代方案来实现这一点。
发布于 2020-01-10 23:09:26
import subprocess
host = input()
port = 1
cmd = "sudo rfcomm connect /dev/rfcomm0 {} {} &".format(host, port)
conn = subprocess.Popen(cmd, shell=True)
if conn.returncode is None:
print("error in opening connection")导入子流程模块
从用户(主机)读取蓝牙地址
端口号也可以作为输入读取,我正在考虑默认端口1
cmd = "sudo rfcomm connect /dev/rfcomm0 {} {} &“.format(主机,端口)将根据给定的参数创建命令
有许多方法可以读取命令执行后的输出和错误。阅读更多关于Popen@https://docs.python.org/3/library/subprocess.html的信息
发布于 2016-06-07 23:56:39
您可以使用os模块来运行Shell命令。您可以像这样存储返回值:
from os import system
Returnedstring = system("Shell command")https://stackoverflow.com/questions/37683473
复制相似问题