Skip to content

Commit fb3c8c7

Browse files
committed
[Linux] Start/Stop BLE scan when going to sleep
1 parent 05c0a7c commit fb3c8c7

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

linux/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ qt_add_executable(librepods
3434
eardetection.hpp
3535
media/playerstatuswatcher.cpp
3636
media/playerstatuswatcher.h
37+
systemsleepmonitor.hpp
3738
)
3839

3940
qt_add_qml_module(librepods

linux/ble/blemanager.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ void BleManager::stopScan()
112112
discoveryAgent->stop();
113113
}
114114

115+
bool BleManager::isScanning() const
116+
{
117+
return discoveryAgent->isActive();
118+
}
119+
115120
void BleManager::onDeviceDiscovered(const QBluetoothDeviceInfo &info)
116121
{
117122
// Check for Apple's manufacturer ID (0x004C)

linux/ble/blemanager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class BleManager : public QObject
7272

7373
void startScan();
7474
void stopScan();
75+
bool isScanning() const;
7576

7677
private slots:
7778
void onDeviceDiscovered(const QBluetoothDeviceInfo &info);

linux/main.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "ble/blemanager.h"
2525
#include "ble/bleutils.h"
2626
#include "QRCodeImageProvider.hpp"
27+
#include "systemsleepmonitor.hpp"
2728

2829
using namespace AirpodsTrayApp::Enums;
2930

@@ -45,6 +46,7 @@ class AirPodsTrayApp : public QObject {
4546
: QObject(parent), debugMode(debugMode), m_settings(new QSettings("AirPodsTrayApp", "AirPodsTrayApp"))
4647
, m_autoStartManager(new AutoStartManager(this)), m_hideOnStart(hideOnStart), parent(parent)
4748
, m_deviceInfo(new DeviceInfo(this)), m_bleManager(new BleManager(this))
49+
, m_systemSleepMonitor(new SystemSleepMonitor(this))
4850
{
4951
QLoggingCategory::setFilterRules(QString("airpodsApp.debug=%1").arg(debugMode ? "true" : "false"));
5052
LOG_INFO("Initializing AirPodsTrayApp");
@@ -74,6 +76,8 @@ class AirPodsTrayApp : public QObject {
7476

7577
connect(m_bleManager, &BleManager::deviceFound, this, &AirPodsTrayApp::bleDeviceFound);
7678
connect(m_deviceInfo->getBattery(), &Battery::primaryChanged, this, &AirPodsTrayApp::primaryChanged);
79+
connect(m_systemSleepMonitor, &SystemSleepMonitor::systemGoingToSleep, this, &AirPodsTrayApp::onSystemGoingToSleep);
80+
connect(m_systemSleepMonitor, &SystemSleepMonitor::systemWakingUp, this, &AirPodsTrayApp::onSystemWakingUp);
7781

7882
// Load settings
7983
CrossDevice.isEnabled = loadCrossDeviceEnabled();
@@ -333,6 +337,20 @@ public slots:
333337
int loadRetryAttempts() const { return m_settings->value("bluetooth/retryAttempts", 3).toInt(); }
334338
void saveRetryAttempts(int attempts) { m_settings->setValue("bluetooth/retryAttempts", attempts); }
335339

340+
void onSystemGoingToSleep()
341+
{
342+
if (m_bleManager->isScanning())
343+
{
344+
LOG_INFO("Stopping BLE scan before going to sleep");
345+
m_bleManager->stopScan();
346+
}
347+
}
348+
void onSystemWakingUp()
349+
{
350+
LOG_INFO("System is waking up, starting ble scan");
351+
m_bleManager->startScan();
352+
}
353+
336354
private slots:
337355
void onTrayIconActivated()
338356
{
@@ -851,6 +869,7 @@ private slots:
851869
bool m_hideOnStart = false;
852870
DeviceInfo *m_deviceInfo;
853871
BleManager *m_bleManager;
872+
SystemSleepMonitor *m_systemSleepMonitor = nullptr;
854873
};
855874

856875
int main(int argc, char *argv[]) {

linux/systemsleepmonitor.hpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef SYSTEMSLEEPMONITOR_HPP
2+
#define SYSTEMSLEEPMONITOR_HPP
3+
4+
#include <QObject>
5+
#include <QDBusConnection>
6+
#include <QDBusInterface>
7+
#include <QDBusMessage>
8+
#include <QDebug>
9+
10+
class SystemSleepMonitor : public QObject {
11+
Q_OBJECT
12+
13+
public:
14+
explicit SystemSleepMonitor(QObject *parent = nullptr) : QObject(parent) {
15+
// Connect to the system D-Bus
16+
QDBusConnection systemBus = QDBusConnection::systemBus();
17+
if (!systemBus.isConnected()) {
18+
qWarning() << "Cannot connect to system D-Bus";
19+
return;
20+
}
21+
22+
// Subscribe to PrepareForSleep signal from logind
23+
systemBus.connect(
24+
"org.freedesktop.login1",
25+
"/org/freedesktop/login1",
26+
"org.freedesktop.login1.Manager",
27+
"PrepareForSleep",
28+
this,
29+
SLOT(handlePrepareForSleep(bool))
30+
);
31+
}
32+
33+
~SystemSleepMonitor() override = default;
34+
35+
signals:
36+
void systemGoingToSleep();
37+
void systemWakingUp();
38+
39+
private slots:
40+
void handlePrepareForSleep(bool sleeping) {
41+
if (sleeping) {
42+
emit systemGoingToSleep();
43+
} else {
44+
emit systemWakingUp();
45+
}
46+
}
47+
};
48+
49+
#endif // SYSTEMSLEEPMONITOR_HPP

0 commit comments

Comments
 (0)