Establish and maintain connection, no examples. #856
Replies: 2 comments 2 replies
-
Beta Was this translation helpful? Give feedback.
-
Connecting is implicit when using I like using the "observer" pattern, so I might do something like this: import asyncio
from enum import Enum, auto
from typing import Any, Callable, NamedTuple
from bleak import BleakClient
class EventType(Enum):
CONNECTED = auto()
CHAR_XXXX = auto()
class Event(NamedTuple):
type: EventType
payload: Any
class Observer:
def on_next(self, event: Event) -> None:
...
def on_complete(self) -> None:
...
def on_error(self, exc: Exception) -> None:
...
async def monitor_connection(
client: BleakClient, observer: Observer, disconnect_event: asyncio.Event
):
"""
Connects to a BLE device and attaches event handlers, then waits for disconnection.
Args:
client: The BLE device.
observer: The event observer than handles events.
disconnect_event: An event that triggers disconnection.
"""
try:
def on_disconnect(_):
# this handles the case where the remote device or the OS
# triggers the disconnection
disconnect_event.set()
client.set_disconnected_callback(on_disconnect)
def on_notify_xxxx(_, data):
# TODO: decode data
observer.on_next(Event(EventType.CHAR_XXXX, data))
async with client:
await client.start_notify("XXXX", on_notify_xxxx)
observer.on_next(Event(EventType.CONNECTED, None))
await disconnect_event.wait()
# this serves as the DISCONNECTED event
observer.on_complete()
except Exception as exc:
observer.on_error(exc)
def subscribe(address: str, observer: Observer) -> Callable[[], None]:
"""
Scans for and connects to a device with the given address.
Since this scans, be sure no other scans are in progress before calling
this method.
Args:
address: The Bluetooth address (UUID on macOS) of the device to be connected.
observer: An Observer that will handle callback for events.
Returns:
An unsubscribe function that, when called, will disconnect the device.
"""
client = BleakClient(address)
disconnect_event = asyncio.Event()
asyncio.ensure_future(monitor_connection(client, observer, disconnect_event))
# this probably doesn't strictly follow observable unsubscribe semantics
# since the observer will still continue to receive events until
# disconnection is complete
return disconnect_event.set In a GUI, clicking the connect button would call the subscribe function. Then then the GUI could indicate connection in progress until either |
Beta Was this translation helpful? Give feedback.
-
I am trying to establish a connection and maintain it and still be able to perform other things like notify. I am building off the examples but they all seem to do something and then disconnect. I am an embedded developer and only recently have begun to use python so do forgive my ignorance.
I read here where you guys mention that we have to call the
connect
function but it takes no arguments or does not really give much info other than a discover must have been ran before so I assume it will connect to that device but how do I go about this.. This is a GUI based application so I have a button to discover services like your example:And another button to "connect" or maintain the connection?
I assume the connection does not persist after the service discovery above since I am still able to connect to the device with another device. However when I run the code to connect it says its
already connected
.However I am able to discover and connect to the device from other devices so it cant possibly be
already connected
to bleak.I am just not sure how I can leave the connection established and then keep running code from other examples like get
notifications
with NO timeout until the user presses aDisconnect button
.. I can add a huge timeout to the notifications examples but then I fear doing other things like reading a Characteristic from GATT will result in analready connected
error.Beta Was this translation helpful? Give feedback.
All reactions