Created
July 7, 2019 23:56
-
-
Save thiagoghisi/50c3ba835ea72cdb0318fb3306fd2c76 to your computer and use it in GitHub Desktop.
Script for Mac OSX to Restart Bluetooth service & Reconnect all recently paired devices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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
Here's another version adapted from above that selects only recently paired devices (within the last day), and also prints out the device name. Note that the script requires a recent version of
blueutil
that can output JSON.