forked from juanmaneo/BNNB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluetooth-near.py
executable file
·369 lines (301 loc) · 12.7 KB
/
bluetooth-near.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# MIT License
#
# Copyright (c) 2019 Jean-Manuel CABA
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Based on numerous project that are almost similar in codes
# see: https://github.com/sandalian/bluefence/blob/master/bluefence.py
# https://github.com/FrederikBolding/bluetooth-proximity
# https://github.com/ewenchou/bluetooth-proximity/blob/master/bt_proximity/bt_rssi.py
# https://github.com/dagar/bluetooth-proximity/blob/master/proximity_dagar.py
# https://github.com/ewenchou/bluetooth-proximity/blob/master/examples/lnsm/lnsm.py
# intended as a replacement for Blueproximity
# for RSSI to distance
# read papers and information:
# https://www.cise.ufl.edu/~qathrady/reports/BLE.pdf and
# https://www.rn.inf.tu-dresden.de/dargie/papers/icwcuca.pdf
# https://iotandelectronics.wordpress.com/2016/10/07/how-to-calculate-distance-from-the-rssi-value-of-the-ble-beacon/
# https://stackoverflow.com/questions/36399927/distance-calculation-from-rssi-ble-android
# here https://python-forum.io/Thread-Python-Ble-Distance-Problem
import time
import bluetooth
# noinspection PyProtectedMember
import bluetooth._bluetooth as bt
import struct
import array
import fcntl
class BleRssi(object):
def __init__(self, addr):
self._addr = addr
self._hci_sock = bt.hci_open_dev()
self._hci_fd = self._hci_sock.fileno()
self._bt_sock = bluetooth.BluetoothSocket(bluetooth.L2CAP)
self._bt_sock.settimeout(10)
self._connected = False
self._cmd_pkt = None
def _prep_cmd_pkt(self):
_request_command = struct.pack(b'6sB17s', bt.str2ba(self._addr), bt.ACL_LINK, b'\0' * 17)
_request_as_array = array.array('b', _request_command)
_ = fcntl.ioctl(self._hci_fd, bt.HCIGETCONNINFO, _request_as_array, 1)
_handle = struct.unpack(b'8xH14x', _request_as_array.tobytes())[0]
self._cmd_pkt = struct.pack('H', _handle)
def connect(self):
# noinspection PyUnresolvedReferences
self._bt_sock.connect_ex((self._addr, 1))
self._connected = True
# see code of https://github.com/r10r/bluez/blob/master/lib/hci.c
def get_rssi(self):
try:
# noinspection PyPep8Naming
READ_RSSI_RP_SIZE = 4
if not self._connected:
self.connect()
if self._cmd_pkt is None:
self._prep_cmd_pkt()
_rssi = bt.hci_send_req(self._hci_sock,
bt.OGF_STATUS_PARAM,
bt.OCF_READ_RSSI,
bt.EVT_CMD_COMPLETE,
READ_RSSI_RP_SIZE,
self._cmd_pkt)
_rssi = struct.unpack('b', _rssi[3].to_bytes(1, 'big'))
_rssi = _rssi[0]
if _rssi is not None:
return float(_rssi)
self._connected = False
return None
except IOError:
self._connected = False
return None
class BleTxPower(object):
def __init__(self, addr):
self._addr = addr
self._hci_sock = bt.hci_open_dev()
self._hci_fd = self._hci_sock.fileno()
self._bt_sock = bluetooth.BluetoothSocket(bluetooth.L2CAP)
self._bt_sock.settimeout(10)
self._connected = False
self._cmd_pkt = None
def _prep_cmd_pkt(self):
_request_command = struct.pack(b'6sB17s', bt.str2ba(self._addr), bt.ACL_LINK, b'\0' * 17)
_request_as_array = array.array('b', _request_command)
fcntl.ioctl(self._hci_fd, bt.HCIGETCONNINFO, _request_as_array, 1)
_handle = struct.unpack(b'8xH14x', _request_as_array.tobytes())[0]
self._cmd_pkt = struct.pack('H', _handle)
def connect(self):
# noinspection PyUnresolvedReferences
self._bt_sock.connect_ex((self._addr, 1))
self._connected = True
# see code of https://github.com/r10r/bluez/blob/master/lib/hci.c
# noinspection PyPep8Naming
def get_tx_power(self):
try:
# noinspection PyPep8Naming
READ_TRANSMIT_POWER_LEVEL_RP_SIZE = 4
if not self._connected:
self.connect()
if self._cmd_pkt is None:
self._prep_cmd_pkt()
_tx_power = bt.hci_send_req(self._hci_sock,
bt.OGF_HOST_CTL,
bt.OCF_READ_TRANSMIT_POWER_LEVEL,
bt.EVT_CMD_COMPLETE,
READ_TRANSMIT_POWER_LEVEL_RP_SIZE,
self._cmd_pkt)
_tx_power = struct.unpack('b', _tx_power[3].to_bytes(1, 'big'))[0]
if _tx_power is not None:
return float(_tx_power)
self._connected = False
return None
except IOError:
self._connected = False
return None
def get_rssi(bt_addr):
_b = BleRssi(addr=bt_addr)
_rssi = _b.get_rssi()
return _rssi
def get_tx_power(bt_addr):
_b = BleTxPower(addr=bt_addr)
_tx_power = _b.get_tx_power()
return _tx_power
# (tuned for my phone ... may need tuning for other phones)
MY_PHONE_RSSI_AT_1m = -2.0
def rssi_to_distance(input_rssi):
import math
n = 2.5 # Path loss exponent(n) = Range from 2 to 4
measured_power = MY_PHONE_RSSI_AT_1m # aka RSSI value at 1 meter
x = float((measured_power - float(input_rssi)) / (10.0 * n))
distance = math.pow(10, x)
return distance
def old_calculate_accuracy(tx_power, input_rssi):
import math
# noinspection PyBroadException,PyPep8
try:
ratio = float(input_rssi) / float(tx_power)
return 0.89976 * math.pow(ratio, 7.7095) + 0.111
except:
return rssi_to_distance(input_rssi)
def new_calculate_accuracy(tx_power, input_rssi):
import math
# noinspection PyBroadException,PyPep8
try:
ratio = float(input_rssi) / float(tx_power)
return 0.42093 * math.pow(ratio, 6.9476) + 0.54992
except:
return rssi_to_distance(input_rssi)
def calculate_accuracy(tx_power, input_rssi):
d1 = float(old_calculate_accuracy(tx_power, input_rssi))
d2 = float(new_calculate_accuracy(tx_power, input_rssi))
return (d1 + d2) / 2.0
# in case no measurement ... default to 1 meter values
# if there was not previous measurement
previous_avg_rssi = MY_PHONE_RSSI_AT_1m
previous_avg_distance = 1.0
previous_tx_power = MY_PHONE_RSSI_AT_1m
def get_distance_approximation(bt_addr):
global previous_avg_rssi
global previous_avg_distance
global previous_tx_power
# this numbers are totally arbitrary
num_ok_measure = 7
loop_step_sleep = 0.005
all_rssi = []
all_distances = []
nb_measure_counter = 0
while nb_measure_counter < num_ok_measure:
_rssi_bt = get_rssi(bt_addr)
if _rssi_bt is None or int(_rssi_bt) == 0:
continue
nb_measure_counter += 1
_rssi_bt = float(_rssi_bt)
distance1 = rssi_to_distance(_rssi_bt)
_tx_power_bt = get_tx_power(bt_addr)
if _tx_power_bt is None:
_tx_power_bt = previous_tx_power
previous_tx_power = _tx_power_bt
distance2 = calculate_accuracy(_tx_power_bt, _rssi_bt)
if distance2 > 1.0:
distance = distance1
else:
# use distance2 to tune approximation
distance = (distance1 * 3 + distance2) / 4.0
all_rssi.append(_rssi_bt)
all_distances.append(distance)
time.sleep(loop_step_sleep)
assert len(all_rssi) == len(all_distances)
if len(all_rssi) == 0:
avg_rssi = previous_avg_rssi
avg_distance = previous_avg_distance
else:
avg_rssi = sum(all_rssi) / len(all_rssi)
avg_distance = sum(all_distances) / len(all_distances)
previous_avg_rssi = avg_rssi
previous_avg_distance = avg_distance
return avg_distance
VERBOSE = True
INFO = True
def _verbose_log(msg):
if VERBOSE:
print(msg)
def _main():
import sys
import os
# time between searches for device in seconds
scan_period = 1
# The command to run when the device is out of range
# noinspection PyPep8
run_xscreensaver_if_killed = '(xscreensaver-command --time 2>&1|grep -q "no screensaver is running" && xscreensaver &)'
if_bt_gone = run_xscreensaver_if_killed + '; xscreensaver-command -lock'
# The command to run when the device is back in range
# noinspection PyPep8
if_bt_back = '(xscreensaver-command -time 2>&1|grep -q "screen non-blanked since" && xscreensaver-command -deactivate || killall xscreensaver); ' + run_xscreensaver_if_killed
max_missed = 3
max_distance_in_cm = 90.0
if len(sys.argv) < 2:
print("Usage " + sys.argv[0] + " <btaddr>")
sys.exit(1)
bt_addr = sys.argv[1]
bt_in_range = True
away_counter = 0
far_counter = 0
near_counter = 0
max_near_to_unlock = 1
status = 'near'
_verbose_log("Identifying device...")
# noinspection PyPep8,PyBroadException
try:
# first scan to ensure bluetooth works
nearby_devices = bluetooth.discover_devices(duration=8, lookup_names=True, flush_cache=True, lookup_class=False)
print("found %d devices" % len(nearby_devices))
print("performing inquiry...")
# initial check, see if mentioned BT device active. If it's not, clean exit
# noinspection PyPep8Naming
btName = bluetooth.lookup_name(bt_addr, timeout=5)
if btName:
_verbose_log('OK: Found your device ' + btName)
while True:
who = bluetooth.lookup_name(bt_addr, timeout=2)
if who:
assert btName == who
bt_in_range = True
distance = get_distance_approximation(bt_addr)
print("Approximate distance: " + str(distance * 100.0) + " cm")
if distance * 100.0 > max_distance_in_cm:
_verbose_log('DEVICE FAR')
status = 'far'
far_counter += 1
near_counter = 0
if far_counter >= max_missed:
_verbose_log('RUNNING GONE COMMAND AFTER THRESHOLD (FAR)')
status = 'far_need_lock'
os.system(if_bt_gone)
else:
_verbose_log('DEVICE NEAR')
near_counter += 1
away_counter = 0
far_counter = 0
if near_counter >= max_near_to_unlock:
status = 'near'
os.system(if_bt_back)
else:
_verbose_log('DEVICE AWAY')
near_counter = 0
away_counter += 1
status = 'away'
if away_counter >= max_missed:
_verbose_log('RUNNING GONE COMMAND AFTER THRESHOLD (AWAY)')
os.system(if_bt_gone)
status = 'away_need_lock'
bt_in_range = False
time.sleep(scan_period)
print(status,
'| far counter=', far_counter,
'| away counter', away_counter,
'|', bt_in_range, '|', time.strftime('%H:%M:%S'))
else:
sys.exit('ERROR: Your bluetooth device is not active')
# this usually happen when your PC's bluetooth is disabled.
except:
import traceback
traceback.print_exc()
sys.exit('ERROR: Bluetooth on machine is not active or unknown error')
if __name__ == "__main__":
_main()