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

bleak.exc.BleakError: failed to discover services, device disconnected #1489

Closed
bisudw opened this issue Jan 4, 2024 · 1 comment · Fixed by #1544
Closed

bleak.exc.BleakError: failed to discover services, device disconnected #1489

bisudw opened this issue Jan 4, 2024 · 1 comment · Fixed by #1544

Comments

@bisudw
Copy link

bisudw commented Jan 4, 2024

  • bleak version: 0.21.1
  • Python version: 3.10
  • Operating System: Ubuntu
  • BlueZ version (bluetoothctl -v) in case of Linux: 5.64

Description

I have a Vernier Caliper from TESA which has a BLE module and it gives data from that BLE Module. I am trying to read the data from the BLE Module. I am ble to read it in My Phone using LightBlue App (a ble scanner app).

What I Did

When I am rnning the Service_Explorer I am able to discover the device and the advertised service UUID is also visible. Its the same service UUID that i get in my phone app.

It looks like this:

E0:4B:16:4A:55:EA: TLC-BLE
--------------------------
AdvertisementData(local_name='TLC-BLE', manufacturer_data={1341: b'\x0800530322\x069E0445'}, service_uuids=['6e400001-b5a3-f393-e0a9-e50e24dcca9e'], tx_power=4, rssi=-49)

But when I am trying to connect to the same BLE device I am getting error which says:

bleak.exc.BleakError: failed to discover services, device disconnected

here is the code which I am using to trying to connect:

import asyncio
from bleak import BleakClient


async def main():
    async with BleakClient("E0:4B:16:4A:55:EA") as client:
        # Read a characteristic, etc.
        print("Connected")

    # Device will disconnect when block exits.
    print("disconnect")

# Using asyncio.run() is important to ensure that device disconnects on
# KeyboardInterrupt or other unhandled exception.
asyncio.run(main())

Logs

Traceback (most recent call last):
  File "/home/biswa/PycharmProjects/BLEScanner/main.py", line 15, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/home/biswa/PycharmProjects/BLEScanner/main.py", line 6, in main
    async with BleakClient("E0:4B:16:4A:55:EA") as client:
  File "/home/biswa/PycharmProjects/BLEScanner/.venv/lib/python3.10/site-packages/bleak/__init__.py", line 565, in __aenter__
    await self.connect()
  File "/home/biswa/PycharmProjects/BLEScanner/.venv/lib/python3.10/site-packages/bleak/__init__.py", line 605, in connect
    return await self._backend.connect(**kwargs)
  File "/home/biswa/PycharmProjects/BLEScanner/.venv/lib/python3.10/site-packages/bleak/backends/bluezdbus/client.py", line 268, in connect
    await self.get_services(
  File "/home/biswa/PycharmProjects/BLEScanner/.venv/lib/python3.10/site-packages/bleak/backends/bluezdbus/client.py", line 656, in get_services
    self.services = await manager.get_services(
  File "/home/biswa/PycharmProjects/BLEScanner/.venv/lib/python3.10/site-packages/bleak/backends/bluezdbus/manager.py", line 644, in get_services
    await self._wait_for_services_discovery(device_path)
  File "/home/biswa/PycharmProjects/BLEScanner/.venv/lib/python3.10/site-packages/bleak/backends/bluezdbus/manager.py", line 779, in _wait_for_services_discovery
    raise BleakError("failed to discover services, device disconnected")
bleak.exc.BleakError: failed to discover services, device disconnected

@dlech
Copy link
Collaborator

dlech commented Jan 4, 2024

Can you run the same program with debug logging enabled? If that doesn't provide enough info you may need to log Bluetooth packets as well.

https://bleak.readthedocs.io/en/latest/troubleshooting.html

arthur-proglove added a commit to arthur-proglove/bleak that referenced this issue Apr 25, 2024
The _wait_removed method registers the "InterfacesRemoved" callback on
the adapter path of the device without checking which object was
removed when called.
This means that any removed interface while a connection is
being established can cause the _wait_removed to complete and cancel
the connection[1].

This commit simply checks that the callback is for the proper device
object path.
[1] hbldh#1489
arthur-proglove added a commit to arthur-proglove/bleak that referenced this issue Apr 25, 2024
The _wait_removed method registers the "InterfacesRemoved" callback on
the adapter path of the device without checking which object was
removed when called.
This means that any removed interface while a connection is
being established can cause the _wait_removed to complete and cancel
the connection[1].

This commit simply checks that the callback is for the proper device
object path.
[1] hbldh#1489
arthur-proglove added a commit to arthur-proglove/bleak that referenced this issue Apr 25, 2024
The _wait_removed method registers the "InterfacesRemoved" callback on
the adapter path of the device without checking which object was
removed when called.
This means that any removed interface while a connection is
being established can cause the _wait_removed to complete and cancel
the connection.

This commit simply checks that the callback is for the proper device
object path.

Fixes hbldh#1489
arthur-proglove added a commit to arthur-proglove/bleak that referenced this issue Apr 25, 2024
The _wait_removed method registers the "InterfacesRemoved" callback on
the adapter path of the device without checking which object was
removed when called.
This means that any removed interface while a connection is
being established can cause the _wait_removed to complete and cancel
the connection.

This commit simply checks that the callback is for the proper device
object path.

Fixes hbldh#1489
arthur-proglove added a commit to arthur-proglove/bleak that referenced this issue Apr 25, 2024
The _wait_removed method registers the "InterfacesRemoved" callback on
the adapter path of the device without checking which object was
removed when called.
This means that any removed interface while a connection is
being established can cause the _wait_removed to complete and cancel
the connection.

This commit simply checks that the callback is for the proper device
object path.

Fixes hbldh#1489
arthur-proglove added a commit to workaroundgmbh/bleak that referenced this issue Apr 25, 2024
The _wait_removed method registers the "InterfacesRemoved" callback on
the adapter path of the device without checking which object was
removed when called.
This means that any removed interface while a connection is
being established can cause the _wait_removed to complete and cancel
the connection.

This commit simply checks that the callback is for the proper device
object path.

Fixes hbldh#1489
dlech pushed a commit that referenced this issue Apr 28, 2024
* Fix _wait_removed completion on invalid object path

The _wait_removed method registers the "InterfacesRemoved" callback on
the adapter path of the device without checking which object was
removed when called.
This means that any removed interface while a connection is
being established can cause the _wait_removed to complete and cancel
the connection.

This commit simply checks that the callback is for the proper device
object path.

Fixes #1489
@dlech dlech mentioned this issue May 4, 2024
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