auto search
This commit is contained in:
parent
2c8489a0de
commit
22c0f4b0cc
@ -1,6 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from PySide6.QtCore import Qt, QTimer, QSettings
|
||||
from PySide6.QtCore import Qt, QTimer, QSettings, QEvent
|
||||
from PySide6.QtGui import QFont
|
||||
from PySide6.QtWidgets import (
|
||||
QWidget,
|
||||
@ -23,6 +23,8 @@ class BluetoothScreen(QWidget):
|
||||
self._on_back = on_back
|
||||
self._settings = QSettings("car_ui", "ui")
|
||||
self._bt_service = BluetoothService(self)
|
||||
self._discoverable_timer = QTimer(self)
|
||||
self._discoverable_timer.timeout.connect(self._refresh_discoverable)
|
||||
|
||||
root = QVBoxLayout(self)
|
||||
root.setContentsMargins(0, 0, 0, 0)
|
||||
@ -127,7 +129,13 @@ class BluetoothScreen(QWidget):
|
||||
elif not success:
|
||||
self.status.setText(f"Статус: ошибка ({self._bt_service.last_error})")
|
||||
else:
|
||||
self.status.setText("Статус: видим для сопряжения")
|
||||
self.status.setText("Статус: видим для сопряжения (10 сек)")
|
||||
# Запускаем таймер продления видимости
|
||||
self._discoverable_timer.start(9000) # 9 секунд
|
||||
|
||||
def _refresh_discoverable(self):
|
||||
"""Продлить режим сопряжения."""
|
||||
self._bt_service.make_discoverable()
|
||||
|
||||
def _connect_selected(self):
|
||||
"""Подключить выбранное устройство."""
|
||||
@ -163,3 +171,13 @@ class BluetoothScreen(QWidget):
|
||||
"""Обновить текстовый статус."""
|
||||
mac = self._selected_mac()
|
||||
self.status.setText(self._bt_service.get_status_text(mac))
|
||||
|
||||
def showEvent(self, event):
|
||||
"""Экран показан — запускаем таймер, если видимость активна."""
|
||||
super().showEvent(event)
|
||||
# Можно запустить таймер, если нужно авто-продление при показе экрана
|
||||
|
||||
def hideEvent(self, event):
|
||||
"""Экран скрыт — останавливаем таймер."""
|
||||
super().hideEvent(event)
|
||||
self._discoverable_timer.stop()
|
||||
|
||||
@ -81,9 +81,12 @@ class BluetoothService(QObject):
|
||||
self.connected_changed.emit(mac, False)
|
||||
return success
|
||||
|
||||
def make_discoverable(self) -> bool:
|
||||
def make_discoverable(self, timeout_sec: int = 10) -> bool:
|
||||
"""Сделать устройство видимым для сопряжения.
|
||||
|
||||
Args:
|
||||
timeout_sec: Время видимости в секундах (по умолчанию 10).
|
||||
|
||||
Возвращает False, если достигнуто максимальное количество сопряженных устройств.
|
||||
"""
|
||||
# Проверка лимита сопряженных устройств
|
||||
@ -96,7 +99,7 @@ class BluetoothService(QObject):
|
||||
script_out = self._run_btctl_script(
|
||||
[
|
||||
"power on",
|
||||
"discoverable-timeout 0",
|
||||
f"discoverable-timeout {timeout_sec}",
|
||||
"discoverable on",
|
||||
"pairable on",
|
||||
"agent NoInputNoOutput",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user