new refresh bl
This commit is contained in:
parent
4bed805253
commit
7f577613d3
@ -25,6 +25,8 @@ class BluetoothScreen(QWidget):
|
|||||||
self._bt_service = BluetoothService(self)
|
self._bt_service = BluetoothService(self)
|
||||||
self._discoverable_timer = QTimer(self)
|
self._discoverable_timer = QTimer(self)
|
||||||
self._discoverable_timer.timeout.connect(self._refresh_discoverable)
|
self._discoverable_timer.timeout.connect(self._refresh_discoverable)
|
||||||
|
self._list_refresh_timer = QTimer(self)
|
||||||
|
self._list_refresh_timer.timeout.connect(self.refresh_list)
|
||||||
|
|
||||||
root = QVBoxLayout(self)
|
root = QVBoxLayout(self)
|
||||||
root.setContentsMargins(0, 0, 0, 0)
|
root.setContentsMargins(0, 0, 0, 0)
|
||||||
@ -83,9 +85,6 @@ class BluetoothScreen(QWidget):
|
|||||||
root.addWidget(self.list, 1)
|
root.addWidget(self.list, 1)
|
||||||
root.addLayout(actions)
|
root.addLayout(actions)
|
||||||
|
|
||||||
# Подписка на событие успешного сопряжения
|
|
||||||
self._bt_service.pairing_completed.connect(lambda: self.refresh_list())
|
|
||||||
|
|
||||||
self.refresh_list()
|
self.refresh_list()
|
||||||
QTimer.singleShot(200, self._auto_connect_last)
|
QTimer.singleShot(200, self._auto_connect_last)
|
||||||
|
|
||||||
@ -179,13 +178,14 @@ class BluetoothScreen(QWidget):
|
|||||||
self.status.setText(self._bt_service.get_status_text(mac))
|
self.status.setText(self._bt_service.get_status_text(mac))
|
||||||
|
|
||||||
def showEvent(self, event):
|
def showEvent(self, event):
|
||||||
"""Экран показан — запускаем режим сопряжения и мониторинг."""
|
"""Экран показан — запускаем режим сопряжения и обновление списка."""
|
||||||
super().showEvent(event)
|
super().showEvent(event)
|
||||||
self._refresh_discoverable()
|
self._refresh_discoverable()
|
||||||
self._bt_service.start_pairing_monitor()
|
# Обновляем список каждые 2 секунды для отслеживания новых устройств
|
||||||
|
self._list_refresh_timer.start(2000)
|
||||||
|
|
||||||
def hideEvent(self, event):
|
def hideEvent(self, event):
|
||||||
"""Экран скрыт — останавливаем таймер и мониторинг."""
|
"""Экран скрыт — останавливаем таймеры."""
|
||||||
super().hideEvent(event)
|
super().hideEvent(event)
|
||||||
self._discoverable_timer.stop()
|
self._discoverable_timer.stop()
|
||||||
self._bt_service.stop_pairing_monitor()
|
self._list_refresh_timer.stop()
|
||||||
|
|||||||
@ -24,13 +24,11 @@ class BluetoothService(QObject):
|
|||||||
status_changed = Signal(str)
|
status_changed = Signal(str)
|
||||||
devices_changed = Signal(list)
|
devices_changed = Signal(list)
|
||||||
connected_changed = Signal(str, bool) # mac, connected
|
connected_changed = Signal(str, bool) # mac, connected
|
||||||
pairing_completed = Signal() # новое устройство сопряжено
|
|
||||||
|
|
||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self._log_path = Path("~/.cache/car_ui/bluetooth.log").expanduser()
|
self._log_path = Path("~/.cache/car_ui/bluetooth.log").expanduser()
|
||||||
self._last_error = ""
|
self._last_error = ""
|
||||||
self._monitor_process: subprocess.Popen | None = None
|
|
||||||
|
|
||||||
# === Public API ===
|
# === Public API ===
|
||||||
|
|
||||||
@ -184,50 +182,6 @@ class BluetoothService(QObject):
|
|||||||
"""Предыдущий трек."""
|
"""Предыдущий трек."""
|
||||||
self._run_btctl(["menu player", "previous"])
|
self._run_btctl(["menu player", "previous"])
|
||||||
|
|
||||||
def start_pairing_monitor(self) -> None:
|
|
||||||
"""Запустить мониторинг событий сопряжения."""
|
|
||||||
if self._monitor_process is not None:
|
|
||||||
return
|
|
||||||
try:
|
|
||||||
self._monitor_process = subprocess.Popen(
|
|
||||||
["bluetoothctl", "events"],
|
|
||||||
stdout=subprocess.PIPE,
|
|
||||||
stderr=subprocess.PIPE,
|
|
||||||
text=True,
|
|
||||||
bufsize=1,
|
|
||||||
)
|
|
||||||
# Запускаем поток для чтения событий
|
|
||||||
import threading
|
|
||||||
threading.Thread(target=self._read_events, daemon=True).start()
|
|
||||||
except (subprocess.SubprocessError, OSError):
|
|
||||||
self._monitor_process = None
|
|
||||||
|
|
||||||
def stop_pairing_monitor(self) -> None:
|
|
||||||
"""Остановить мониторинг событий."""
|
|
||||||
if self._monitor_process is not None:
|
|
||||||
self._monitor_process.terminate()
|
|
||||||
self._monitor_process = None
|
|
||||||
|
|
||||||
def _read_events(self) -> None:
|
|
||||||
"""Чтение событий из bluetoothctl events."""
|
|
||||||
if self._monitor_process is None:
|
|
||||||
return
|
|
||||||
try:
|
|
||||||
for line in iter(self._monitor_process.stdout.readline, ''):
|
|
||||||
if line:
|
|
||||||
self._process_event(line.strip())
|
|
||||||
except (ValueError, OSError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def _process_event(self, line: str) -> None:
|
|
||||||
"""Обработка события bluetoothctl."""
|
|
||||||
# Событие успешного сопряжения
|
|
||||||
if "Pairing successful" in line or "Pairing complete" in line:
|
|
||||||
self.pairing_completed.emit()
|
|
||||||
# Событие нового устройства
|
|
||||||
elif "Device" in line and "connected" in line.lower():
|
|
||||||
pass # можно обработать подключение
|
|
||||||
|
|
||||||
# === Private methods ===
|
# === Private methods ===
|
||||||
|
|
||||||
def _run_cmd(self, args: list[str]) -> str:
|
def _run_cmd(self, args: list[str]) -> str:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user