树莓派与蓝牙模块和树莓派终端之间的通信

时间:2019-01-15 22:22:30

标签: python terminal bluetooth raspberry-pi

我们正在尝试使用Python将蓝牙模块连接到Raspberry Pi,而不是直接输入终端命令。运行以下代码时,HC-05模块和Raspberry pi之间没有建立连接。我们已经尝试了Popen模块中的runsubprocess函数。

代码:

from subprocess import Popen, PIPE, run
mac = "20:17:07:25:51:04"

#code to open an rfcomm to connect bluetooth and raspberry pi
run("sudo rfcomm connect 0" + mac + "1 -i hci0 &", shell=True)

#separating above and below code
print("===========================SPACER========================")

#detecting if raspberry pi is connected to bluetooth module
process = Popen(["sudo", "bluetoothctl", "-a"], stdin=PIPE, stdout=PIPE, universal_newlines=True)
process.stdin.write('info 20:17:07:25:51:04')
output, errors = process.communicate()

print(output)

输出:

===========================SPACER======================== Waiting to connect to bluetoothd...
#[0;94m[bluetooth]#[0m#
[#[0;92mNEW#[0m] Controller B8:27:EB:CC:0E:69 raspberrypi [default]
#[0;94m[bluetooth]#[0m#
[#[0;92mNEW#[0m] Device 20:17:07:25:51:04 hc01.com HC-05
#[0;94m[bluetooth]#[0m#
Agent registered
#[0;94m[bluetooth]#[0m# info 20:17:07:25:51:04 Device 20:17:07:25:51:04     Name: hc01.com HC-05    Alias: hc01.com HC-05   Class:
0x001f00    Paired: yes     Trusted: yes    Blocked: no     Connected: no
    LegacyPairing: no   UUID: Serial Port
(00001101-0000-1000-8000-00805f9b34fb)
#[0;94m[bluetooth]#[0m#


Agent unregistered
#[0;94m[bluetooth]#[0m#


[#[0;91mDEL#[0m] Controller B8:27:EB:CC:0E:69 raspberrypi [default]
#[0;94m[bluetooth]#[0m#

Waiting to connect to bluetoothd...

我们希望输出中的“ connected”行为yes,但仍显示为no。尽管直接在终端中输入这些命令时,我们会收到连接的肯定回答。

0 个答案:

没有答案