spacecheese / bluez_peripheral

A library for building BLE peripherals using GATT and bluez
MIT License
38 stars 8 forks source link

Async characteristic #15

Closed Martwall closed 1 year ago

Martwall commented 1 year ago

Hello, Thank you for the work with this library! Helps out a lot working with bluez. Would just like to ask if there is any way of defining an async characteristic? Example:

@characteristic(uuid, flag)
async def my_async_read_characteristic(self, options)
    some_value = await some_coroutine()
    return bytes(some_value, 'UTF-8')

Or perhaps it should somehow be handled using signals instead?

spacecheese commented 1 year ago

There isn't currently a way to do this however I think this should probably be quite easy to implement.

Martwall commented 1 year ago

Alright I understand. Let me know if you need any more information. Unfortunately I'm not sure how I would implement it myself but perhaps I can contribute in some other way if needed. Currently trying to implement it using signals where the characteristic would return a reference to a result object that is later referenced in a signal to the client that has subscribed for notifications. But using async characteristic directly would probably be easier.

spacecheese commented 1 year ago

I've implemented this on the async_attributes branch but I'm not currently set up for testing so if you get the chance any feedback is much appreciated (pip install -e git+https://github.com/spacecheese/bluez_peripheral.git@async-attributes#egg=bluez_peripheral to install from the repo, just make sure to replace it with a stable version afterwards)

Martwall commented 1 year ago

Great, I'll try and do some testing in the next few days.

Martwall commented 1 year ago

I did some testing with read, write and notify characteristics and it all worked out just fine. Does seem that after a couple of seconds (in my case >4) there is no response on the receiving end (on read characteristic at least) but that would be up to each implementation to look out for and handle and not handled in bluez-peripheral. Something related with the Bluetooth protocol perhaps. Could handle long running tasks with notifications instead I suppose.

I did have an issue installing per the instructions with -e as there was no setup.pybut not a python expert so probably not something that needs fixing. Did add setup.py with

from setuptools import setup

if __name == '__main__':
  setup()

and I could then install.

Thanks for the fix!

spacecheese commented 1 year ago

Ah yeah sorry didn't know that I'll add a setup.py since that seems quite useful. I'll merge and release this once I've had some time to update the unit tests.

Martwall commented 1 year ago

Sounds good, thanks!


From: Space Cheese @.> Sent: Monday, December 19, 2022 5:26:32 PM To: spacecheese/bluez_peripheral @.> Cc: Martwall @.>; Author @.> Subject: Re: [spacecheese/bluez_peripheral] Async characteristic (Issue #15)

Ah yeah sorry didn't know that I'll add a setup.py since that seems quite useful. I'll merge and release this once I've had some time to update the unit tests.

— Reply to this email directly, view it on GitHubhttps://github.com/spacecheese/bluez_peripheral/issues/15#issuecomment-1357921665, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AIBOXZ3R2MEGKGVJY46GZRDWOCEDRANCNFSM6AAAAAASVISNJA. You are receiving this because you authored the thread.Message ID: @.***>

tilersmyth commented 1 year ago

Looking forward to this async functionality as well. Doing some testing with the async_attributes branch but running into the scenario @Martwall mentioned above (re read characteristics) but more severe.

If I run the following characteristic I get no response:

  @characteristic(uuid, CharFlags.READ)
  async def on_read_request(self, options):
        await asyncio.sleep(1)
        return bytes('anything', "utf-8")

But if I run it with await asyncio.sleep(.5) it works.

I'm communicating with react-native-ble-plx which I've used with bleno for a while with no similar issues. So I don't think it has to do with the receiving side but I could be wrong.

spacecheese commented 1 year ago

I think this is probably protocol related. I can't see any obvious differences with the Bleno characteristic code however it is using a slightly different API and bluez is very inconsistent.