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
|
# Test BLE GAP advertising and scanning
from micropython import const
import time, machine, bluetooth
_IRQ_SCAN_RESULT = const(5)
_IRQ_SCAN_DONE = const(6)
ADV_TIME_MS = 3000
def instance0():
multitest.globals(BDADDR=ble.config("mac"))
multitest.next()
adv_data = b"\x02\x01\x06\x04\xffMPY"
print("gap_advertise(100_000, connectable=False)")
ble.gap_advertise(100_000, adv_data, connectable=False)
time.sleep_ms(ADV_TIME_MS)
print("gap_advertise(20_000, connectable=True)")
ble.gap_advertise(20_000, adv_data, connectable=True)
time.sleep_ms(ADV_TIME_MS)
print("gap_advertise(None)")
ble.gap_advertise(None)
ble.active(0)
def instance1():
multitest.next()
finished = False
matched_adv_types = {}
matched_adv_data = None
def irq(ev, data):
nonlocal finished, matched_adv_types, matched_adv_data
if ev == _IRQ_SCAN_RESULT:
addr_type, addr, adv_type, rssi, adv_data = data
if addr_type == BDADDR[0] and addr == BDADDR[1]:
matched_adv_types[adv_type] = True
if matched_adv_data is None:
matched_adv_data = bytes(adv_data)
else:
if adv_data != matched_adv_data:
matched_adv_data = b"MISMATCH"
elif ev == _IRQ_SCAN_DONE:
finished = True
try:
ble.config(rxbuf=2000)
except:
pass
ble.irq(irq)
ble.gap_scan(2 * ADV_TIME_MS, 30000, 30000)
while not finished:
machine.idle()
ble.active(0)
print("adv_types:", sorted(matched_adv_types))
print("adv_data:", matched_adv_data)
ble = bluetooth.BLE()
ble.active(1)
|