2424#include " ble/blemanager.h"
2525#include " ble/bleutils.h"
2626#include " QRCodeImageProvider.hpp"
27+ #include " systemsleepmonitor.hpp"
2728
2829using 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+
336354private 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
856875int main (int argc, char *argv[]) {
0 commit comments