Я выполняю последовательность команд Bluetoothctl на терминале каждый раз, прежде чем хочу запустить скрипт python на моем pi. Я хочу подключиться к BLE-устройству автоматически с пи без какого-либо подтверждения сопряжения или взаимодействия с пользователем.
Вот команды, которые я должен вводить каждый раз, когда я перезагружаю pi перед запуском другого скрипта python (после этого скрипт будет работать несколько дней, пока я не остановлю или не перезагружу pi):
$sudo bluetoothctl
[Bluetooth]power on
[Bluetooth]discoverable on
[Bluetooth]pairable on
[Bluetooth]agent NoInputNoOutput
[Bluetooth]default-agent
Я хочу автоматизировать этот процесс.
Итак, я попытался использовать оболочку bluetoothctl и изменил ее, но, похоже, не работает. Ошибок тоже нет.
import time
import pexpect
import subprocess
import sys
import re
class BluetoothctlError(Exception):
"""This exception is raised, when bluetoothctl fails to start."""
pass
class Bluetoothctl:
def __init__(self):
out = subprocess.check_output("rfkill unblock bluetooth", shell = True)
self.child = pexpect.spawn("bluetoothctl", echo = False)
print("bluetoothctl")
def get_output(self, command, pause = 0):
"""Run a command in bluetoothctl prompt, return output as a list of lines."""
self.child.send(command + "\n")
time.sleep(pause)
start_failed = self.child.expect(["bluetooth", pexpect.EOF])
if start_failed:
raise BluetoothctlError("Bluetoothctl failed after running " + command)
return self.child.before.split(b"\r\n")
def make_discoverable(self):
"""Make device discoverable."""
try:
out = self.get_output("discoverable on")
print("discoverable on")
except BluetoothctlError as e:
print(e)
return None
def power_on(self):
"""Start agent"""
try:
out = self.get_output("power on")
print("power on")
except BluetoothctlError as e:
print(e)
return None
def pairable_on(self):
"""Start agent"""
try:
out = self.get_output("pairable on")
print("pairable on")
except BluetoothctlError as e:
print(e)
return None
def agent_noinputnooutput(self):
"""Start agent"""
try:
out = self.get_output("agent NoInputNoOutput")
print("agent Registered Successfully")
except BluetoothctlError as e:
print(e)
return None
def default_agent(self):
"""Start default agent"""
try:
out = self.get_output("default-agent")
print("set as default agent")
except BluetoothctlError as e:
print(e)
return None
if __name__ == "__main__":
print("Init bluetooth...")
bl = Bluetoothctl()
bl.power_on()
bl.make_discoverable()
bl.pairable_on()
bl.agent_noinputnooutput()
bl.default_agent()