Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows : Failed to connect to device: Could not get GATT characteristics #1291

Closed
pymenow opened this issue Apr 28, 2023 · 8 comments · Fixed by #1294
Closed

Windows : Failed to connect to device: Could not get GATT characteristics #1291

pymenow opened this issue Apr 28, 2023 · 8 comments · Fixed by #1294

Comments

@pymenow
Copy link

pymenow commented Apr 28, 2023

  • bleak version: 0.20.2
  • Python version: 3.9.13
  • Operating System: Windows 11 22621.1635
  • BlueZ version (bluetoothctl -v) in case of Linux:

Description

Failed to connect to device: Could not get GATT characteristics for <_bleak_winrt_Windows_Devices_Bluetooth_GenericAttributeProfile.GattDeviceService object at 0x00000206F9189750>: Access Denied

The same code works in Linux but issues seen in windows.

What I Did

Just attempting to connect to the BLE device . Below is the function I am using to connect.

async def omn_connect(address: str, reconnect_delay: int = 5) -> Optional[BleakClient]:
    async def on_disconnect(client, error):
        print(f"Device disconnected, error: {error}")

    try:
        print(f"Attempting to connect to device {address}...")
        client = BleakClient(address, disconnected_callback=lambda _: asyncio.ensure_future(on_disconnect(client, _)))
        await client.connect()
        print("Connected")
        
        return client
    except Exception as e:
        print(f"Failed to connect to device: {e}")
        return None


async def omn_disconnect(client: BleakClient):
    try:
        await client.disconnect()
        print("Disconnected successfully")
    except Exception as e:
        print(f"Disconnection failed: {e}")

Logs

Added logs below .
blelogs.txt

Also added the Wireshark logs :
GATT-Wireshark.zip

@pymenow
Copy link
Author

pymenow commented Apr 28, 2023

GATT-Wider.zip
Added extended logs for Wireshark where I pair with the device via Windows Bluetooth and then start the bleak script to connect before I run into the same issue.

@dlech
Copy link
Collaborator

dlech commented Apr 28, 2023

The program is not complete, so it is impossible to say what could be happening.

Please provide a full minimal reproducible test case.

@pymenow
Copy link
Author

pymenow commented Apr 28, 2023

Hello here is a fully reproducible test .

import asyncio
from bleak import BleakClient


async def discover_services_and_handles(device_address):
    async with BleakClient(device_address) as client:
        services = await client.get_services()

        for service in services:
            print(f"Service: {service.uuid}")
            for characteristic in service.characteristics:
                print(f"  Characteristic: {characteristic.uuid}, Handle: {characteristic.handle}")

device_address = "XX:XX:XX:XX:XX:XX"  # Replace with your device's address
asyncio.run(discover_services_and_handles(device_address))

@dlech
Copy link
Collaborator

dlech commented Apr 28, 2023

Can you please run with the following change so we can see which UUID is causing the problem?

diff --git a/bleak/backends/winrt/client.py b/bleak/backends/winrt/client.py
index 3431d0b..003b3b7 100644
--- a/bleak/backends/winrt/client.py
+++ b/bleak/backends/winrt/client.py
@@ -706,7 +706,7 @@ class BleakClientWinRT(BaseBleakClient):
                 characteristics: Sequence[GattCharacteristic] = _ensure_success(
                     await FutureLike(service.get_characteristics_async(*args)),
                     "characteristics",
-                    f"Could not get GATT characteristics for {service}",
+                    f"Could not get GATT characteristics for {service.uuid} ({service.attribute_handle})",
                 )
 
                 logger.debug("returned from get_characteristics_async")

@pymenow
Copy link
Author

pymenow commented Apr 29, 2023

Thanks - Here is the output -
bleak.exc.BleakError: Could not get GATT characteristics for 0000181e-0000-1000-8000-00805f9b34fb (68): Access Denied

On linux these are read correctly using the same code.

  - Service: 0000181e-0000-1000-8000-00805f9b34fb
    - Characteristic: 00002aa4-0000-1000-8000-00805f9b34fb
      - Handle: 69
      - Properties: ['read', 'write']
    - Characteristic: 00002aa5-0000-1000-8000-00805f9b34fb
      - Handle: 71
      - Properties: ['read', 'write']

dlech added a commit that referenced this issue Apr 29, 2023
We had a hard-coded list of services know to return an access denied
error when attempting to enumerate characteristics. However, since
we don't know Windows internals, the list was not exhaustive and it
could also change in the future.

Instead, we can just check for the access denied error and skip any
service that returns that error which should also handle additional
services we don't know about yet.

Fixes: #1291
@dlech
Copy link
Collaborator

dlech commented Apr 29, 2023

Thanks. Could you please test #1294 to see if it fixed the problem?

@pymenow
Copy link
Author

pymenow commented Apr 30, 2023

Thanks , was able to connect to the device successfully with this change.
Its likely since 0000181e is a bond management servcie - Windows wants to handle this and hence the restriction ?

@dlech
Copy link
Collaborator

dlech commented Apr 30, 2023

Great, thanks for testing! Yes, it seems Windows gives an error for services like this that are "owned" by the OS while other OSes just hide them completely.

dlech added a commit that referenced this issue Apr 30, 2023
We had a hard-coded list of services know to return an access denied
error when attempting to enumerate characteristics. However, since
we don't know Windows internals, the list was not exhaustive and it
could also change in the future.

Instead, we can just check for the access denied error and skip any
service that returns that error which should also handle additional
services we don't know about yet.

Fixes: #1291
@dlech dlech mentioned this issue Sep 2, 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 a pull request may close this issue.

2 participants