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