2020-03-04 21:34:46 -05:00
|
|
|
# Test BLE GAP advertising and scanning
|
|
|
|
|
|
|
|
from micropython import const
|
|
|
|
import time, machine, bluetooth
|
|
|
|
|
2020-05-13 00:35:32 -04:00
|
|
|
_IRQ_SCAN_RESULT = const(5)
|
|
|
|
_IRQ_SCAN_DONE = const(6)
|
2020-03-04 21:34:46 -05:00
|
|
|
|
2023-03-08 00:44:43 -05:00
|
|
|
ADV_TIME_MS = 3000
|
2020-03-04 21:34:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
def instance0():
|
|
|
|
multitest.globals(BDADDR=ble.config("mac"))
|
|
|
|
multitest.next()
|
extmod/modbluetooth: Change scan result's "connectable" to "adv_type".
This commit changes the BLE _IRQ_SCAN_RESULT data from:
addr_type, addr, connectable, rssi, adv_data
to:
addr_type, addr, adv_type, rssi, adv_data
This allows _IRQ_SCAN_RESULT to handle all scan result types (not just
connectable and non-connectable passive scans), and to distinguish between
them using adv_type which is an integer taking values 0x00-0x04 per the BT
specification.
This is a breaking change to the API, albeit a very minor one: the existing
connectable value was a boolean and True now becomes 0x00, False becomes
0x02.
Documentation is updated and a test added.
Fixes #5738.
2020-03-09 20:45:03 -04:00
|
|
|
|
2023-03-08 00:44:43 -05:00
|
|
|
adv_data = b"\x02\x01\x06\x04\xffMPY"
|
|
|
|
|
extmod/modbluetooth: Change scan result's "connectable" to "adv_type".
This commit changes the BLE _IRQ_SCAN_RESULT data from:
addr_type, addr, connectable, rssi, adv_data
to:
addr_type, addr, adv_type, rssi, adv_data
This allows _IRQ_SCAN_RESULT to handle all scan result types (not just
connectable and non-connectable passive scans), and to distinguish between
them using adv_type which is an integer taking values 0x00-0x04 per the BT
specification.
This is a breaking change to the API, albeit a very minor one: the existing
connectable value was a boolean and True now becomes 0x00, False becomes
0x02.
Documentation is updated and a test added.
Fixes #5738.
2020-03-09 20:45:03 -04:00
|
|
|
print("gap_advertise(100_000, connectable=False)")
|
2023-03-08 00:44:43 -05:00
|
|
|
ble.gap_advertise(100_000, adv_data, connectable=False)
|
|
|
|
time.sleep_ms(ADV_TIME_MS)
|
extmod/modbluetooth: Change scan result's "connectable" to "adv_type".
This commit changes the BLE _IRQ_SCAN_RESULT data from:
addr_type, addr, connectable, rssi, adv_data
to:
addr_type, addr, adv_type, rssi, adv_data
This allows _IRQ_SCAN_RESULT to handle all scan result types (not just
connectable and non-connectable passive scans), and to distinguish between
them using adv_type which is an integer taking values 0x00-0x04 per the BT
specification.
This is a breaking change to the API, albeit a very minor one: the existing
connectable value was a boolean and True now becomes 0x00, False becomes
0x02.
Documentation is updated and a test added.
Fixes #5738.
2020-03-09 20:45:03 -04:00
|
|
|
|
|
|
|
print("gap_advertise(20_000, connectable=True)")
|
2023-03-08 00:44:43 -05:00
|
|
|
ble.gap_advertise(20_000, adv_data, connectable=True)
|
|
|
|
time.sleep_ms(ADV_TIME_MS)
|
extmod/modbluetooth: Change scan result's "connectable" to "adv_type".
This commit changes the BLE _IRQ_SCAN_RESULT data from:
addr_type, addr, connectable, rssi, adv_data
to:
addr_type, addr, adv_type, rssi, adv_data
This allows _IRQ_SCAN_RESULT to handle all scan result types (not just
connectable and non-connectable passive scans), and to distinguish between
them using adv_type which is an integer taking values 0x00-0x04 per the BT
specification.
This is a breaking change to the API, albeit a very minor one: the existing
connectable value was a boolean and True now becomes 0x00, False becomes
0x02.
Documentation is updated and a test added.
Fixes #5738.
2020-03-09 20:45:03 -04:00
|
|
|
|
2020-03-04 21:34:46 -05:00
|
|
|
print("gap_advertise(None)")
|
|
|
|
ble.gap_advertise(None)
|
extmod/modbluetooth: Change scan result's "connectable" to "adv_type".
This commit changes the BLE _IRQ_SCAN_RESULT data from:
addr_type, addr, connectable, rssi, adv_data
to:
addr_type, addr, adv_type, rssi, adv_data
This allows _IRQ_SCAN_RESULT to handle all scan result types (not just
connectable and non-connectable passive scans), and to distinguish between
them using adv_type which is an integer taking values 0x00-0x04 per the BT
specification.
This is a breaking change to the API, albeit a very minor one: the existing
connectable value was a boolean and True now becomes 0x00, False becomes
0x02.
Documentation is updated and a test added.
Fixes #5738.
2020-03-09 20:45:03 -04:00
|
|
|
|
2020-03-04 21:34:46 -05:00
|
|
|
ble.active(0)
|
|
|
|
|
|
|
|
|
|
|
|
def instance1():
|
|
|
|
multitest.next()
|
|
|
|
finished = False
|
2023-03-08 00:44:43 -05:00
|
|
|
matched_adv_types = {}
|
|
|
|
matched_adv_data = None
|
2020-03-04 21:34:46 -05:00
|
|
|
|
|
|
|
def irq(ev, data):
|
2023-03-08 00:44:43 -05:00
|
|
|
nonlocal finished, matched_adv_types, matched_adv_data
|
2020-03-04 21:34:46 -05:00
|
|
|
if ev == _IRQ_SCAN_RESULT:
|
2023-03-08 00:44:43 -05:00
|
|
|
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)
|
extmod/modbluetooth: Change scan result's "connectable" to "adv_type".
This commit changes the BLE _IRQ_SCAN_RESULT data from:
addr_type, addr, connectable, rssi, adv_data
to:
addr_type, addr, adv_type, rssi, adv_data
This allows _IRQ_SCAN_RESULT to handle all scan result types (not just
connectable and non-connectable passive scans), and to distinguish between
them using adv_type which is an integer taking values 0x00-0x04 per the BT
specification.
This is a breaking change to the API, albeit a very minor one: the existing
connectable value was a boolean and True now becomes 0x00, False becomes
0x02.
Documentation is updated and a test added.
Fixes #5738.
2020-03-09 20:45:03 -04:00
|
|
|
else:
|
2023-03-08 00:44:43 -05:00
|
|
|
if adv_data != matched_adv_data:
|
|
|
|
matched_adv_data = b"MISMATCH"
|
2020-05-13 00:35:32 -04:00
|
|
|
elif ev == _IRQ_SCAN_DONE:
|
2020-03-04 21:34:46 -05:00
|
|
|
finished = True
|
|
|
|
|
2020-11-03 01:41:28 -05:00
|
|
|
try:
|
|
|
|
ble.config(rxbuf=2000)
|
|
|
|
except:
|
|
|
|
pass
|
2020-03-04 21:34:46 -05:00
|
|
|
ble.irq(irq)
|
2023-03-08 00:44:43 -05:00
|
|
|
ble.gap_scan(2 * ADV_TIME_MS, 30000, 30000)
|
2020-03-04 21:34:46 -05:00
|
|
|
while not finished:
|
|
|
|
machine.idle()
|
|
|
|
ble.active(0)
|
2023-03-08 00:44:43 -05:00
|
|
|
print("adv_types:", sorted(matched_adv_types))
|
|
|
|
print("adv_data:", matched_adv_data)
|
2020-03-04 21:34:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
ble = bluetooth.BLE()
|
|
|
|
ble.active(1)
|