add bp connect

This commit is contained in:
Your Name 2026-01-08 02:54:12 +03:00
parent 4dc76de17c
commit b90adfd155
2 changed files with 38 additions and 7 deletions

5
qqqq
View File

@ -3,3 +3,8 @@ export DISPLAY=:0
./.venv/bin/python3 ./main.py
sudo usermod -aG bluetooth cheykrym
rfkill list
sudo rfkill unblock bluetooth
cat ~/.cache/car_ui/bluetooth.log

View File

@ -140,15 +140,19 @@ class BluetoothScreen(QWidget):
def _make_visible(self):
self._last_error = ""
self._run_cmd(["bluetoothctl", "power", "on"])
if self._last_error:
script_out = self._run_btctl_script(
[
"power on",
"discoverable-timeout 0",
"discoverable on",
"pairable on",
"agent NoInputNoOutput",
"default-agent",
]
)
if "Failed to set power on" in script_out or "org.bluez.Error" in script_out:
self.status.setText("Статус: питание BT выключено (проверьте rfkill)")
return
self._run_cmd(["bluetoothctl", "discoverable-timeout", "0"])
self._run_cmd(["bluetoothctl", "discoverable", "on"])
self._run_cmd(["bluetoothctl", "pairable", "on"])
self._run_cmd(["bluetoothctl", "agent", "NoInputNoOutput"])
self._run_cmd(["bluetoothctl", "default-agent"])
if self._last_error:
self.status.setText(f"Статус: ошибка ({self._last_error})")
else:
@ -240,6 +244,28 @@ class BluetoothScreen(QWidget):
self._log(args, stdout, stderr, result.returncode)
return stdout
def _run_btctl_script(self, commands: list[str]) -> str:
script = "\n".join(commands + ["quit"]) + "\n"
try:
result = subprocess.run(
["bluetoothctl"],
input=script,
capture_output=True,
text=True,
timeout=4,
check=False,
)
except (subprocess.SubprocessError, OSError):
self._last_error = "run-failed"
self._log(["bluetoothctl", "(script)"], "", "run-failed", 1)
return ""
stdout = result.stdout.strip()
stderr = result.stderr.strip()
if result.returncode != 0 or stderr:
self._last_error = stderr or f"exit={result.returncode}"
self._log(["bluetoothctl", "(script)"], stdout, stderr, result.returncode)
return stdout
def _log(self, args: list[str], stdout: str, stderr: str, code: int):
try:
os.makedirs(os.path.dirname(self._log_path), exist_ok=True)