Skip to content

Instantly share code, notes, and snippets.

@pablojimeno
Forked from thiagoghisi/bluetooth-restart.sh
Created January 28, 2020 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pablojimeno/04dc6603913f785f3af73e04f4e87d13 to your computer and use it in GitHub Desktop.
Save pablojimeno/04dc6603913f785f3af73e04f4e87d13 to your computer and use it in GitHub Desktop.
Script for Mac OSX to Restart Bluetooth service & Reconnect all recently paired devices
#!/bin/bash
echo "Restarting bluetooth service..."
blueutil -p 0 && sleep 1 && blueutil -p 1
echo "Waiting bluetooth service to be restored..."
until blueutil -p | grep "1" >/dev/null; do sleep 1; done
echo "Searching for devices not connected..."
devices=($(blueutil --paired | grep "not connected" | awk -F '[ ,]' '{print $2}'))
echo "Found ${#devices[@]} recently paired devices not connected"
for device in ${devices[@]}; do
for retry in {1..5}; do
echo "Trying to connect to ${device} ..."
if blueutil --connect ${device}; then break; fi
echo "Failed to connect to ${device}"
sleep 1
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment