-
Notifications
You must be signed in to change notification settings - Fork 5
/
bluefence.py
executable file
·69 lines (51 loc) · 1.51 KB
/
bluefence.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/python
# BlueFence version 0.1 (Prototype)
# Author: Yeni Setiawan
# Email : [email protected]
# Blog : https://sandalian.com
import bluetooth
import time
import sys
import os
SCAN_PERIOD = 5 # time between searches for device in seconds
IF_BT_GONE = 'xrandr --output eDP1 --brightness 0' # The command to run when the device is out of range
IF_BT_BACK = 'xrandr --output eDP1 --brightness 1' # The command to run when the device is back in range
MAX_MISSED = 3
VERBOSE = True
if len(sys.argv) < 2:
print("usage bluefence.py <btaddr>")
sys.exit(1)
btAddr = sys.argv[1]
btInRange = True
screenLocked = False
awayCounter = 0
print("Identifying device...")
try:
# initial check, see if mentioned BT device active. If it's not, clean exit
btName = bluetooth.lookup_name(btAddr,timeout=5)
if btName:
if VERBOSE:
print('OK: Found your device',btName)
while True:
who = bluetooth.lookup_name(btAddr,timeout=2)
if who:
status = 'near'
btInRange=True
awayCounter=0
os.system(IF_BT_BACK)
else:
awayCounter+=1
status = 'away'
if awayCounter > MAX_MISSED:
os.system(IF_BT_GONE)
status = 'MATI!'
awayCounter = 0
btInRange=False
time.sleep(SCAN_PERIOD)
print(status, '|', awayCounter, '|', btInRange, '|', time.strftime('%H:%M:%S'))
else:
print('ER: Your bluetooth device is not active')
sys.exit
# this usually happen when your PC's bluetooth is disabled.
except:
print('ER: Bluetooth on PC is not active')