diff --git a/linux/ble/blemanager.cpp b/linux/ble/blemanager.cpp index 9331f12..158f3b3 100644 --- a/linux/ble/blemanager.cpp +++ b/linux/ble/blemanager.cpp @@ -1,5 +1,6 @@ #include "blemanager.h" #include +#include BleManager::BleManager(QObject *parent) : QObject(parent) { @@ -12,11 +13,17 @@ BleManager::BleManager(QObject *parent) : QObject(parent) this, &BleManager::onScanFinished); connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::errorOccurred, this, &BleManager::onErrorOccurred); + + // Set up pruning timer + pruneTimer = new QTimer(this); + connect(pruneTimer, &QTimer::timeout, this, &BleManager::pruneOldDevices); + pruneTimer->start(PRUNE_INTERVAL_MS); // Start timer (runs every 5 seconds) } BleManager::~BleManager() { delete discoveryAgent; + delete pruneTimer; } void BleManager::startScan() @@ -24,6 +31,7 @@ void BleManager::startScan() qDebug() << "Starting BLE scan..."; devices.clear(); discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod); + pruneTimer->start(PRUNE_INTERVAL_MS); // Ensure timer is running } void BleManager::stopScan() @@ -111,6 +119,9 @@ void BleManager::onDeviceDiscovered(const QBluetoothDeviceInfo &info) else deviceInfo.lidState = DeviceInfo::LidState::UNKNOWN; + // Update timestamp + deviceInfo.lastSeen = QDateTime::currentDateTime(); + // Store device info in the map devices[address] = deviceInfo; @@ -136,4 +147,22 @@ void BleManager::onErrorOccurred(QBluetoothDeviceDiscoveryAgent::Error error) { qDebug() << "Error occurred:" << error; stopScan(); +} + +void BleManager::pruneOldDevices() +{ + QDateTime now = QDateTime::currentDateTime(); + auto it = devices.begin(); + while (it != devices.end()) + { + if (it.value().lastSeen.msecsTo(now) > DEVICE_TIMEOUT_MS) + { + qDebug() << "Removing old device:" << it.value().name << "at" << it.key(); + it = devices.erase(it); // Remove device if not seen recently + } + else + { + ++it; + } + } } \ No newline at end of file diff --git a/linux/ble/blemanager.h b/linux/ble/blemanager.h index 0562592..004778d 100644 --- a/linux/ble/blemanager.h +++ b/linux/ble/blemanager.h @@ -5,6 +5,9 @@ #include #include #include +#include + +class QTimer; class DeviceInfo { @@ -41,6 +44,8 @@ public: UNKNOWN }; LidState lidState = LidState::UNKNOWN; + + QDateTime lastSeen; // Timestamp of last detection }; class BleManager : public QObject @@ -58,10 +63,15 @@ private slots: void onDeviceDiscovered(const QBluetoothDeviceInfo &info); void onScanFinished(); void onErrorOccurred(QBluetoothDeviceDiscoveryAgent::Error error); + void pruneOldDevices(); private: QBluetoothDeviceDiscoveryAgent *discoveryAgent; QMap devices; + + QTimer *pruneTimer; // Timer for periodic pruning + static const int PRUNE_INTERVAL_MS = 5000; // Check every 5 seconds + static const int DEVICE_TIMEOUT_MS = 10000; // Remove after 10 seconds }; #endif // BLEMANAGER_H \ No newline at end of file diff --git a/linux/ble/blescanner.cpp b/linux/ble/blescanner.cpp index 5928ae3..416e9a8 100644 --- a/linux/ble/blescanner.cpp +++ b/linux/ble/blescanner.cpp @@ -131,15 +131,6 @@ BleScanner::BleScanner(QWidget *parent) : QMainWindow(parent) mainLayout->addWidget(detailsGroup); detailsGroup->setVisible(false); - trayIcon = new QSystemTrayIcon(QIcon::fromTheme("bluetooth"), this); - QMenu *trayMenu = new QMenu(this); - QAction *showAction = trayMenu->addAction("Show"); - trayMenu->addSeparator(); - QAction *exitAction = trayMenu->addAction("Exit"); - trayIcon->setContextMenu(trayMenu); - trayIcon->setToolTip("AirPods Battery Monitor"); - trayIcon->show(); - bleManager = new BleManager(this); refreshTimer = new QTimer(this); @@ -147,8 +138,6 @@ BleScanner::BleScanner(QWidget *parent) : QMainWindow(parent) connect(stopButton, &QPushButton::clicked, this, &BleScanner::stopScan); connect(deviceTable, &QTableWidget::itemSelectionChanged, this, &BleScanner::onDeviceSelected); connect(refreshTimer, &QTimer::timeout, this, &BleScanner::updateDeviceList); - connect(showAction, &QAction::triggered, this, &BleScanner::show); - connect(exitAction, &QAction::triggered, qApp, &QApplication::quit); } void BleScanner::startScan() @@ -158,7 +147,7 @@ void BleScanner::startScan() deviceTable->setRowCount(0); detailsGroup->setVisible(false); bleManager->startScan(); - refreshTimer->start(2000); + refreshTimer->start(500); } void BleScanner::stopScan() @@ -202,18 +191,8 @@ void BleScanner::updateDeviceList() } } - if (!devices.isEmpty()) - { - auto it = devices.begin(); - QString leftBat = (it.value().leftPodBattery >= 0 ? QString::number(it.value().leftPodBattery) + "%" : "N/A"); - QString rightBat = (it.value().rightPodBattery >= 0 ? QString::number(it.value().rightPodBattery) + "%" : "N/A"); - QString caseBat = (it.value().caseBattery >= 0 ? QString::number(it.value().caseBattery) + "%" : "N/A"); - QString tooltip = QString("%1\nLeft: %2 | Right: %3 | Case: %4") - .arg(it.value().name) - .arg(leftBat) - .arg(rightBat) - .arg(caseBat); - trayIcon->setToolTip(tooltip); + if (deviceTable->selectedItems().isEmpty()) { + deviceTable->selectRow(0); } } diff --git a/linux/ble/blescanner.h b/linux/ble/blescanner.h index 1c0280e..ae10ccd 100644 --- a/linux/ble/blescanner.h +++ b/linux/ble/blescanner.h @@ -45,7 +45,6 @@ private: QLabel *lidStateLabel; // Renamed from lidOpenLabel QLabel *colorLabel; QLabel *rawDataLabel; - QSystemTrayIcon *trayIcon; // New labels for additional DeviceInfo fields QLabel *leftInEarLabel;