Skip to content

Enable notifications without going through the callback system? #1638

Discussion options

You must be logged in to vote

To resolve this: my workaround is to create an async context manager to manage the queue and callback:

class NotifyWrapper:
    def __init__(self, client):
        self.queue = asyncio.Queue()
        self.client = client

    async def __aenter__(self):
        async def callback(sender: BleakGATTCharacteristic, data: bytearray):
            await self.queue.put(data)
        await self.client.start_notify(uart_receive_uuid, callback)

        return self

    async def read(self):
        message = await self.queue.get()
        return message
    
    async def __aexit__(self, exc_type, exc, tb):
        await self.client.stop_notify(uart_receive_uuid) 

This is for a command line util…

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by JensRestemeier
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1637 on September 06, 2024 16:04.