aioble how do i list all services and characteristics #18174
-
|
I have been using aioble to connect a client to an existing peripheral - all working well when I knew what the peripherals UUIDs were. import sys
import uasyncio as asyncio
import aioble
import bluetooth
import structThen I find the device and connect to it and look for services: async def discover_services_and_characteristics(connection):
print("Discovering services...")
#service = await connection.service(_SERVICE_UUID) # specific service
services = await connection.services()
for service in range(services):
if service.uuid() == _SERVICE_UUID:
print("Found target Service")
characteristics = await service.characteristics()
for char in characteristics:
print(f"characteristic {char}")this gave me ~ ClientDiscovery object isn't iterable characteristics = await aioble.discover_characteristics(connection)which was found in a google AI search result - i have found that none of the AI answers work so if anyone can point me in the right direction or a sample piece of code to find the services and characteristics from a connection without knowing what the UUIDs are for services and Characteristics Colin C |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Please see the comment of An example code: import asyncio
import aioble
import bluetooth
_TARGET_NAME = "mpy-nus"
_SERVICE_UUID = bluetooth.UUID("6e400001-b5a3-f393-e0a9-e50e24dcca9e")
async def discover_device(target_name):
# Scan for 20 seconds, in active mode, with very low interval/window (to maximise detection rate).
async with aioble.scan(duration_ms=20_000, interval_us=30000, window_us=30000, active=True) as scanner:
async for result in scanner:
# See if it matches target_name.
if (name := result.name()) is not None and target_name in name:
print(f"Found target device: {name} - {result.device}")
return result.device
print(f"Device with name {target_name} not found.")
return None
async def run():
device = await discover_device(_TARGET_NAME)
if not device:
return
connection = await device.connect(
timeout_ms=60_000,
scan_duration_ms=5_000, min_conn_interval_us=7_500, max_conn_interval_us=7_500)
async with connection:
print(f"Connected to {device}")
found = False
async for service in connection.services():
if service.uuid == _SERVICE_UUID:
print("Found target Service")
found = True
# After the loop completion.
if found:
service = await connection.service(_SERVICE_UUID)
async for char in service.characteristics():
print(f"characteristic {char}")
asyncio.run(run()) |
Beta Was this translation helpful? Give feedback.
Please see the comment of
def services()in device.pyAn example code: