hbldh / bleak

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

Exception trying to discover services. #172

Closed whoopsmith closed 2 years ago

whoopsmith commented 4 years ago

Description

Trying to learn how to use this project by playing around. I'm scanning for devices and trying to discover services.

I have combined the discover example with the service_explorer example. I discover all the devices and then feed each of those to the service_explorer code.

Sometimes I get a list of services but sometimes get the exception below. Is this expected?

What I Did

Scanning: ED:E4:C8:1D:86:45 
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/bleak/backends/bluezdbus/client.py", line 144, in connect
    ).asFuture(self.loop)
txdbus.error.RemoteError: org.bluez.Error.Failed: Software caused connection abort

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./get_whoop.py", line 80, in <module>
    loop.run_until_complete(service_explorer(address, loop, True))
  File "/usr/lib/python3.6/asyncio/base_events.py", line 484, in run_until_complete
    return future.result()
  File "./get_whoop.py", line 40, in service_explorer
    async with BleakClient(address, loop=loop) as client:
  File "/usr/lib/python3.6/asyncio/coroutines.py", line 129, in throw
    return self.gen.throw(type, value, traceback)
  File "/usr/local/lib/python3.6/dist-packages/bleak/backends/client.py", line 44, in __aenter__
    await self.connect()
  File "/usr/lib/python3.6/asyncio/coroutines.py", line 129, in throw
    return self.gen.throw(type, value, traceback)
  File "/usr/local/lib/python3.6/dist-packages/bleak/backends/bluezdbus/client.py", line 146, in connect
    raise BleakError(str(e))
bleak.exc.BleakError: org.bluez.Error.Failed: Software caused connection abort
hbldh commented 4 years ago

It is not expected and this is the first time I have seen this issue.

I will not look at this in the foreseeable future I am afraid. I am swamped with other (paid) work and cannot guarantee doing anything with this project for quite some time. I will still address PRs to develop branch occasionally, so if you desire these changes you have to do the legwork yourselves.

whoopsmith commented 4 years ago

Great. Thank you for the response. I don't mind digging in and figuring out whats going on. That will be a great way to learn more about how it works.

Now that I know its not expected I'll drill down and see what I can find. Like you my day job calls me away but the device(s) I'm trying to connect to are part of my day job so I can justify some paid time to investigate.

corvis commented 4 years ago

Hey @whoopsmith,

Have you solved your issue? It seems I have the similar one - from time to time when I call connect method I got org.bluez.Error.Failed: Software caused connection abort error. And this happens really randomly.

I implemented retry logic which tries to reconnect again and again when this happens. Sometimes it connects from the first attempt quickly, sometimes it takes 2-3 attempts and sometimes 7-8. Basically this makes my software pretty unstable and super slow.

So any insight on the root cause will be highly appertained.

hbldh commented 4 years ago

There was quite a long discussion about this in #94. See if anything there helps.

This repo also had issues with this some years ago. The accepted solution seems to be to restart the bluetooth adapter...

ArthurKun21 commented 4 years ago

Hi @whoopsmith and @corvis did you resolved this issue? I am also experiencing this issue at raspbian and my bluez is already at 5.50

corvis commented 4 years ago

@RulerKun unfoirtunately no. It still happens randomly, for some people (combination of hardware and software) it happens more often then for others. The recent example is here: https://github.com/corvis/prana_rc/issues/1#issuecomment-680928556 the guy has raspbian and uses embedded rpi adaptor.

hbldh commented 4 years ago

I have no news for you regarding this. It might be alleviated if #236 is worked on. It might be due to txdbus and how it handles the DBus connections.

dlech commented 2 years ago

There are newer issues where people have reported the same "Software caused connection abort" error, so let's close this one as a duplicate.