bleak
bleak copied to clipboard
Same script working only on some computers
- bleak version: 0.12.1
- Python version: 3.8.5 (or 3.9)
- Operating System: Windows 10
Description
I'm using bleak to connect a medical device (Heart Rate Monitoring mainly) in BLE to computers and receive the measurements.
I wrote a small script to do this and this worked on my computer. My model is a ASUS Vivobook 14 X405UA.
After this, I tried the script on another computer, this model is a Dell Inspiron 15 3505. On this computer the script worked well for 5 days after which it started to missbehave and then almost not work at all. I suspect a windows update changed something to this computer maybe related to the Bluetooth driver. I rebooted the computer and reinstalled visual studio, python, bleak from scratch and I'm still getting errors.
My issue is to understand why the script is not working anymore on the Dell.
What I Did
This is not exactly the script I'm using as I'm in fact reading from two characteristics and writing to another one but the structure in terms of Bleak and asyncio usage is the same, if needed I can provide a more complete code.
import asyncio
from bleak import BleakClient, BleakScanner
from bleak.exc import BleakError
# Variables
hr_uuid = "0aad7ea0-0d60-11e2-8e3c-0002a5d5c51b"
ADDRESS = "AA:AA:AA:AA:AA:AA"
def handler(sender, data):
"""Simple notification handler which prints the data received."""
print(data)
async def run(address):
device = await BleakScanner.find_device_by_address(ADDRESS, timeout=30.0)
if not device:
raise BleakError(f"A device with address {ADDRESS} could not be found.")
async with BleakClient(device, timeout=30.0) as client:
await client.start_notify(hr_uuid, handler)
await asyncio.sleep(10.0)
await client.stop_notify(hr_uuid)
await client.disconnect()
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(run(ADDRESS))
So on my computer this still works fine, I can connect to the device, receive notifications, read them and disconnect properly. On the dell computer I got mainly one error (I tried the script in pyzo and visual studio code) , here's the traceback in Pyzo :
Traceback (most recent call last):
File "C:\Users\Script.py", line 178, in <module>
loop.run_until_complete(run(ADDRESS))
File "C:\Program Files\pyzo\source\pyzo\pyzokernel\guiintegration.py", line 209, in stub_run_until_complete
return self.app._original_run_until_complete(*args)
File "c:\users\user\appdata\local\programs\python\python39\lib\asyncio\base_events.py", line 642, in run_until_complete
return future.result()
File "C:\Users\Script.py", line 134, in run
await client.start_notify(hr_uuid, handler)
File "c:\users\user\appdata\local\programs\python\python39\lib\site-packages\bleak\backends\winrt\client.py", line 773, in start_notify
status = await characteristic_obj.write_client_characteristic_configuration_descriptor_async(
RuntimeError
The device connects to the computer but has an issue with the notification function. So the error is in start notify, and particularly in the line 773 of winrt\client.py (I slightly modified the paths of the traceback but the important is the one mentioned before). I suppose this is linked to the windows wrt backend but I am not sure how to fix this.
Can you please try using the current develop
branch? At the very least, it should give a more informative error message.
pip install https://github.com/hbldh/bleak/archive/develop.zip
Thank you very much, the log I got was the following :
Traceback (most recent call last):
File "c:\Users\Company\Bitbucket\bleak\examples\script_test.py", line 34, in <module>
loop.run_until_complete(run(ADDRESS))
File "C:\Program Files\lib\asyncio\base_events.py", line 642, in run_until_complete
return future.result()
File "c:\Users\Company\Bitbucket\bleak\examples\script_test.py", line 26, in run
async with BleakClient(device, timeout=30.0) as client:
File "C:\Users\Company\AppData\Roaming\Python\Python39\site-packages\bleak\backends\client.py", line 61, in _aenter_
await self.connect()
File "C:\Users\Company\AppData\Roaming\Python\Python39\site-packages\bleak\backends\winrt\client.py", line 257, in connect
await self.get_services()
File "C:\Users\Company\AppData\Roaming\Python\Python39\site-packages\bleak\backends\winrt\client.py", line 425, in get_services
services: Sequence[GattDeviceService] = _ensure_success(
File "C:\Users\Company\AppData\Roaming\Python\Python39\site-packages\bleak\backends\winrt\client.py", line 101, in _ensure_success
raise BleakError(f"{fail_msg}: Unreachable")
bleak.exc.BleakError: Could not get GATT services: Unreachable
Once again, the script does work on other computers than the DELL.
I'm guessing the Dell has a different Bluetooth chip and/or a different Windows version/patches that could be triggering the problem. You could also research the "Unreachable" error in relation to Windows Bluetooth Low Energy and see if that gives any hints. And there is always logging Bluetooth packets to see if you can see a difference between the two computers. https://bleak.readthedocs.io/en/develop/troubleshooting.html#windows-10
Okay I'm going to check the unreachable error and troubleshoot. I'll tell you if anything comes up.
You could also research the "Unreachable" error in relation to Windows Bluetooth Low Energy and see if that gives any hints.
Not sure if it is helpful, but this gives some explanation: https://docs.microsoft.com/en-us/windows/uwp/devices-sensors/gatt-client#connecting-to-the-device
Hello David, Hello Henrik, I'm trying to activate the bleak package and without any success. I've tried it on 4 PCs (All of them are Win10 x 64). I'm using python 3.9.7, bleak 0.13.0 and windows10x64. I've tried different bleak versions 0.6.4, 0.8.0, 0.12.1, different python versions 3.6.10, 3.8.4, 3.10 all the combinations and my conclusion is that it does not work on every ble H/W . I have one laptop with win10x 64 21H1OS Build 19043.1288 python 3.9.7, bleak 0.13.0 and if I use the built-in bluetooth which is Qualcomm Atherros QCA61x4 Bluetooth 4.1 Driver version 10.0.0.297 it works. This is the only Ble that had actually worked. On the same laptop, when I put a different ble-usb dongle, the scanning/discover function is working but I couldn't succeed to activate the connect/gets service functions (using the 'connect_by_bledevice.py' example). After several seconds, I've got the following error message:
========== RESTART: E:\bleak-develop\examples\connect_by_bledevice.py ==========
Traceback (most recent call last):
File "C:\Program Files\Python39\lib\asyncio\locks.py", line 227, in wait
await fut
asyncio.exceptions.CancelledError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files\Python39\lib\asyncio\tasks.py", line 492, in wait_for
fut.result()
asyncio.exceptions.CancelledError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "E:\bleak-develop\examples\connect_by_bledevice.py", line 31, in <module>
asyncio.run(main(sys.argv[1] if len(sys.argv) == 2 else ADDRESS))
File "C:\Program Files\Python39\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
return future.result()
File "E:\bleak-develop\examples\connect_by_bledevice.py", line 23, in main
async with BleakClient(device) as client:
File "C:\Program Files\Python39\lib\site-packages\bleak\backends\client.py", line 61, in __aenter__
await self.connect()
File "C:\Program Files\Python39\lib\site-packages\bleak\backends\winrt\client.py", line 249, in connect
await asyncio.wait_for(event.wait(), timeout=timeout)
File "C:\Program Files\Python39\lib\asyncio\tasks.py", line 494, in wait_for
raise exceptions.TimeoutError() from exc
asyncio.exceptions.TimeoutError
I've tried this with the following BLEs:
- Avantree DG45 Bluetooth 5.0 USB
- Gold Touch 4.0 E-USB-BT - Generic Bluetooth Radio - Cambridge Silicon Radio Ltd. - Driver 10.0.19041.1202
- Intel(R) Wireless Bluetooth(R) - Driver 20.100.7.1
- Qualcomm Atherros QCA61x4 Bluetooth 4.1 Driver version 10.0.0.297 - The only one that actually works.
@GuyOfek1 Does it work if you increase the timeout
setting for the connect call? Or is it impossible to connect at all using some of the hardware where it failed? Are they all running the same Windows 10? Check with e.g. python -c "import platform; print(platform.win32_ver())"
I'm not sure it will fix the issues here, but could you please try #680 to see if it makes a difference?
I'm not sure it will fix the issues here, but could you please try #680 to see if it makes a difference?
I have a similar issue (BleakError: Could not get GATT services: Unreachable
when trying to instantiate a BleakClient
). I just tried to pip install the develop branch with the recently merged PR #680, but this didn't change the issue.
One thing to note is that the device I am trying to connect to is broadcasting advertisement messages every 100ms for 400µs, so it's not infrequent that it doesn't even display in the list of devices. And anecdotally, my device is also a medical device. Maybe this contributes to this issue?
/EDIT: yes it is an issue specific to the device I am using, as other (medical) devices can be connected to and the services can be listed without an issue. A long timeout doesn't help either unfortunately.
Fixed the issue, it was because Windows was paired with the device, it works now that I unpaired them. I also used this free Microsoft tool to debug, maybe this can be helpful to OP. If this tool cannot connect either, then the issue is with the device or the pairing between the computer and the device, but not Bleak, since the tool I linked to is developed by Microsoft itself, so that's how I troubleshot my own issue.