Skip to content

Instantly share code, notes, and snippets.

@ysimonson
Last active March 26, 2024 03:47
Show Gist options
  • Star 32 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ysimonson/fea48ee8a68ed2cbac12473e87134f58 to your computer and use it in GitHub Desktop.
Save ysimonson/fea48ee8a68ed2cbac12473e87134f58 to your computer and use it in GitHub Desktop.
Hammerspoon script to disable bluetooth when the computer is put to sleep. Requires `blueutil` to be installed (`brew install blueutil`).
require "string"
function checkBluetoothResult(rc, stderr, stderr)
if rc ~= 0 then
print(string.format("Unexpected result executing `blueutil`: rc=%d stderr=%s stdout=%s", rc, stderr, stdout))
end
end
function bluetooth(power)
print("Setting bluetooth to " .. power)
local t = hs.task.new("/usr/local/bin/blueutil", checkBluetoothResult, {"--power", power})
t:start()
end
function f(event)
if event == hs.caffeinate.watcher.systemWillSleep then
bluetooth("off")
elseif event == hs.caffeinate.watcher.screensDidWake then
bluetooth("on")
end
end
watcher = hs.caffeinate.watcher.new(f)
watcher:start()
@surajsharma
Copy link

could i extend this to discover and connect to a device when the macbook reopens?

@ysimonson
Copy link
Author

Yup, see blueutil which this uses: https://github.com/toy/blueutil

@Lugoues
Copy link

Lugoues commented Jun 10, 2022

Great stuff, thanks for the inspiration!

@kg-currenxie
Copy link

Brew installs it in /opt/homebrew/bin/blueutil for me :)

@sebastienkb
Copy link

Brew installs it in /opt/homebrew/bin/blueutil for me :)

Correct, /usr/local/bin for Intel Macs, and /opt/homebrew/bin for M1 Macs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment