Skip to content

Instantly share code, notes, and snippets.

@justmoon
Created December 24, 2015 23:11
Show Gist options
  • Save justmoon/df5851cdc741ad8f80be to your computer and use it in GitHub Desktop.
Save justmoon/df5851cdc741ad8f80be to your computer and use it in GitHub Desktop.
Automatic screen locking via Bluetooth
#!/bin/bash
# Simple script to lock your screen when your phone is out of range. Don't rely
# on this (obviously), but it can be used to provide a faster idle lock if you do
# forget to lock your screen.
# Bluetooth address of the unlock device
DEVICE=AA:BB:CC:00:00:00
# Name of the unlock device
DEVICE_NAME="Nexus 6P"
# How long before the script starts looking for the Bluetooth device (in ms)
IDLE_TIME=10000
while true; do
if [ `xprintidle` -gt $IDLE_TIME ];
then
echo "Checking $DEVICE_NAME state..."
state=`hcitool name $DEVICE`
if [ "$state" != "$DEVICE_NAME" ]; then
echo "Device absent, locking..."
dbus-send --type=method_call --dest=org.gnome.ScreenSaver \
/org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock
# After locking, we sleep for a while. This prevents you from getting
# locked out over and over in case of a bug.
sleep 60
else
# Don't check more than every five seconds
sleep 5
fi
fi
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment