bleak icon indicating copy to clipboard operation
bleak copied to clipboard

Better error message for write without response with too large of data

Open dlech opened this issue 4 years ago • 2 comments

I'm not super familiar with the inner workings of Bleak, but I am getting a OSError: The parameter is incorrect after calling await client.write_gatt_char(MY_UUID, msg). Is this related to this issue, or am I doing something wrong?

I have tried it on v0.13, v0.14, and the develop branch (v0.15.1 I believe)

Context:

async def run(address, name):
    print("Connecting to {} ({})...".format(name, address))
    async with BleakClient(address) as client:
        print("Connected")
        msg = initMsg()
        await client.write_gatt_char(MY_UUID, msg)
        print("Message sent")

...

loop = asyncio.get_event_loop()
loop.run_until_complete(
    run(address, name))

Originally posted by @kaedenbrinkman in https://github.com/hbldh/bleak/issues/705#issuecomment-1012664228

dlech avatar Jan 14 '22 02:01 dlech

We solved the issue in an off topic discussion in #705, but we could do a better job of giving a more useful error message.

The problem was that the data was too large for a write without response.

Windows gives the cryptic OSError: [WinError -2147024809] The parameter is incorrect in this case. I imagine other OSes give their own similar cryptic errors. However, the behavior is the same on all platforms. If the size of the data is larger than mtu_size - 3, then the operation will fail. So we could check for this before even attempting the write and raise an exception.

The only problem is that BlueZ doesn't currently have a way to get the proper mtu_size. So we would probably have to skip the check on BlueZ in order to not break existing code.

dlech avatar Jan 14 '22 02:01 dlech

I also opened a related issue #738 for a better way to get this limit at runtime to perhaps avoid the error in the first place.

dlech avatar Jan 14 '22 03:01 dlech