hbldh / bleak

A cross platform Bluetooth Low Energy Client for Python using asyncio
MIT License
1.73k stars 291 forks source link

Windows 11 - Error in write_gatt_descriptor #615

Closed theGENreel closed 3 years ago

theGENreel commented 3 years ago

Description

I'm trying to control my Mi Band 4. It seems that other people do not have this error. Therefore, I think that the problem is in the changes in the API. Perhaps the API is now returning int.

What I Did

I have a function that writes data to descriptor.

async def aut():
    await client.write_gatt_descriptor(_desc_auth.handle, b"\x01\x00")

And here write_gatt_descriptor throws AttributeError.

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\Skummer\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
    return future.result()
  File "<input>", line 2, in aut
  File "C:\Users\Skummer\Documents\Projects\Python\miband4\venv\lib\site-packages\bleak\backends\winrt\client.py", line 690, in write_gatt_descriptor
    if write_result.status == GattCommunicationStatus.SUCCESS:
AttributeError: 'int' object has no attribute 'status'

I have verified that descriptor.obj.write_value_async in code below returns 0 (backends/winrt/client.py on 686 line).

write_result.status = await descriptor.obj.write_value_async(
            CryptographicBuffer.create_from_byte_array(list(data))
        )

And if you remove ".status" in the code below, the error goes away (line 690).

 if write_result.status == GattCommunicationStatus.SUCCESS:
            logger.debug("Write Descriptor {0} : {1}".format(handle, data))

It looks like there are some changes in the Windows 11 API, so the lib is not working now.

hbldh commented 3 years ago

I do not think Bleak will support Windows 11 until it has been released for public use. I appreciate you raising the issue and making us aware of it, but solution to this will have to wait.

That said, you are free to delve deeper into the changes into the Bluetooth API of Windows 11 and make pull requests to fix the inevitably approaching problems. That would be much appreciated, since I am in no position to install the W11 preview on any of my systems!

dlech commented 3 years ago

I don't think this has anything to do with Win 11. It should be calling write_value_with_result_async() to return a GattWriteResult instead of write_value_async() which returns GattStatus (which is enum with base type of int).

dlech commented 3 years ago

@theGENreel can you please test #616?

theGENreel commented 3 years ago

@hbldh @dlech Thanks for answers. Checked at #616, write_gatt_descriptor works.

dlech commented 3 years ago

Great, thanks for testing.

hbldh commented 3 years ago

Ah, I see. I should stop making comments on issues without checking the code first...

Good work!