hbldh / bleak

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

Code working on Linux but not on Windows #487

Closed dolors-1210 closed 3 years ago

dolors-1210 commented 3 years ago

Description

Hi, it's Lola! I developed a code using bleak module in Linux and when I imported the code in a Windows machine, the code did not work. It's like bleakScanner is working but not bleakClient.

What I Did

I made some checkings with simple code extracted from the git examples and that was my results:

discover.py: this code is working for Windows:

"""
Scan/Discovery
Example showing how to scan for BLE devices.
Updated on 2019-03-25 by hbldh <henrik.blidh@nedomkull.com>
"""

import asyncio
from bleak import discover

async def run():
    devices = await discover()
    for d in devices:
        print(d)

loop = asyncio.get_event_loop()
loop.run_until_complete(run())

connect_by_bledevice.py: this code is not working for Windows but is working for Linux. I replaced the mac_addr field for the mac_addr of my phone and the phone had the bluetooth enabled.

"""
Connect by BLEDevice
"""

import asyncio
import platform

from bleak import BleakClient, BleakScanner

async def print_services(mac_addr: str):
    device = await BleakScanner.find_device_by_address(mac_addr)
    async with BleakClient(device) as client:
        svcs = await client.get_services()
        print("Services:")
        for service in svcs:
            print(service)

mac_addr = (
    "24:71:89:cc:09:05"
    if platform.system() != "Darwin"
    else "B9EA5233-37EF-4DD6-87A8-2A875E821C46"
)
loop = asyncio.get_event_loop()
loop.run_until_complete(print_services(mac_addr))

The errors I'm getting are the following:

Traceback (most recent call last):
  File "c:/Users/34638/Documents/UPC/TFG/Code/Application/Sandbox.py", line 26, in <module>
    loop.run_until_complete(print_services(mac_addr))
  File "C:\Users\34638\AppData\Local\Programs\Python\Python37-32\lib\asyncio\base_events.py", line 579, in run_until_complete
    return future.result()
  File "c:/Users/34638/Documents/UPC/TFG/Code/Application/Sandbox.py", line 13, in print_services
    async with BleakClient(device) as client:
  File "C:\Users\34638\AppData\Local\Programs\Python\Python37-32\lib\site-packages\bleak\backends\client.py", line 59, in __aenter__
    await self.connect()
  File "C:\Users\34638\AppData\Local\Programs\Python\Python37-32\lib\site-packages\bleak\backends\dotnet\client.py", line 142, in connect
    self.address, timeout=timeout
  File "C:\Users\34638\AppData\Local\Programs\Python\Python37-32\lib\site-packages\bleak\backends\scanner.py", line 159, in find_device_by_address   
    device_identifier = device_identifier.lower()
AttributeError: 'NoneType' object has no attribute 'lower'

Please if you know what happens just let me know.

Thanks!

Lola

hbldh commented 3 years ago

Hello,

It seems like the await BleakScanner.find_device_by_address call could not find your device, so a None was returned. The example could be improved there. It should be something like this:

"""
Connect by BLEDevice
"""

import asyncio
import platform

from bleak import BleakClient, BleakScanner
from bleak.exc import BleakError

async def print_services(ble_address: str):
    device = await BleakScanner.find_device_by_address(ble_address, timeout=20.0)
    if not device:
        raise BleakError(f"A device with address {ble_address} could not be found.")
    async with BleakClient(device) as client:
        svcs = await client.get_services()
        print("Services:")
        for service in svcs:
            print(service)

ble_address = (
    "24:71:89:cc:09:05"
    if platform.system() != "Darwin"
    else "B9EA5233-37EF-4DD6-87A8-2A875E821C46"
)
loop = asyncio.get_event_loop()
loop.run_until_complete(print_services(ble_address))

Connecting to phones have often caused problems with Bleak. Sometimes they require pairing, so you will have to experiment some with getting it to work. Anyway, try increasing the timeout from the default 10 seconds to maybe 20 for the discovery part (I have done this in the example above)

Also try to use the service_explorer.py example instead. It is more verbose and well tested.

A new release of Bleak was just done, version 0.11.0. I recommend upgrading to that since there are some critical fixes for the Windows backend.