add priority mac

This commit is contained in:
cheykrym 2026-04-01 04:00:52 +03:00
parent 9ae9e7873a
commit 126e27e29f

View File

@ -47,6 +47,7 @@ class BluetoothService(QObject):
super().__init__(parent)
self._log_path = Path("~/.cache/car_ui/bluetooth.log").expanduser()
self._last_error = ""
self._settings = QSettings("car_ui", "ui")
# === Public API ===
@ -95,30 +96,48 @@ class BluetoothService(QObject):
return info.get("Connected", "no") == "yes"
def connect_device(self, mac: str) -> bool:
"""Подключиться к устройству."""
"""Подключиться к устройству.
При успешном подключении обновляет bluetooth/music_mac.
"""
self._last_error = ""
self._run_cmd(["bluetoothctl", "trust", mac])
result = self._run_cmd(["bluetoothctl", "connect", mac])
success = not self._last_error
if success:
# Обновляем приоритетный MAC
self._settings.setValue("bluetooth/music_mac", mac)
self.connected_changed.emit(mac, success)
return success
def disconnect_device(self, mac: str) -> bool:
"""Отключить устройство."""
"""Отключить устройство.
Если это устройство было в bluetooth/music_mac, обнуляем настройку.
"""
self._last_error = ""
result = self._run_cmd(["bluetoothctl", "disconnect", mac])
success = not self._last_error
# Если отключаем приоритетное устройство, обнуляем настройку
if success and self._settings.value("bluetooth/music_mac") == mac:
self._settings.setValue("bluetooth/music_mac", "")
self.connected_changed.emit(mac, False)
return success
def remove_device(self, mac: str) -> bool:
"""Удалить устройство из списка сопряженных."""
"""Удалить устройство из списка сопряженных.
Если это устройство было в bluetooth/music_mac, обнуляем настройку.
"""
self._last_error = ""
# Сначала отключаем, если подключено
if self.is_connected(mac):
self._run_cmd(["bluetoothctl", "disconnect", mac])
result = self._run_cmd(["bluetoothctl", "remove", mac])
success = not self._last_error
# Если удаляем приоритетное устройство, обнуляем настройку
if success and self._settings.value("bluetooth/music_mac") == mac:
self._settings.setValue("bluetooth/music_mac", "")
return success
def make_discoverable(self, timeout_sec: int = 10) -> bool: