![]() One simple note if it wasn't already obvious: make sure the Hammerspoon app, the headphones script and the watcher are configured on all Macs you want this functionality on.This project is looking for a new maintainer. If the toggle subcommand is chosen, we check whether the headphones are already connected and disconnect if so, or vice versa.įrom now onwards, everytime the MacBook goes to sleep the headphones are disconnected automatically and you're free to turn them off or switch to a different MacBook seamlessly. We identify the MAC address for the headphones from the previously defined array and pass them to the bluetoothconnector CLI based on the subcommand. In the above snippet of code, we define four subcommands, status, toggle, disconnect and connect. $CMD -disconnect " $TARGET_MAC" -notify >/dev/null 2> &1 & $CMD -connect " $TARGET_MAC" -notify >/dev/null 2> &1 &Įcho "Disconnecting $2 with mac address $TARGET_MAC." If ] thenĮcho "Connecting $2 with mac address $TARGET_MAC." OUTPUT = " $($CMD -status " $TARGET_MAC" )" Remember to redefine the array based on your headphones' name and MAC address. You can install BluetoothConnector using Homebrew by running brew install bluetoothconnector. This command shows a list of all the bluetooth devices paired to your MacBook and their respective MAC address. You can find the MAC addresses you are interesting in by running the bluetoothconnector -status command. Here, we create associative array that maps a human readable headphone name to it's MAC address. Let's try to understand how the script works by looking at the relevant parts. I have made the script available as a Github Gist, so feel free to skip the rest of the blog post if you would rather look at the script directly. ![]() It uses the BluetoothConnector command line tool to get the status, connect, disconnect or toggle bluetooth devices using their MAC address. Headphones is simple shell script that disconnects the headphones with the given name. The watcher is started when Hammerspoon is first launched. We'll use this inbuilt framework to listen for relevant system events by registering and starting a watcher. MacOS provides the caffienate system/command to prevent the system from going to sleep. If the type of the event is systemWillSleep, then the headphones disconnect command is executed. new (disconnectHeadphonesOnSleep )įirst, we create a new function called disconnectHeadphonesOnSleep that accepts an event type. Shell ( "~/bin/headphones disconnect sony-wh-1000-xm4" ) Let's look at my Hammerspoon configuration to see how it can be done: - wrapper function for shell commandsįunction disconnectHeadphonesOnSleep (eventType ) If you're unfamiliar with Hammerspoon, it's a macOS based automation tool that allows you interact with various parts of the operating system using a Lua based scripting environment. Thankfully, Hammerspoon provides an easy way to hook into system's sleep events. To avoid constant disconnects, you can adjust your Mac's sleep/wake configuration or use an app like Amphetamine to keep your Mac awake. The solution I came up with is to disconnect my headphones when the MacBook goes to sleep. If I want to switch the headphones to my other MacBook they will have to be manually disconnected first. If I close my MacBook without disconnecting, they will stay connected.When the headphones are turned off without disconnecting, they connect to the machine they were previously connected to when they are turned back on.This is not specific to these but bluetooth headphones in general. I use them on both my personal and work MacBook.īut I have two minor annoyances with how they behave. They are light, have great noise cancellation and tremendous battery life. My current favorite headphones are the Sony-WH1000XM4s.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |