def main(args=None): global opts parser = argparse.ArgumentParser( description="Track BLE advertised packets") parser.add_argument( "-e", "--eddy", action="store_true", default=False, help="Look specificaly for Eddystone messages.", ) parser.add_argument( "-m", "--mac", type=check_mac, action="append", help="Look for these MAC addresses.", ) parser.add_argument( "-r", "--ruuvi", action="store_true", default=False, help="Look only for Ruuvi tag Weather station messages", ) parser.add_argument( "-p", "--pebble", action="store_true", default=False, help="Look only for Pebble Environment Monitor", ) parser.add_argument( "-A", "--atcmi", action="store_true", default=False, help="Look only for ATC_MiThermometer tag messages", ) parser.add_argument( "-R", "--raw", action="store_true", default=False, help="Also show the raw data.", ) parser.add_argument( "-a", "--advertise", type=int, default=0, help= "Broadcast like an EddyStone Beacon. Set the interval between packet in millisec", ) parser.add_argument( "-u", "--url", type=str, default="", help="When broadcasting like an EddyStone Beacon, set the url.", ) parser.add_argument( "-t", "--txpower", type=int, default=0, help="When broadcasting like an EddyStone Beacon, set the Tx power", ) parser.add_argument( "-D", "--device", type=int, default=0, help="Select the hciX device to use (default 0, i.e. hci0).", ) try: opts = parser.parse_args() except Exception as e: parser.error("Error: " + str(e)) sys.exit() event_loop = asyncio.get_event_loop() # First create and configure a raw socket mysocket = aiobs.create_bt_socket(opts.device) # create a connection with the raw socket # This used to work but now requires a STREAM socket. # fac=event_loop.create_connection(aiobs.BLEScanRequester,sock=mysocket) # Thanks to martensjacobs for this fix fac = event_loop._create_connection_transport(mysocket, aiobs.BLEScanRequester, None, None) # Start it conn, btctrl = event_loop.run_until_complete(fac) # Attach your processing btctrl.process = my_process if opts.advertise: command = aiobs.HCI_Cmd_LE_Advertise(enable=False) event_loop.run_until_complete(btctrl.send_command(command)) command = aiobs.HCI_Cmd_LE_Set_Advertised_Params( interval_min=opts.advertise, interval_max=opts.advertise) event_loop.run_until_complete(btctrl.send_command(command)) if opts.url: myeddy = EddyStone(param=opts.url) else: myeddy = EddyStone() if opts.txpower: myeddy.power = opts.txpower command = aiobs.HCI_Cmd_LE_Set_Advertised_Msg(msg=myeddy) event_loop.run_until_complete(btctrl.send_command(command)) command = aiobs.HCI_Cmd_LE_Advertise(enable=True) event_loop.run_until_complete(btctrl.send_command(command)) # Probe event_loop.run_until_complete(btctrl.send_scan_request()) try: # event_loop.run_until_complete(coro) event_loop.run_forever() except KeyboardInterrupt: print("keyboard interrupt") finally: print("closing event loop") event_loop.run_until_complete(btctrl.stop_scan_request()) command = aiobs.HCI_Cmd_LE_Advertise(enable=False) event_loop.run_until_complete(btctrl.send_command(command)) conn.close() event_loop.close()
#Start it conn, btctrl = event_loop.run_until_complete(fac) #Attach your processing btctrl.process = my_process if opts.advertise: command = aiobs.HCI_Cmd_LE_Advertise(enable=False) btctrl.send_command(command) command = aiobs.HCI_Cmd_LE_Set_Advertised_Params( interval_min=opts.advertise, interval_max=opts.advertise) btctrl.send_command(command) if opts.url: myeddy = EddyStone(param=opts.url) else: myeddy = EddyStone() if opts.txpower: myeddy.power = opts.txpower command = aiobs.HCI_Cmd_LE_Set_Advertised_Msg(msg=myeddy) btctrl.send_command(command) command = aiobs.HCI_Cmd_LE_Advertise(enable=True) btctrl.send_command(command) #Probe btctrl.send_scan_request() try: # event_loop.run_until_complete(coro) event_loop.run_forever() except KeyboardInterrupt: print('keyboard interrupt') finally: print('closing event loop') btctrl.stop_scan_request()