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

BleakClientWinRT.get_services takes longer time in Windows 11 than in Windows 10 #686

Closed
hbldh opened this issue Nov 6, 2021 · 2 comments · Fixed by #695
Closed

BleakClientWinRT.get_services takes longer time in Windows 11 than in Windows 10 #686

hbldh opened this issue Nov 6, 2021 · 2 comments · Fixed by #695
Assignees
Labels
Backend: WinRT Issues or PRs relating to the WinRT backend help wanted Extra attention is needed Opinions Appreciated Please add an opinion on your desired resolution on this issue!

Comments

@hbldh
Copy link
Owner

hbldh commented Nov 6, 2021

  • bleak version: 0.14.0a1 (feature/wtd branch)
  • Python version: 3.7.8
  • Operating System: Windows 11 ('10', '10.0.22000', 'SP0', 'Multiprocessor Free')

Description

get_services call take a lot longer time in Windows 11 than in Windows 10.

What I Did

A near 10 second connection delay in Windows 11 comparing to 1-5 seconds in Windows 10 was surprising to me. This issue is more of a mental note than an error report. Based on memory, it was faster than 10 seconds earlier.

If anyone else using Bleak and noticing the same, please verify that it takes longer time in Windows 11 if you have experienced the same, or refute it if you have experienced the opposite.

import os

os.environ["BLEAK_LOGGING"] = "1"

import asyncio
from bleak import BleakClient, BleakScanner

address = "24:71:89:cc:09:05"
MODEL_NBR_UUID = "00002a24-0000-1000-8000-00805f9b34fb"


async def run(address):
    device = await BleakScanner.find_device_by_address(address)
    async with BleakClient(device) as client:
        model_number = await client.read_gatt_char(MODEL_NBR_UUID)
        print("Model Number: {0}".format("".join(map(chr, model_number))))

asyncio.run(run(address))

Output:

2021-11-06 21:54:40,931 bleak.backends.winrt.scanner DEBUG: Received 24:71:89:CC:09:05: Unknown.
2021-11-06 21:54:40,933 bleak.backends.winrt.scanner DEBUG: 2 devices found. Watcher status: 3.
2021-11-06 21:54:40,933 bleak.backends.winrt.client DEBUG: Connecting to BLE device @ 24:71:89:CC:09:05
2021-11-06 21:54:41,551 bleak.backends.winrt.client DEBUG: _ConnectionStatusChanged_Handler: 1
2021-11-06 21:54:41,551 bleak.backends.winrt.client DEBUG: Get Services...
2021-11-06 21:54:53,966 bleak.backends.winrt.client INFO: Services resolved for BleakClientWinRT (24:71:89:CC:09:05)
2021-11-06 21:54:54,085 bleak.backends.winrt.client DEBUG: Read Characteristic 000C : bytearray(b'CC2650 SensorTag')
Model Number: CC2650 SensorTag
2021-11-06 21:54:54,086 bleak.backends.winrt.client DEBUG: Disconnecting from BLE device...
2021-11-06 21:54:57,205 bleak.backends.winrt.client DEBUG: _ConnectionStatusChanged_Handler: 0
@hbldh hbldh added help wanted Extra attention is needed Opinions Appreciated Please add an opinion on your desired resolution on this issue! Backend: WinRT Issues or PRs relating to the WinRT backend labels Nov 6, 2021
@hbldh hbldh self-assigned this Nov 6, 2021
@tifsolus
Copy link

tifsolus commented Nov 8, 2021

I'll spin up windows 11 and give a try.

hbldh added a commit that referenced this issue Dec 2, 2021
This allows Bleak to use the Windows Bluetooth cache for services, characteristics and descriptors. Since Windows 11 took 10 seconds to fetch services on each connect, this will provide a faster connect on devices that do not change.

Fixes #686

Also modified Characteristic class in WinRT to use description property from super class in case of empty string description from Windows object.

The SensorTag example is a bit expanded to also light up a LED.
@dlech
Copy link
Collaborator

dlech commented Dec 2, 2021

Discovering services takes only one second for me on Windows 11.

2021-12-02 16:53:56,187 bleak.backends.winrt.client DEBUG: Get Services...
2021-12-02 16:53:57,127 bleak.backends.winrt.client INFO: Services resolved for BleakClientWinRT (F0:F8:F2:48:90:50)

hbldh added a commit that referenced this issue Dec 7, 2021
This allows Bleak to use the Windows Bluetooth cache for services, characteristics and descriptors. Since Windows 11 took 10 seconds to fetch services on each connect, this will provide a faster connect on devices that do not change.

Fixes #686

Also modified Characteristic class in WinRT to use description property from super class in case of empty string description from Windows object.

The SensorTag example is a bit expanded to also light up a LED.
dlech added a commit that referenced this issue Jul 29, 2022
Added
-----

* Added new ``assigned_numbers`` module and ``AdvertisementDataType`` enum.
* Added new ``bluez`` kwarg to ``BleakScanner`` in BlueZ backend.
* Added support for passive scanning in the BlueZ backend. Fixes #606.
* Added option to use cached services, characteristics and descriptors in WinRT backend. Fixes #686.
* Added ``PendingDeprecationWarning`` to use of ``address_type`` as keyword argument. It will be moved into the
  ``winrt`` keyword instead according to #623.
* Added better error message when adapter is not present in BlueZ backend. Fixes #889.

Changed
-------

* Add ``py.typed`` file so mypy discovers Bleak's type annotations.
* UUID descriptions updated to 2022-03-16 assigned numbers document.
* Replace use of deprecated ``asyncio.get_event_loop()`` in Android backend.
* Adjust default timeout for ``read_gatt_char()`` with CoreBluetooth to 10s. Merged #891.
* ``BleakScanner()`` args ``detection_callback`` and ``service_uuids`` are no longer keyword-only.
* ``BleakScanner()`` arg ``scanning_mode`` is no longer Windows-only and is no longer keyword-only.
* All ``BleakScanner()`` instances in BlueZ backend now use common D-Bus object manager.
* Deprecated ``filters`` kwarg in ``BleakScanner`` in BlueZ backend.
* BlueZ version is now checked on first connection instead of import to avoid import side effects. Merged #907.

Fixed
-----

* Documentation fixes.
* On empty characteristic description from WinRT, use the lookup table instead of returning empty string.
* Fixed detection of first advertisement in BlueZ backend. Merged #903.
* Fixed performance issues in BlueZ backend caused by calling "GetManagedObjects" each time a
  ``BleakScanner`` scans or ``BleakClient`` is connected. Fixes #500.
* Fixed not handling "InterfacesRemoved" in ``BleakClient`` in BlueZ backend. Fixes #882.
* Fixed leaking D-Bus socket file descriptors in BlueZ backend. Fixes #805.
@dlech dlech mentioned this issue Jul 29, 2022
dlech added a commit that referenced this issue Jul 29, 2022
Added
-----

* Added new ``assigned_numbers`` module and ``AdvertisementDataType`` enum.
* Added new ``bluez`` kwarg to ``BleakScanner`` in BlueZ backend.
* Added support for passive scanning in the BlueZ backend. Fixes #606.
* Added option to use cached services, characteristics and descriptors in WinRT backend. Fixes #686.
* Added ``PendingDeprecationWarning`` to use of ``address_type`` as keyword argument. It will be moved into the
  ``winrt`` keyword instead according to #623.
* Added better error message when adapter is not present in BlueZ backend. Fixes #889.

Changed
-------

* Add ``py.typed`` file so mypy discovers Bleak's type annotations.
* UUID descriptions updated to 2022-03-16 assigned numbers document.
* Replace use of deprecated ``asyncio.get_event_loop()`` in Android backend.
* Adjust default timeout for ``read_gatt_char()`` with CoreBluetooth to 10s. Merged #891.
* ``BleakScanner()`` args ``detection_callback`` and ``service_uuids`` are no longer keyword-only.
* ``BleakScanner()`` arg ``scanning_mode`` is no longer Windows-only and is no longer keyword-only.
* All ``BleakScanner()`` instances in BlueZ backend now use common D-Bus object manager.
* Deprecated ``filters`` kwarg in ``BleakScanner`` in BlueZ backend.
* BlueZ version is now checked on first connection instead of import to avoid import side effects. Merged #907.

Fixed
-----

* Documentation fixes.
* On empty characteristic description from WinRT, use the lookup table instead of returning empty string.
* Fixed detection of first advertisement in BlueZ backend. Merged #903.
* Fixed performance issues in BlueZ backend caused by calling "GetManagedObjects" each time a
  ``BleakScanner`` scans or ``BleakClient`` is connected. Fixes #500.
* Fixed not handling "InterfacesRemoved" in ``BleakClient`` in BlueZ backend. Fixes #882.
* Fixed leaking D-Bus socket file descriptors in BlueZ backend. Fixes #805.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend: WinRT Issues or PRs relating to the WinRT backend help wanted Extra attention is needed Opinions Appreciated Please add an opinion on your desired resolution on this issue!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants