Skip to content

Instantly share code, notes, and snippets.

@ofen
Last active May 9, 2020 22:18
Show Gist options
  • Save ofen/ce24ef20f106b0fc8b6391f44b4fb3f6 to your computer and use it in GitHub Desktop.
Save ofen/ce24ef20f106b0fc8b6391f44b4fb3f6 to your computer and use it in GitHub Desktop.
Simple Bluetooth auto-connect script for systemd
[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target
[Service]
Type=simple
ExecStart=/etc/rc.local start
ExecStop=/etc/rc.local stop
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no
Restart=on-failure
RestartSec=5s
#!/bin/sh
ARG="$1"
DEVICE_MAC="A0:B1:C2:D3:E4:F5"
start() {
bluetoothctl connect "$DEVICE_MAC"
while bluetoothctl info "$DEVICE_MAC" | grep -c 'Connected: yes' > /dev/null; do
echo "Device $DEVICE_MAC connected"
sleep 5
done
echo "Device $DEVICE_MAC disconnected"
exit 1
}
stop() {
bluetoothctl disconnect "$DEVICE_MAC"
}
case "$ARG" in
start ) start;;
stop ) stop;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment