10c4cc577SMatthias Ringwald#!/usr/bin/env python3 20c4cc577SMatthias Ringwald 3b1f6df3bSMatthias Ringwaldfrom btstack import btstack_server, btstack_client, event_factory 4*5f5762dbSMatthias Ringwaldimport sys 50c4cc577SMatthias Ringwald 60c4cc577SMatthias Ringwalddef packet_handler(packet): 7b1f6df3bSMatthias Ringwald global btstack_client 8b1f6df3bSMatthias Ringwald if isinstance(packet, event_factory.BTstackEventState): 9b1f6df3bSMatthias Ringwald print("BTstack state: %u" % packet.get_state()) 10b1f6df3bSMatthias Ringwald if packet.get_state() == 2: 11b1f6df3bSMatthias Ringwald print('BTstack up and running, starting scan') 12b1f6df3bSMatthias Ringwald btstack_client.gap_le_scan_start() 13b1f6df3bSMatthias Ringwald if isinstance(packet, event_factory.GAPEventAdvertisingReport): 140c4cc577SMatthias Ringwald print(packet) 150c4cc577SMatthias Ringwald 16*5f5762dbSMatthias Ringwald# check version 17*5f5762dbSMatthias Ringwaldif sys.version_info < (3, 0): 18*5f5762dbSMatthias Ringwald print('BTstack Server Client library, requires Python 3.x or higher.\n') 19*5f5762dbSMatthias Ringwald sys.exit(10) 20*5f5762dbSMatthias Ringwald 210c4cc577SMatthias Ringwald# Conrtrol for BTstack Server 220c4cc577SMatthias Ringwaldbtstack_server = btstack_server.BTstackServer() 230c4cc577SMatthias Ringwald 240c4cc577SMatthias Ringwald# start BTstack Server from .dll 250c4cc577SMatthias Ringwaldbtstack_server.load() 260c4cc577SMatthias Ringwald# btstack_server.set_storage_path("/tmp") 270c4cc577SMatthias Ringwaldbtstack_server.run_tcp() 280c4cc577SMatthias Ringwald 290c4cc577SMatthias Ringwald# Client for BTstack Server 300c4cc577SMatthias Ringwaldbtstack_client = btstack_client.BTstackClient() 310c4cc577SMatthias Ringwald 320c4cc577SMatthias Ringwald# connect to slient, register for HCI packets and power up 33a43b5e87SMatthias Ringwaldok = btstack_client.connect() 34a43b5e87SMatthias Ringwaldif ok: 350c4cc577SMatthias Ringwald btstack_client.register_packet_handler(packet_handler) 369fb9416bSMatthias Ringwald btstack_client.btstack_set_power_mode(1) 370c4cc577SMatthias Ringwald btstack_client.run() 38