diff --git a/Python3/Examples/bglib_test_scanner.py b/Python3/Examples/bglib_test_scanner.py new file mode 100644 index 0000000..e247413 --- /dev/null +++ b/Python3/Examples/bglib_test_scanner.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python3 + +""" Bluegiga BGAPI/BGLib implementation + +Changelog: + 2013-04-11 - Initial release + +============================================ +Bluegiga BGLib Python interface library test scanner app +2013-04-10 by Jeff Rowberg +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__ = "jeff@rowberg.net" + + +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/ttyACM0" + baud_rate = 115200 + packet_mode = True + + # 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() diff --git a/Python3/bglib.py b/Python3/bglib.py index 4556d08..63f116f 100644 --- a/Python3/bglib.py +++ b/Python3/bglib.py @@ -615,7 +615,7 @@ def wifi_cmd_i2c_stop(self, endpoint): debug = False def send_command(self, ser, packet): - if self.packet_mode: packet = chr(len(packet) & 0xFF) + packet + if self.packet_mode: packet = bytes([len(packet) & 0xFF]) + packet if self.debug: print('=>[ ' + ' '.join(['%02X' % b for b in packet]) + ' ]') self.on_before_tx_command() self.busy = True