Skip to content

Commit

Permalink
Store state of bluetooth before sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
CGDogan committed Feb 19, 2024
1 parent bfa5e6d commit 2d47849
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions Bluesnooze/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 2d47849

Please sign in to comment.