From 06f7b6bdb8386d50ff2ea2609bb3015b6dc6d12d Mon Sep 17 00:00:00 2001 From: Tim Gromeyer Date: Sun, 23 Mar 2025 21:49:27 +0100 Subject: [PATCH] Add connection state --- linux/ble/blemanager.cpp | 5 +++++ linux/ble/blemanager.h | 13 +++++++++++++ linux/ble/blescanner.cpp | 29 +++++++++++++++++++++++++++++ linux/ble/blescanner.h | 2 ++ 4 files changed, 49 insertions(+) diff --git a/linux/ble/blemanager.cpp b/linux/ble/blemanager.cpp index 158f3b3..bf8171b 100644 --- a/linux/ble/blemanager.cpp +++ b/linux/ble/blemanager.cpp @@ -77,6 +77,8 @@ void BleManager::onDeviceDiscovered(const QBluetoothDeviceInfo &info) deviceInfo.lidOpenCounter = static_cast(data[8]); deviceInfo.deviceColor = static_cast(data[9]); + deviceInfo.connectionState = static_cast(data[10]); + // Determine primary pod (bit 5 of status) and value flipping bool primaryLeft = (status & 0x20) != 0; // Bit 5: 1 = left primary, 0 = right primary bool areValuesFlipped = !primaryLeft; // Flipped when right pod is primary @@ -119,6 +121,9 @@ void BleManager::onDeviceDiscovered(const QBluetoothDeviceInfo &info) else deviceInfo.lidState = DeviceInfo::LidState::UNKNOWN; + + + // Update timestamp deviceInfo.lastSeen = QDateTime::currentDateTime(); diff --git a/linux/ble/blemanager.h b/linux/ble/blemanager.h index 004778d..aed7323 100644 --- a/linux/ble/blemanager.h +++ b/linux/ble/blemanager.h @@ -45,6 +45,19 @@ public: }; LidState lidState = LidState::UNKNOWN; + // Connection state enumeration + enum class ConnectionState : uint8_t + { + DISCONNECTED = 0x00, + IDLE = 0x04, + MUSIC = 0x05, + CALL = 0x06, + RINGING = 0x07, + HANGING_UP = 0x09, + UNKNOWN = 0xFF // Using 0xFF for representing null in the original + }; + ConnectionState connectionState = ConnectionState::UNKNOWN; + QDateTime lastSeen; // Timestamp of last detection }; diff --git a/linux/ble/blescanner.cpp b/linux/ble/blescanner.cpp index 416e9a8..9c38b88 100644 --- a/linux/ble/blescanner.cpp +++ b/linux/ble/blescanner.cpp @@ -128,6 +128,11 @@ BleScanner::BleScanner(QWidget *parent) : QMainWindow(parent) bothPodsInCaseLabel = new QLabel(this); detailsLayout->addWidget(bothPodsInCaseLabel, 14, 1); + // Row 15: Connection State + detailsLayout->addWidget(new QLabel("Connection State:"), 15, 0); + connectionStateLabel = new QLabel(this); + detailsLayout->addWidget(connectionStateLabel, 15, 1); + mainLayout->addWidget(detailsGroup); detailsGroup->setVisible(false); @@ -302,6 +307,7 @@ void BleScanner::onDeviceSelected() thisPodInCaseLabel->setText(device.isThisPodInTheCase ? "Yes" : "No"); onePodInCaseLabel->setText(device.isOnePodInCase ? "Yes" : "No"); bothPodsInCaseLabel->setText(device.areBothPodsInCase ? "Yes" : "No"); + connectionStateLabel->setText(getConnectionStateName(device.connectionState)); detailsGroup->setVisible(true); } @@ -368,4 +374,27 @@ QString BleScanner::getColorName(quint8 colorId) default: return "Unknown"; } +} + +QString BleScanner::getConnectionStateName(DeviceInfo::ConnectionState state) +{ + using ConnectionState = DeviceInfo::ConnectionState; + switch (state) + { + case ConnectionState::DISCONNECTED: + return QString("Disconnected"); + case ConnectionState::IDLE: + return QString("Idle"); + case ConnectionState::MUSIC: + return QString("Playing Music"); + case ConnectionState::CALL: + return QString("On Call"); + case ConnectionState::RINGING: + return QString("Ringing"); + case ConnectionState::HANGING_UP: + return QString("Hanging Up"); + case ConnectionState::UNKNOWN: + default: + return QString("Unknown"); + } } \ No newline at end of file diff --git a/linux/ble/blescanner.h b/linux/ble/blescanner.h index ae10ccd..a3958f6 100644 --- a/linux/ble/blescanner.h +++ b/linux/ble/blescanner.h @@ -27,6 +27,7 @@ private slots: private: QString getModelName(quint16 modelId); QString getColorName(quint8 colorId); + QString getConnectionStateName(DeviceInfo::ConnectionState state); BleManager *bleManager; QTimer *refreshTimer; @@ -54,6 +55,7 @@ private: QLabel *thisPodInCaseLabel; QLabel *onePodInCaseLabel; QLabel *bothPodsInCaseLabel; + QLabel *connectionStateLabel; }; #endif // BLESCANNER_H \ No newline at end of file