|
| 1 | +# Test descriptor discovery/read/write from both GATTS and GATTC. |
| 2 | + |
| 3 | +import sys |
| 4 | + |
| 5 | +sys.path.append("") |
| 6 | + |
| 7 | +from micropython import const |
| 8 | +import time, machine |
| 9 | + |
| 10 | +import uasyncio as asyncio |
| 11 | +import aioble |
| 12 | +import bluetooth |
| 13 | + |
| 14 | +TIMEOUT_MS = 5000 |
| 15 | + |
| 16 | +SERVICE_UUID = bluetooth.UUID("A5A5A5A5-FFFF-9999-1111-5A5A5A5A5A5A") |
| 17 | +CHAR1_UUID = bluetooth.UUID("00000000-1111-2222-3333-444444444444") |
| 18 | +CHAR1_DESC1_UUID = bluetooth.UUID("00000000-1111-2222-3333-444444444445") |
| 19 | +CHAR1_DESC2_UUID = bluetooth.UUID("00000000-1111-2222-3333-444444444446") |
| 20 | +CHAR2_UUID = bluetooth.UUID("00000000-1111-2222-3333-555555555555") |
| 21 | +CHAR3_UUID = bluetooth.UUID("00000000-1111-2222-3333-666666666666") |
| 22 | +CHAR3_DESC1_UUID = bluetooth.UUID("00000000-1111-2222-3333-666666666667") |
| 23 | + |
| 24 | + |
| 25 | +# Acting in peripheral role. |
| 26 | +async def instance0_task(): |
| 27 | + service = aioble.Service(SERVICE_UUID) |
| 28 | + char1 = aioble.Characteristic( |
| 29 | + service, CHAR1_UUID, read=True, write=True, notify=True, indicate=True |
| 30 | + ) |
| 31 | + char1_desc1 = aioble.Descriptor(char1, CHAR1_DESC1_UUID, read=True) |
| 32 | + char1_desc1.write("char1_desc1") |
| 33 | + char1_desc2 = aioble.Descriptor(char1, CHAR1_DESC2_UUID, read=True, write=True) |
| 34 | + char1_desc2.write("char1_desc2") |
| 35 | + char2 = aioble.Characteristic( |
| 36 | + service, CHAR2_UUID, read=True, write=True, notify=True, indicate=True |
| 37 | + ) |
| 38 | + char3 = aioble.Characteristic( |
| 39 | + service, CHAR3_UUID, read=True, write=True, notify=True, indicate=True |
| 40 | + ) |
| 41 | + char3_desc1 = aioble.Descriptor(char3, CHAR3_DESC1_UUID, read=True) |
| 42 | + char3_desc1.write("char3_desc1") |
| 43 | + aioble.register_services(service) |
| 44 | + |
| 45 | + multitest.globals(BDADDR=aioble.config("mac")) |
| 46 | + multitest.next() |
| 47 | + |
| 48 | + # Wait for central to connect to us. |
| 49 | + print("advertise") |
| 50 | + connection = await aioble.advertise( |
| 51 | + 20_000, adv_data=b"\x02\x01\x06\x04\xffMPY", timeout_ms=TIMEOUT_MS |
| 52 | + ) |
| 53 | + print("connected") |
| 54 | + |
| 55 | + await char1_desc2.written() |
| 56 | + print("written", char1_desc2.read()) |
| 57 | + |
| 58 | + # Wait for the central to disconnect. |
| 59 | + await connection.disconnected(timeout_ms=TIMEOUT_MS) |
| 60 | + print("disconnected") |
| 61 | + |
| 62 | + |
| 63 | +def instance0(): |
| 64 | + try: |
| 65 | + asyncio.run(instance0_task()) |
| 66 | + finally: |
| 67 | + aioble.stop() |
| 68 | + |
| 69 | + |
| 70 | +# Acting in central role. |
| 71 | +async def instance1_task(): |
| 72 | + multitest.next() |
| 73 | + |
| 74 | + # Connect to peripheral and then disconnect. |
| 75 | + print("connect") |
| 76 | + device = aioble.Device(*BDADDR) |
| 77 | + connection = await device.connect(timeout_ms=TIMEOUT_MS) |
| 78 | + |
| 79 | + service = await connection.service(SERVICE_UUID) |
| 80 | + print("service", service.uuid) |
| 81 | + |
| 82 | + # Discover characteristics. |
| 83 | + uuids = [] |
| 84 | + async for char in service.characteristics(): |
| 85 | + uuids.append(char.uuid) |
| 86 | + print("found", sorted(uuids)) |
| 87 | + |
| 88 | + char1 = await service.characteristic(CHAR1_UUID) |
| 89 | + print("char1", char1.uuid) |
| 90 | + |
| 91 | + uuids = [] |
| 92 | + async for desc in char1.descriptors(): |
| 93 | + uuids.append(desc.uuid) |
| 94 | + print("char1 descs", sorted(uuids)) |
| 95 | + |
| 96 | + char1_desc1 = await char1.descriptor(CHAR1_DESC1_UUID) |
| 97 | + print("char1 desc1", char1_desc1.uuid) |
| 98 | + print("read", await char1_desc1.read()) |
| 99 | + |
| 100 | + char1_desc2 = await char1.descriptor(CHAR1_DESC2_UUID) |
| 101 | + print("char1 desc2", char1_desc2.uuid) |
| 102 | + print("read", await char1_desc2.read()) |
| 103 | + print("write") |
| 104 | + await char1_desc2.write("update") |
| 105 | + |
| 106 | + char2 = await service.characteristic(CHAR2_UUID) |
| 107 | + print("char2", char2.uuid) |
| 108 | + |
| 109 | + uuids = [] |
| 110 | + async for desc in char2.descriptors(): |
| 111 | + uuids.append(desc.uuid) |
| 112 | + print("char2 descs", sorted(uuids)) |
| 113 | + |
| 114 | + char3 = await service.characteristic(CHAR3_UUID) |
| 115 | + print("char3", char3.uuid) |
| 116 | + |
| 117 | + uuids = [] |
| 118 | + async for desc in char3.descriptors(): |
| 119 | + uuids.append(desc.uuid) |
| 120 | + print("char3 descs", sorted(uuids)) |
| 121 | + |
| 122 | + char3_desc1 = await char3.descriptor(CHAR3_DESC1_UUID) |
| 123 | + print("char3 desc1", char3_desc1.uuid) |
| 124 | + print("read", await char3_desc1.read()) |
| 125 | + |
| 126 | + # Disconnect from peripheral. |
| 127 | + print("disconnect") |
| 128 | + await connection.disconnect(timeout_ms=TIMEOUT_MS) |
| 129 | + print("disconnected") |
| 130 | + |
| 131 | + |
| 132 | +def instance1(): |
| 133 | + try: |
| 134 | + asyncio.run(instance1_task()) |
| 135 | + finally: |
| 136 | + aioble.stop() |
0 commit comments