2 # Automatically lock the screen when bluetooth beacon moves out of range
5 from beacontools
import BeaconScanner
, IBeaconFilter
, IBeaconAdvertisement
8 from gi
.repository
import Gio
10 global isHere
, beaconTimeout
11 global screenSaverSetActive
, screenSaverGetActive
, sessionIdleTime
13 def handler(signum
, frame
):
17 def callback(bt_addr
, rssi
, packet
, additional_info
):
21 sessionBus
= dbus
.SessionBus()
23 screenSaver
= sessionBus
.get_object("org.gnome.ScreenSaver", "/org/gnome/ScreenSaver")
24 screenSaverIface
= dbus
.Interface(screenSaver
, 'org.gnome.ScreenSaver')
25 screenSaverSetActive
= screenSaverIface
.get_dbus_method("SetActive")
26 screenSaverGetActive
= screenSaverIface
.get_dbus_method("GetActive")
28 mutter
= sessionBus
.get_object('org.gnome.Mutter.IdleMonitor', '/org/gnome/Mutter/IdleMonitor/Core')
29 mutterIface
= dbus
.Interface(mutter
, 'org.gnome.Mutter.IdleMonitor')
30 sessionIdleTime
= mutterIface
.get_dbus_method("GetIdletime")
32 # scan for all iBeacon advertisements regardless from which beacon
33 #scanner = BeaconScanner(callback,
34 # packet_filter=IBeaconAdvertisement
37 signal
.signal(signal
.SIGINT
, handler
)
43 scanner
= BeaconScanner(callback
,
44 device_filter
=IBeaconFilter(uuid
="INSERT BEACON UUID HERE")
49 idleTime
= sessionIdleTime() / 1000
52 #print("Beacon observed")
56 #print("Beacon not observed,", idleTime, "seconds since last activity,", beaconTimeout, "seconds since beacon seen")
57 if idleTime
>= 60 and beaconTimeout
>= 60:
58 if not screenSaverGetActive():
59 print("No activity and not seen for at least 60 seconds, locking screen")
60 screenSaverSetActive(True)