Skip to content

Instantly share code, notes, and snippets.

@idserda
Created March 3, 2022 06:35
Show Gist options
  • Save idserda/e03bf2f58ede1655f070e7f3541cbc46 to your computer and use it in GitHub Desktop.
Save idserda/e03bf2f58ede1655f070e7f3541cbc46 to your computer and use it in GitHub Desktop.
Toggle openHAB switch on bluetooth device connect Mac

bluetooth-changed.sh

#!/bin/bash
CONNECTED=`system_profiler SPBluetoothDataType | grep -B 2  "Connected: Yes"`

XM4=`echo $CONNECTED | grep XM4`
XM2=`echo $CONNECTED | grep XM2`
AIRPODS=`echo $CONNECTED | grep AirPods`

if [ ! -z "$XM4" ] || [ ! -z "$XM2" ] || [ ! -z "$AIRPODS" ];
then
	/usr/bin/curl --header "Content-Type: text/plain" --request POST --data "OFF" http://192.168.1.17:8090/rest/items/WerkkamerSpeakers
else
	/usr/bin/curl --header "Content-Type: text/plain" --request POST --data "ON" http://192.168.1.17:8090/rest/items/WerkkamerSpeakers
fi

~/Library/LaunchAgents/bluetooth-watcher.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>

    <key>Label</key>
    <string>bluetooth-watcher</string>

    <key>KeepAlive</key>
    <true/>

    <key>ProgramArguments</key>
    <array>
        <string>/Users/jidserda/scripts/bluetooth-changed.sh</string>
    </array>

    <key>WatchPaths</key>
    <array>
        <string>/Library/Preferences/com.apple.Bluetooth.plist</string>
    </array>

</dict>
</plist>

activate plist with launchctl load ~/Library/LaunchAgents/bluetooth-watcher.plist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment