forked from wasp-os/ota-dfu-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.py
executable file
·114 lines (85 loc) · 3.08 KB
/
scan.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
#!/usr/bin/env python3
#------------------------------------------------------------------------------
# Device scan
#------------------------------------------------------------------------------
from subprocess import call
import pexpect
import signal
import sys
#------------------------------------------------------------------------------
# Bluetooth LE scan for advertising peripheral devices
#------------------------------------------------------------------------------
class HciTool:
def __init__( self, advert_name ):
self.advert_name = advert_name
return
def scan( self ):
try:
self.hcitool = pexpect.spawn('hcitool lescan')
#self.hcitool.logfile = sys.stdout
index = self.hcitool.expect(['LE Scan ...'], 1)
except pexpect.EOF:
self.hcitool.terminate(force=True)
return []
except Exception as err:
print("scan: exception: {0}".format(sys.exc_info()[0]))
self.hcitool.terminate(force=True)
return []
if index != 0:
print("scan: failed")
self.hcitool.terminate(force=True)
return []
list = []
for dummy in range(0, 2):
try:
list.append(self.hcitool.readline())
except pexpect.TIMEOUT:
break
if list == []:
return []
# Eliminate duplicate items in list
list = set(list)
# Remove non self.advert_name units
if self.advert_name != None:
list = [item for item in list if self.advert_name in item]
# remove empty items from list
while '\r\n' in list:
list.remove('\r\n')
# Strip newline from items in list
list = [item.strip() for item in list]
list = list[0:2]
# Close pexpect (release device for subsequent use)
self.hcitool.terminate(force=True)
return list
#------------------------------------------------------------------------------
#
#------------------------------------------------------------------------------
class Scan:
def __init__( self, advert_name ):
self.advert_name = advert_name
return
def scan(self):
scan_list = []
try:
hcitool = HciTool(self.advert_name)
scan_list = hcitool.scan()
except KeyboardInterrupt:
# On Cntl-C
pass;
except pexpect.TIMEOUT:
print("scan: pexpect.TIMEOUT")
pass
except Exception as e:
print("scan: exception: {0} ".format(sys.exc_info()[0]))
pass
return scan_list
#------------------------------------------------------------------------------
#
#------------------------------------------------------------------------------
if __name__ == '__main__':
# Do not litter the world with broken .pyc files.
sys.dont_write_bytecode = True
#scanner = Scan("DfuTarg") # specific advertisement name
scanner = Scan(None) # any advertising name
scanner.scan()
print("scan complete")