Skip to content

Commit 63736c1

Browse files
committed
aioble/multitests: Add test for advertising and scanning services.
This tests both encoding and decoding multiple 16-bit and 32-bit services within the one advertising field. Signed-off-by: Damien George <[email protected]>
1 parent 46e243c commit 63736c1

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Test advertising multiple services, and scanning them.
2+
3+
import sys
4+
5+
# ruff: noqa: E402
6+
sys.path.append("")
7+
8+
import asyncio
9+
import aioble
10+
import bluetooth
11+
12+
TIMEOUT_MS = 5000
13+
14+
_SERVICE_16_A = bluetooth.UUID(0x180F) # Battery Service
15+
_SERVICE_16_B = bluetooth.UUID(0x181A) # Environmental Sensing Service
16+
_SERVICE_32_A = bluetooth.UUID("AB12") # random
17+
_SERVICE_32_B = bluetooth.UUID("CD34") # random
18+
19+
20+
# Acting in peripheral role (advertising).
21+
async def instance0_task():
22+
multitest.globals(BDADDR=aioble.config("mac"))
23+
multitest.next()
24+
25+
# Advertise, and wait for central to connect to us.
26+
print("advertise")
27+
async with await aioble.advertise(
28+
20_000,
29+
name="MPY",
30+
services=[_SERVICE_16_A, _SERVICE_16_B, _SERVICE_32_A, _SERVICE_32_B],
31+
timeout_ms=TIMEOUT_MS,
32+
) as connection:
33+
print("connected")
34+
await connection.disconnected()
35+
print("disconnected")
36+
37+
38+
def instance0():
39+
try:
40+
asyncio.run(instance0_task())
41+
finally:
42+
aioble.stop()
43+
44+
45+
# Acting in central role (scanning).
46+
async def instance1_task():
47+
multitest.next()
48+
49+
wanted_device = aioble.Device(*BDADDR)
50+
51+
# Scan for the wanted device/peripheral and print its advertised services.
52+
async with aioble.scan(5000, interval_us=30000, window_us=30000, active=True) as scanner:
53+
async for result in scanner:
54+
if result.device == wanted_device:
55+
services = list(result.services())
56+
if services:
57+
print(services)
58+
break
59+
60+
# Connect to peripheral and then disconnect.
61+
print("connect")
62+
device = aioble.Device(*BDADDR)
63+
async with await device.connect(timeout_ms=TIMEOUT_MS):
64+
print("disconnect")
65+
66+
67+
def instance1():
68+
try:
69+
asyncio.run(instance1_task())
70+
finally:
71+
aioble.stop()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--- instance0 ---
2+
advertise
3+
connected
4+
disconnected
5+
--- instance1 ---
6+
[UUID(0x180f), UUID(0x181a), UUID(0x32314241), UUID(0x34334443)]
7+
connect
8+
disconnect

0 commit comments

Comments
 (0)