Skip to content

Instantly share code, notes, and snippets.

@hoblin
Forked from blz777/teleport-bluetooth.sh
Last active April 15, 2023 20:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hoblin/68c6f60b9a6bf1aeb8ff5b64bec4cf98 to your computer and use it in GitHub Desktop.
Save hoblin/68c6f60b9a6bf1aeb8ff5b64bec4cf98 to your computer and use it in GitHub Desktop.
Switch bluetooth devices between two macbook machines over SSH without password.
#!/bin/sh
# Switch bluetooth devices from one macbook machine to another over SSH
#
# How to configure script?
# - Configure the source and destination variables to point to the username@host to the respective machines - machines should be accessible over SSH.
# The script identifies which machine it is running on depending on the username, so we can use the same script on both machines.
# If usernames are the same on both machines, we should modify the script to identify source and destination using another method,
# e.g. a hostname. Another option is to have different scripts on both machines which set directly $source and $destination.
#
# Install prerequisite tools:
# brew install blueutil
# brew install bluetoothconnector
#
# - Configure bluetooth addresses of devices. To see the device addresses:
# ❯ blueutil --paired
# address: 1c-36-bb-0e-97-42, connected (master, -60 dBm), not favourite, paired, name: "User’s Mouse", recent access date: 2021-03-09 19:14:55 +0000
# address: 68-fe-f7-6d-5f-7f, connected (master, -57 dBm), not favourite, paired, name: "User's Keyboard", recent access date: 2021-03-09 19:14:57 +0000
#
# Configure SSH to the destination machine to use SSH keys, so it does not require a password:
# https://www.ssh.com/ssh/copy-id
#
# Make sure the destinatino macbook pro will wake up when trying to ssh to it:
# Preferences -> Battery -> Power Adapter -> (check) Wake for network access
#
# How to use tool?
# 1. Run the script.
# 2. Wait for the prompt to hardware-restart bluetooth devices.
# 3. Turn off both bluetooth devices. Wait two seconds. Turn on both bluetooth devices.
# 4. Bluetooth devices should now be connected to the destination machine - enjoy! :)
username=$(whoami)
if [[ $username == "hoblin" ]]; then
source="hoblin@HoBook.local"
destination="eugenegurin@WorkBook.local"
else
source="eugenegurin@WorkBook.local"
destination="hoblin@HoBook.local"
fi
touchpad_bluetooth_address=a0-78-17-e1-49-c5
echo "Teleporting from $destination ..."
response=$( ssh -T $destination << EOF
blueutil -p 1
BluetoothConnector --disconnect $touchpad_bluetooth_address
blueutil --unpair $touchpad_bluetooth_address
sleep 3
blueutil -p 0
echo OK
EOF
)
if [[ $response == "OK" ]]; then
echo "Disconnected on $destination."
fi
sleep_seconds=10
echo "Please hardware-restart bluetooth devices (waiting $sleep_seconds seconds)."
sleep $sleep_seconds
blueutil -p 1
blueutil --pair $touchpad_bluetooth_address
sleep 3
BluetoothConnector --connect $touchpad_bluetooth_address
if [[ $response == "OK" ]]; then
echo "Teleporting completed! :)"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment