From b90adfd155ab968788a0b5dc769e6749df458479 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 8 Jan 2026 02:54:12 +0300 Subject: [PATCH] add bp connect --- qqqq | 5 +++++ screens/bluetooth.py | 40 +++++++++++++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/qqqq b/qqqq index a5b5e8b..8e9c6a6 100644 --- a/qqqq +++ b/qqqq @@ -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 \ No newline at end of file diff --git a/screens/bluetooth.py b/screens/bluetooth.py index ccfed0e..7be3b73 100644 --- a/screens/bluetooth.py +++ b/screens/bluetooth.py @@ -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)