+# Automatically lock the screen when bluetooth beacon moves out of range
+# Requires GNOME Shell
+
+from beacontools import BeaconScanner, IBeaconFilter, IBeaconAdvertisement
+import time
+import signal
+import dbus
+
+global isHere, screenSaverSetActive, screenSaverGetActive
+
+def handler(signum, frame):
+ scanner.stop()
+ exit(0)
+
+def callback(bt_addr, rssi, packet, additional_info):
+ global isHere
+ isHere = True
+
+sessionBus = dbus.SessionBus()
+screenSaver = sessionBus.get_object("org.gnome.ScreenSaver", "/org/gnome/ScreenSaver")
+screenSaverIface = dbus.Interface(screenSaver, 'org.gnome.ScreenSaver')
+screenSaverSetActive = screenSaverIface.get_dbus_method("SetActive")
+screenSaverGetActive = screenSaverIface.get_dbus_method("GetActive")
+
+# To find beacon:
+# scan for all iBeacon advertisements regardless from which beacon
+#scanner = BeaconScanner(callback,
+# packet_filter=IBeaconAdvertisement
+#)
+
+signal.signal(signal.SIGINT, handler)
+
+while True:
+ isHere = False
+ scanner = BeaconScanner(callback,
+ device_filter=IBeaconFilter(uuid="INSERT YOUR UUID HERE")
+ )
+ scanner.start()
+ time.sleep(45)
+ scanner.stop()
+
+ if isHere:
+ print("Beacon is here")
+ else:
+ print("Beacon is gone")
+ if not screenSaverGetActive():
+ print("Locking screen")
+ screenSaverSetActive(True)