Skip to content

Conversation

aveao
Copy link

@aveao aveao commented Mar 11, 2023

BufferedCharacteristic is an undocumented characteristic class that merely calls bluetooth.BLE.gatts_set_buffer, allowing it to extend the internal buffer, which is useful for longer writes to successfully happen.

Oddly enough, BufferedCharacteristic currently defaults to read-only characteristics (see read=True), and does not allow specifying other characteristic types.

In my own personal testing, I had issues writing data longer than 20 bytes to a characteristic due to this buffer size issue (see here for more info). Calling gatts_set_buffer with a longer max_len fixes it, but this is not currently exposed to the user in any way through aioble (there's a TODO to allow user to pass in the BLE object).

I tried two different approaches, both porting this _register function over to regular Characteristic, and extending BufferedCharacteristic (this PR). Both function in my case, but doing former defeats the point of BufferedCharacteristic and as such PRing the latter felt like it made more sense.

This PR, as such, extends BufferedCharacteristic in a backwards-compatible manner (hence the ugly read=True+read=read) allowing specifying different flags for the characteristic, and hopefully makes it more useful in the process.

If a different approach is desired (i.e. not making this backwards compatible, changing Characteristic, exposing BLE object, etc) I can also change the PR to do that.

(Tested and functioning on MicroPython v1.19.1-963-g668a7bd28 of 2023-03-10 on ESP32.)

@mohnazemi
Copy link

mohnazemi commented Aug 12, 2023

@jimmo May I ask if there a reason why this PR is not merged yet ? I also expected BufferedCharacteristic class to support both read and write characteristic types

@mohnazemi
Copy link

mohnazemi commented Aug 13, 2023

@aveao I think keeping it backwards compatible means 'read-only by default' will be hidden from any new user. So I am using it like below:

class BufferedCharacteristic(Characteristic):
    def __init__(self, *args, max_len=50, append=True, **kwargs):
        super().__init__(*args, **kwargs)
        self._max_len = max_len
        self._append = append

    def _register(self, value_handle):
        super()._register(value_handle)
        ble.gatts_set_buffer(value_handle, self._max_len, self._append)

@jimmo
Copy link
Member

jimmo commented Sep 14, 2023

Sorry about the delay on this one -- see #729 for an updated version with tests.

@jimmo jimmo closed this Sep 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants