aioble how do i list all services and characteristics #18174
Unanswered
colinc3e
asked this question in
Using MicroPython
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.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have been using aioble to connect a client to an existing peripheral - all working well when I knew what the peripherals UUIDs were.
Then I thought can we just list services and/or characteristics and pick the ones we want ( I have done this with C/C++ bluetooth before.
I should say now that I and using a pico w was the client. now I am using thonny and micro python so moved to aioble as it seemed easier to use than the C/C++ libraries
so I include
Then I find the device and connect to it and look for services:
this gave me ~ ClientDiscovery object isn't iterable
I have difficulty finding details of aioble methods but the 'service.characteristics call was from a google search (AI possibly)
I also tried
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.
All reactions