[英]Pairing bluetooth devices with Passkey/Password in python - RFCOMM (Linux)
[英]Python alternative to linux bluetooth command “rfcomm connect”
要求:我需要連接到遠程藍牙設備和端口並使用設備文件發送數據。 1. 首先掃描最近的藍牙設備 2. 連接到遠程 BT 地址和頻道並使用設備文件 (/dev/rfcomm0) 進行通信
我被困在第二步。 我可以通過 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 替代方案來實現這一目標。
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(host, port) 將從給定的參數創建一個命令
有很多方法可以讀取命令執行后的輸出和錯誤。 閱讀更多關於 Popen@ https://docs.python.org/3/library/subprocess.html
您可以使用 os 模塊來運行 Shell 命令。 您可以像這樣存儲返回值:
from os import system
Returnedstring = system("Shell command")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.