-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBTScanner.py
62 lines (45 loc) · 1.16 KB
/
BTScanner.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
#!/usr/bin/env python3
import time
import select
import requests
import json
import logging
import bluetooth
import uuid
import re
import sys
import os
import socket
BT_Device = {}
retry_count = 0
logging.basicConfig(
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S')
class MyDiscoverer(bluetooth.DeviceDiscoverer):
def pre_inquiry(self):
self.done = False
def device_discovered(self, address, device_class, rssi, name):
# print("{} - {}".format(address, name))
address = address.replace(":", "")
BT_Device[address]=rssi
# Compute distance using Proxmity API later
def inquiry_complete(self):
logging.warning("Scanning completed")
self.done = True
BT_Device.clear()
d = MyDiscoverer()
d.find_devices(lookup_names=True)
readfiles = [d, ]
rfds = select.select(readfiles, [], [])[0]
while True:
if d in rfds:
d.process_event()
if d.done:
time.sleep(0.02)
try:
d.find_devices(lookup_names=True)
except:
pass
# Create a small sleep to flush cache in dBus
time.sleep(1)