Skip to content

Instantly share code, notes, and snippets.

@MihailPreis
Created November 4, 2022 07:04
Show Gist options
  • Save MihailPreis/39d4314a459557e8fc1baa03d4b1c22f to your computer and use it in GitHub Desktop.
Save MihailPreis/39d4314a459557e8fc1baa03d4b1c22f to your computer and use it in GitHub Desktop.
Script for unpair/pair selected bluetooth devices
#!/bin/bash
#
# This script unpair selected device if is paired and vice versa
# for example, to quickly switch mouse and keyboard between macs
# Install `blueutil` with Brew before use this script!
# For get device ID run `blueutil --paired` and get "address" (as is)
# P.S. This script will be more convenient with Automator...
# for Homebrew on M1
export PATH="$PATH:/opt/homebrew/bin"
if ! which blueutil &> /dev/null; then
>&2 echo "blueutil not installed. Run: brew install blueutil"
exit 1
fi
_switch() {
if [[ "$(blueutil --is-connected $1)" = '1' ]]; then
blueutil --unpair $1
else
blueutil --unpair $1
sleep 0.2
blueutil --pair $1
sleep 0.2
blueutil --connect $1
fi
}
# Mike’s Magic Mouse
_switch "0c-e4-41-1c-07-a4"
# Mike’s Magic Keyboard
_switch "ac-49-db-f3-95-fc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment