1# Test BLE GAP advertising and scanning 2 3from micropython import const 4import time, machine, bluetooth 5 6_IRQ_SCAN_RESULT = const(5) 7_IRQ_SCAN_DONE = const(6) 8 9ADV_TIME_S = 3 10 11 12def instance0(): 13 multitest.globals(BDADDR=ble.config("mac")) 14 multitest.next() 15 16 print("gap_advertise(100_000, connectable=False)") 17 ble.gap_advertise(100_000, b"\x02\x01\x06\x04\xffMPY", connectable=False) 18 time.sleep(ADV_TIME_S) 19 20 print("gap_advertise(20_000, connectable=True)") 21 ble.gap_advertise(20_000, b"\x02\x01\x06\x04\xffMPY", connectable=True) 22 time.sleep(ADV_TIME_S) 23 24 print("gap_advertise(None)") 25 ble.gap_advertise(None) 26 27 ble.active(0) 28 29 30def instance1(): 31 multitest.next() 32 finished = False 33 adv_types = set() 34 adv_data = None 35 36 def irq(ev, data): 37 nonlocal finished, adv_types, adv_data 38 if ev == _IRQ_SCAN_RESULT: 39 if data[0] == BDADDR[0] and data[1] == BDADDR[1]: 40 adv_types.add(data[2]) 41 if adv_data is None: 42 adv_data = bytes(data[4]) 43 else: 44 if adv_data != data[4]: 45 adv_data = b"MISMATCH" 46 elif ev == _IRQ_SCAN_DONE: 47 finished = True 48 49 try: 50 ble.config(rxbuf=2000) 51 except: 52 pass 53 ble.irq(irq) 54 ble.gap_scan(2 * ADV_TIME_S * 1000, 10000, 10000) 55 while not finished: 56 machine.idle() 57 ble.active(0) 58 print("adv_types:", sorted(adv_types)) 59 print("adv_data:", adv_data) 60 61 62ble = bluetooth.BLE() 63ble.active(1) 64