From 2d4784988875eb82f308f1163778112a2dc3a508 Mon Sep 17 00:00:00 2001 From: CGDogan <126820728+CGDogan@users.noreply.github.com> Date: Mon, 19 Feb 2024 12:52:26 +0000 Subject: [PATCH] Store state of bluetooth before sleep --- Bluesnooze/AppDelegate.swift | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Bluesnooze/AppDelegate.swift b/Bluesnooze/AppDelegate.swift index 7071f8d..0a6a4d4 100644 --- a/Bluesnooze/AppDelegate.swift +++ b/Bluesnooze/AppDelegate.swift @@ -17,6 +17,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet weak var launchAtLoginMenuItem: NSMenuItem! private let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) + private var disabledByBluesnooze = false func applicationDidFinishLaunching(_ aNotification: Notification) { initStatusItem() @@ -49,16 +50,26 @@ class AppDelegate: NSObject, NSApplicationDelegate { } @objc func onPowerDown(note: NSNotification) { - setBluetooth(powerOn: false) + disabledByBluesnooze = getBluetooth() + if (disabledByBluesnooze) { + setBluetooth(powerOn: false) + } } @objc func onPowerUp(note: NSNotification) { - setBluetooth(powerOn: true) + if (disabledByBluesnooze) { + setBluetooth(powerOn: true) + disabledByBluesnooze = false + } } private func setBluetooth(powerOn: Bool) { IOBluetoothPreferenceSetControllerPowerState(powerOn ? 1 : 0) } + + private func getBluetooth() -> Bool { + return IOBluetoothPreferenceGetControllerPowerState() + } // MARK: UI state