-
Notifications
You must be signed in to change notification settings - Fork 0
/
bglib_test_scanner.py
118 lines (92 loc) · 4.16 KB
/
bglib_test_scanner.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
#!/usr/bin/env python
""" Bluegiga BGAPI/BGLib implementation
Changelog:
2013-04-11 - Initial release
============================================
Bluegiga BGLib Python interface library test scanner app
2013-04-10 by Jeff Rowberg <[email protected]>
Updates should (hopefully) always be available at https://github.com/jrowberg/bglib
============================================
BGLib Python interface library code is placed under the MIT license
Copyright (c) 2013 Jeff Rowberg
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.
===============================================
"""
__author__ = "Jeff Rowberg"
__license__ = "MIT"
__version__ = "2013-04-11"
__email__ = "[email protected]"
import bglib, serial, time, datetime
# handler to notify of an API parser timeout condition
def my_timeout(sender, args):
# might want to try the following lines to reset, though it probably
# wouldn't work at this point if it's already timed out:
#ble.send_command(ser, ble.ble_cmd_system_reset(0))
#ble.check_activity(ser, 1)
print "BGAPI parser timed out. Make sure the BLE device is in a known/idle state."
# handler to print scan responses with a timestamp
def my_ble_evt_gap_scan_response(sender, args):
print "gap_scan_response",
t = datetime.datetime.now()
disp_list = []
disp_list.append("%ld.%03ld" % (time.mktime(t.timetuple()), t.microsecond/1000))
disp_list.append("%d" % args["rssi"])
disp_list.append("%d" % args["packet_type"])
disp_list.append("%s" % ''.join(['%02X' % b for b in args["sender"][::-1]]))
disp_list.append("%d" % args["address_type"])
disp_list.append("%d" % args["bond"])
disp_list.append("%s" % ''.join(['%02X' % b for b in args["data"]]))
print ' '.join(disp_list)
def main():
# NOTE: CHANGE THESE TO FIT YOUR TEST SYSTEM
port_name = "/dev/ttyUSB0"
port_name = "/dev/ttyACM3"
baud_rate = 115200
packet_mode = False
# create BGLib object
ble = bglib.BGLib()
ble.packet_mode = packet_mode
# add handler for BGAPI timeout condition (hopefully won't happen)
ble.on_timeout += my_timeout
# add handler for the gap_scan_response event
ble.ble_evt_gap_scan_response += my_ble_evt_gap_scan_response
# create serial port object and flush buffers
ser = serial.Serial(port=port_name, baudrate=baud_rate, timeout=1)
ser.flushInput()
ser.flushOutput()
# disconnect if we are connected already
ble.send_command(ser, ble.ble_cmd_connection_disconnect(0))
ble.check_activity(ser, 1)
# stop advertising if we are advertising already
ble.send_command(ser, ble.ble_cmd_gap_set_mode(0, 0))
ble.check_activity(ser, 1)
# stop scanning if we are scanning already
ble.send_command(ser, ble.ble_cmd_gap_end_procedure())
ble.check_activity(ser, 1)
# set scan parameters
ble.send_command(ser, ble.ble_cmd_gap_set_scan_parameters(0xC8, 0xC8, 1))
ble.check_activity(ser, 1)
# start scanning now
ble.send_command(ser, ble.ble_cmd_gap_discover(1))
ble.check_activity(ser, 1)
while (1):
# check for all incoming data (no timeout, non-blocking)
ble.check_activity(ser)
# don't burden the CPU
time.sleep(0.01)
if __name__ == '__main__':
main()