hbldh / bleak

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

Need help - TIMETOUT ERROR #1388

Open NeverWe1come opened 10 months ago

NeverWe1come commented 10 months ago

Description

I am trying to obtain all services, characteristics and descriptors, for all ble devices i found near.

What I Did

import argparse
import asyncio
import logging
from bleak import BleakClient, BleakScanner, BleakError

logger = logging.getLogger(__name__)

async def process_device(device, args):
    try:
        logger.info("connecting to device %s...", device)

        async with BleakClient(
            device,
            services=args.services,
        ) as client:
            try:
                logger.info("connected to device %s", device)

                for service in client.services:
                    logger.info("[Service] %s", service)

                    for char in service.characteristics:
                        if "read" in char.properties:
                            try:
                                value = await client.read_gatt_char(char.uuid)
                                logger.info(
                                    "  [Characteristic] %s (%s), Value: %r",
                                    char,
                                    ",".join(char.properties),
                                    value,
                                )
                            except Exception as e:
                                logger.error(
                                    "  [Characteristic] %s (%s), Error: %s",
                                    char,
                                    ",".join(char.properties),
                                    e,
                                )
                        else:
                            logger.info(
                                "  [Characteristic] %s (%s)", char, ",".join(char.properties)
                            )

                        for descriptor in char.descriptors:
                            try:
                                value = await client.read_gatt_descriptor(descriptor.handle)
                                logger.info("    [Descriptor] %s, Value: %r", descriptor, value)
                            except Exception as e:
                                logger.error("    [Descriptor] %s, Error: %s", descriptor, e)
                logger.info("disconnecting from device %s", device)
            except (asyncio.CancelledError, TimeoutError):
                logger.warning("Connection or timeout error for device %s", device)
            except Exception as e:
                logger.error("Error during device processing: %s", e)
    except BleakError as e:
        logger.error("Error while processing device %s: %s", device, e)

async def main(args: argparse.Namespace):
    logger.info("starting scan...")

    scanner = BleakScanner()
    devices = await scanner.discover()
    logger.info("discovered %d devices", len(devices))

    for device in devices:
        await process_device(device, args)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()

    parser.add_argument(
        "--services",
        nargs="+",
        metavar="<uuid>",
        help="if provided, only enumerate matching service(s)",
    )

    parser.add_argument(
        "-d",
        "--debug",
        action="store_true",
        help="sets the log level to debug",
    )

    args = parser.parse_args()

    log_level = logging.DEBUG if args.debug else logging.INFO
    logging.basicConfig(
        level=log_level,
        format="%(asctime)-15s %(name)-8s %(levelname)s: %(message)s",
    )

    asyncio.run(main(args))

Logs and My problem

EXAMPLE1:

2023-08-14 19:13:25,976 __main__ INFO: starting scan...
2023-08-14 19:13:31,019 __main__ INFO: discovered 10 devices
2023-08-14 19:13:31,019 __main__ INFO: connecting to device D4:9D:C0:3F:9C:CF: None...
2023-08-14 19:13:31,025 __main__ ERROR: Error while processing device D4:9D:C0:3F:9C:CF: None: Device with address D4:9D:C0:3F:9C:CF was not found.
2023-08-14 19:13:31,025 __main__ INFO: connecting to device 69:93:AA:AB:5F:12: None...
Traceback (most recent call last):
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\venv\Lib\site-packages\bleak\backends\winrt\client.py", line 440, in connect
    self.services = await self.get_services(
                    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\venv\Lib\site-packages\bleak\backends\winrt\client.py", line 707, in get_services
    await FutureLike(service.get_characteristics_async(*args)),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\venv\Lib\site-packages\bleak\backends\winrt\client.py", line 1102, in __await__
    yield self  # This tells Task to wait for completion.
    ^^^^^^^^^^
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\venv\Lib\site-packages\bleak\backends\winrt\client.py", line 1037, in result
    raise asyncio.CancelledError
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\src\BLEScanDetect\pruabas_vuln.py", line 94, in <module>
  File "C:\Users\vgarc\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\vgarc\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vgarc\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\src\BLEScanDetect\pruabas_vuln.py", line 67, in main
    await process_device(device, args)
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\src\BLEScanDetect\pruabas_vuln.py", line 13, in process_device
    async with BleakClient(
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\venv\Lib\site-packages\bleak\__init__.py", line 491, in __aenter__
    await self.connect()
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\venv\Lib\site-packages\bleak\__init__.py", line 531, in connect
    return await self._backend.connect(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\venv\Lib\site-packages\bleak\backends\winrt\client.py", line 396, in connect
    async with async_timeout(timeout):
  File "C:\Users\vgarc\AppData\Local\Programs\Python\Python311\Lib\asyncio\timeouts.py", line 111, in __aexit__
    raise TimeoutError from exc_val
TimeoutError

EXAMPLE2:

2023-08-14 19:21:31,083 __main__ INFO: starting scan...
2023-08-14 19:21:36,115 __main__ INFO: discovered 12 devices
2023-08-14 19:21:36,115 __main__ INFO: connecting to device 4B:08:33:40:64:5E: None...
2023-08-14 19:21:42,173 __main__ ERROR: Error while processing device 4B:08:33:40:64:5E: None: Could not get GATT services: Unreachable
2023-08-14 19:21:42,173 __main__ INFO: connecting to device 7F:D4:6A:DF:C6:2C: None...
2023-08-14 19:21:46,240 __main__ INFO: connected to device 7F:D4:6A:DF:C6:2C: None
2023-08-14 19:21:46,240 __main__ INFO: [Service] 00001800-0000-1000-8000-00805f9b34fb (Handle: 1): Generic Access Profile
2023-08-14 19:21:46,270 __main__ INFO:   [Characteristic] 00002a00-0000-1000-8000-00805f9b34fb (Handle: 2): Device Name (read), Value: bytearray(b'iPad')
2023-08-14 19:21:46,297 __main__ INFO:   [Characteristic] 00002a01-0000-1000-8000-00805f9b34fb (Handle: 4): Appearance (read), Value: bytearray(b'\x80\x02')
2023-08-14 19:21:46,297 __main__ INFO: [Service] 00001801-0000-1000-8000-00805f9b34fb (Handle: 6): Generic Attribute Profile
2023-08-14 19:21:46,297 __main__ INFO:   [Characteristic] 00002a05-0000-1000-8000-00805f9b34fb (Handle: 7): Service Changed (indicate)
2023-08-14 19:21:46,325 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 9): Client Characteristic Configuration, Value: bytearray(b'\x02\x00')
2023-08-14 19:21:46,325 __main__ INFO: [Service] 0000180a-0000-1000-8000-00805f9b34fb (Handle: 10): Device Information
2023-08-14 19:21:46,360 __main__ INFO:   [Characteristic] 00002a29-0000-1000-8000-00805f9b34fb (Handle: 11): Manufacturer Name String (read), Value: bytearray(b'Apple Inc.')
2023-08-14 19:21:46,394 __main__ INFO:   [Characteristic] 00002a24-0000-1000-8000-00805f9b34fb (Handle: 13): Model Number String (read), Value: bytearray(b'iPad7,11')
2023-08-14 19:21:46,394 __main__ INFO: [Service] d0611e78-bbb4-4591-a5f8-487910ae4366 (Handle: 15): Apple Continuity Service
2023-08-14 19:21:46,394 __main__ INFO:   [Characteristic] 8667556c-9a37-4c91-84ed-54ee27d90049 (Handle: 16): Apple Continuity Characteristic (write,notify,extended-properties)
2023-08-14 19:21:46,420 __main__ INFO:     [Descriptor] 00002900-0000-1000-8000-00805f9b34fb (Handle: 18): Characteristic Extended Properties, Value: bytearray(b'\x01\x00')
2023-08-14 19:21:46,460 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 19): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:21:46,460 __main__ INFO: [Service] 9fa480e0-4967-4542-9390-d343dc5d04ae (Handle: 20): Apple Nearby Service
2023-08-14 19:21:46,460 __main__ INFO:   [Characteristic] af0badb1-5b99-43cd-917a-a77bc549e3cc (Handle: 21): Nearby Characteristic (write,notify,extended-properties)
2023-08-14 19:21:46,503 __main__ INFO:     [Descriptor] 00002900-0000-1000-8000-00805f9b34fb (Handle: 23): Characteristic Extended Properties, Value: bytearray(b'\x01\x00')
2023-08-14 19:21:46,531 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 24): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:21:46,531 __main__ INFO: [Service] 0000180f-0000-1000-8000-00805f9b34fb (Handle: 25): Battery Service
2023-08-14 19:21:46,573 __main__ ERROR:   [Characteristic] 00002a19-0000-1000-8000-00805f9b34fb (Handle: 26): Battery Level (read,notify), Error: Could not read characteristic handle 26: Protocol Error 0x05: Insufficient Authentication
2023-08-14 19:21:46,599 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 28): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:21:46,600 __main__ INFO: [Service] 00001805-0000-1000-8000-00805f9b34fb (Handle: 29): Current Time Service
2023-08-14 19:21:46,710 __main__ ERROR:   [Characteristic] 00002a2b-0000-1000-8000-00805f9b34fb (Handle: 30): Current Time (read,notify), Error: Could not read characteristic handle 30: Protocol Error 0x05: Insufficient Authentication
2023-08-14 19:21:46,803 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 32): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:21:46,891 __main__ ERROR:   [Characteristic] 00002a0f-0000-1000-8000-00805f9b34fb (Handle: 33): Local Time Information (read), Error: Could not read characteristic handle 33: Protocol Error 0x05: Insufficient Authentication
2023-08-14 19:21:46,891 __main__ INFO: [Service] 7905f431-b5ce-4e99-a40f-4b1e122d00d0 (Handle: 35): Apple Notification Center Service
2023-08-14 19:21:46,891 __main__ INFO:   [Characteristic] 69d1d8f3-45e1-49a8-9821-9bbdfdaad9d9 (Handle: 36): Control Point (write,extended-properties)
2023-08-14 19:21:46,970 __main__ INFO:     [Descriptor] 00002900-0000-1000-8000-00805f9b34fb (Handle: 38): Characteristic Extended Properties, Value: bytearray(b'\x01\x00')
2023-08-14 19:21:46,979 __main__ INFO:   [Characteristic] 9fbf120d-6301-42d9-8c58-25e699a21dbd (Handle: 39): Notification Source (notify)
2023-08-14 19:21:47,068 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 41): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:21:47,069 __main__ INFO:   [Characteristic] 22eac6e9-24d6-4bb5-be44-b36ace7c7bfb (Handle: 42): Data Source (notify)
2023-08-14 19:21:47,199 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 44): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:21:47,199 __main__ INFO: [Service] 89d3502b-0f36-433a-8ef4-c502ad55f8dc (Handle: 45): Apple Media Service
2023-08-14 19:21:47,199 __main__ INFO:   [Characteristic] 9b3c81d8-57b1-4a8a-b8df-0e56f7ca51c2 (Handle: 46): Remote Command (write,notify,extended-properties)
2023-08-14 19:21:47,281 __main__ INFO:     [Descriptor] 00002900-0000-1000-8000-00805f9b34fb (Handle: 48): Characteristic Extended Properties, Value: bytearray(b'\x01\x00')
2023-08-14 19:21:47,419 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 49): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:21:47,419 __main__ INFO:   [Characteristic] 2f7cabce-808d-411f-9a0c-bb92ba96c102 (Handle: 50): Entity Update (write,notify,extended-properties)
2023-08-14 19:21:47,500 __main__ INFO:     [Descriptor] 00002900-0000-1000-8000-00805f9b34fb (Handle: 52): Characteristic Extended Properties, Value: bytearray(b'\x01\x00')
2023-08-14 19:21:47,591 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 53): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:21:47,681 __main__ ERROR:   [Characteristic] c6b2f38c-23ab-46d8-a6ab-a3a870bbd5d7 (Handle: 54): Entity Attribute (read,write,extended-properties), Error: Could not read characteristic handle 54: Protocol Error 0x05: Insufficient Authentication
2023-08-14 19:21:47,809 __main__ INFO:     [Descriptor] 00002900-0000-1000-8000-00805f9b34fb (Handle: 56): Characteristic Extended Properties, Value: bytearray(b'\x01\x00')
2023-08-14 19:21:47,809 __main__ INFO: disconnecting from device 7F:D4:6A:DF:C6:2C: None
2023-08-14 19:21:50,876 __main__ INFO: connecting to device 7F:46:7A:F8:81:E3: None...
2023-08-14 19:21:50,881 __main__ ERROR: Error while processing device 7F:46:7A:F8:81:E3: None: Device with address 7F:46:7A:F8:81:E3 was not found.
2023-08-14 19:21:50,882 __main__ INFO: connecting to device D4:9D:C0:3F:9C:CF: None...
2023-08-14 19:21:50,887 __main__ ERROR: Error while processing device D4:9D:C0:3F:9C:CF: None: Device with address D4:9D:C0:3F:9C:CF was not found.
2023-08-14 19:21:50,887 __main__ INFO: connecting to device 5D:D7:0E:33:6A:23: None...
2023-08-14 19:21:58,040 __main__ INFO: connected to device 5D:D7:0E:33:6A:23: None
2023-08-14 19:21:58,040 __main__ INFO: [Service] 00001800-0000-1000-8000-00805f9b34fb (Handle: 1): Generic Access Profile
2023-08-14 19:21:58,130 __main__ INFO:   [Characteristic] 00002a00-0000-1000-8000-00805f9b34fb (Handle: 2): Device Name (read), Value: bytearray(b'iPhone')
2023-08-14 19:21:58,164 __main__ INFO:   [Characteristic] 00002a01-0000-1000-8000-00805f9b34fb (Handle: 4): Appearance (read), Value: bytearray(b'@\x00')
2023-08-14 19:21:58,164 __main__ INFO: [Service] 00001801-0000-1000-8000-00805f9b34fb (Handle: 6): Generic Attribute Profile
2023-08-14 19:21:58,164 __main__ INFO:   [Characteristic] 00002a05-0000-1000-8000-00805f9b34fb (Handle: 7): Service Changed (indicate)
2023-08-14 19:21:58,219 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 9): Client Characteristic Configuration, Value: bytearray(b'\x02\x00')
2023-08-14 19:21:58,219 __main__ INFO: [Service] 0000180a-0000-1000-8000-00805f9b34fb (Handle: 10): Device Information
2023-08-14 19:21:58,279 __main__ INFO:   [Characteristic] 00002a29-0000-1000-8000-00805f9b34fb (Handle: 11): Manufacturer Name String (read), Value: bytearray(b'Apple Inc.')
2023-08-14 19:21:58,354 __main__ INFO:   [Characteristic] 00002a24-0000-1000-8000-00805f9b34fb (Handle: 13): Model Number String (read), Value: bytearray(b'iPhone10,4')
2023-08-14 19:21:58,355 __main__ INFO: [Service] d0611e78-bbb4-4591-a5f8-487910ae4366 (Handle: 15): Apple Continuity Service
2023-08-14 19:21:58,355 __main__ INFO:   [Characteristic] 8667556c-9a37-4c91-84ed-54ee27d90049 (Handle: 16): Apple Continuity Characteristic (write,notify,extended-properties)  
2023-08-14 19:21:58,531 __main__ INFO:     [Descriptor] 00002900-0000-1000-8000-00805f9b34fb (Handle: 18): Characteristic Extended Properties, Value: bytearray(b'\x01\x00')
2023-08-14 19:21:58,659 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 19): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:21:58,659 __main__ INFO: [Service] 9fa480e0-4967-4542-9390-d343dc5d04ae (Handle: 20): Apple Nearby Service
2023-08-14 19:21:58,659 __main__ INFO:   [Characteristic] af0badb1-5b99-43cd-917a-a77bc549e3cc (Handle: 21): Nearby Characteristic (write,notify,extended-properties)
2023-08-14 19:21:58,750 __main__ INFO:     [Descriptor] 00002900-0000-1000-8000-00805f9b34fb (Handle: 23): Characteristic Extended Properties, Value: bytearray(b'\x01\x00')
2023-08-14 19:21:58,882 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 24): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:21:58,883 __main__ INFO: [Service] 0000180f-0000-1000-8000-00805f9b34fb (Handle: 25): Battery Service
2023-08-14 19:21:58,960 __main__ ERROR:   [Characteristic] 00002a19-0000-1000-8000-00805f9b34fb (Handle: 26): Battery Level (read,notify), Error: Could not read characteristic handle 26: Protocol Error 0x05: Insufficient Authentication
2023-08-14 19:21:59,100 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 28): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:21:59,100 __main__ INFO: [Service] 00001805-0000-1000-8000-00805f9b34fb (Handle: 29): Current Time Service
2023-08-14 19:21:59,183 __main__ ERROR:   [Characteristic] 00002a2b-0000-1000-8000-00805f9b34fb (Handle: 30): Current Time (read,notify), Error: Could not read characteristic handle 30: Protocol Error 0x05: Insufficient Authentication
2023-08-14 19:21:59,311 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 32): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:21:59,404 __main__ ERROR:   [Characteristic] 00002a0f-0000-1000-8000-00805f9b34fb (Handle: 33): Local Time Information (read), Error: Could not read characteristic handle 33: Protocol Error 0x05: Insufficient Authentication
2023-08-14 19:21:59,404 __main__ INFO: [Service] 7905f431-b5ce-4e99-a40f-4b1e122d00d0 (Handle: 35): Apple Notification Center Service
2023-08-14 19:21:59,404 __main__ INFO:   [Characteristic] 69d1d8f3-45e1-49a8-9821-9bbdfdaad9d9 (Handle: 36): Control Point (write,extended-properties)
2023-08-14 19:21:59,493 __main__ INFO:     [Descriptor] 00002900-0000-1000-8000-00805f9b34fb (Handle: 38): Characteristic Extended Properties, Value: bytearray(b'\x01\x00')
2023-08-14 19:21:59,493 __main__ INFO:   [Characteristic] 9fbf120d-6301-42d9-8c58-25e699a21dbd (Handle: 39): Notification Source (notify)
2023-08-14 19:21:59,619 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 41): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:21:59,619 __main__ INFO:   [Characteristic] 22eac6e9-24d6-4bb5-be44-b36ace7c7bfb (Handle: 42): Data Source (notify)
2023-08-14 19:21:59,751 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 44): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:21:59,751 __main__ INFO: [Service] 89d3502b-0f36-433a-8ef4-c502ad55f8dc (Handle: 45): Apple Media Service
2023-08-14 19:21:59,751 __main__ INFO:   [Characteristic] 9b3c81d8-57b1-4a8a-b8df-0e56f7ca51c2 (Handle: 46): Remote Command (write,notify,extended-properties)
2023-08-14 19:21:59,970 __main__ INFO:     [Descriptor] 00002900-0000-1000-8000-00805f9b34fb (Handle: 48): Characteristic Extended Properties, Value: bytearray(b'\x01\x00')
2023-08-14 19:22:00,060 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 49): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:22:00,060 __main__ INFO:   [Characteristic] 2f7cabce-808d-411f-9a0c-bb92ba96c102 (Handle: 50): Entity Update (write,notify,extended-properties)
2023-08-14 19:22:00,191 __main__ INFO:     [Descriptor] 00002900-0000-1000-8000-00805f9b34fb (Handle: 52): Characteristic Extended Properties, Value: bytearray(b'\x01\x00')
2023-08-14 19:22:00,365 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 53): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:22:00,541 __main__ ERROR:   [Characteristic] c6b2f38c-23ab-46d8-a6ab-a3a870bbd5d7 (Handle: 54): Entity Attribute (read,write,extended-properties), Error: Could not read characteristic handle 54: Protocol Error 0x05: Insufficient Authentication
2023-08-14 19:22:00,760 __main__ INFO:     [Descriptor] 00002900-0000-1000-8000-00805f9b34fb (Handle: 56): Characteristic Extended Properties, Value: bytearray(b'\x01\x00')
2023-08-14 19:22:00,760 __main__ INFO: disconnecting from device 5D:D7:0E:33:6A:23: None
2023-08-14 19:22:03,821 __main__ INFO: connecting to device 7B:78:87:6A:84:96: None...
2023-08-14 19:22:11,129 __main__ INFO: connected to device 7B:78:87:6A:84:96: None
2023-08-14 19:22:11,129 __main__ INFO: [Service] 00001800-0000-1000-8000-00805f9b34fb (Handle: 1): Generic Access Profile
2023-08-14 19:22:11,171 __main__ INFO:   [Characteristic] 00002a00-0000-1000-8000-00805f9b34fb (Handle: 2): Device Name (read), Value: bytearray(b'iPhone')
2023-08-14 19:22:11,198 __main__ INFO:   [Characteristic] 00002a01-0000-1000-8000-00805f9b34fb (Handle: 4): Appearance (read), Value: bytearray(b'@\x00')
2023-08-14 19:22:11,198 __main__ INFO: [Service] 00001801-0000-1000-8000-00805f9b34fb (Handle: 6): Generic Attribute Profile
2023-08-14 19:22:11,199 __main__ INFO:   [Characteristic] 00002a05-0000-1000-8000-00805f9b34fb (Handle: 7): Service Changed (indicate)
2023-08-14 19:22:11,239 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 9): Client Characteristic Configuration, Value: bytearray(b'\x02\x00')
2023-08-14 19:22:11,241 __main__ INFO: [Service] 0000180a-0000-1000-8000-00805f9b34fb (Handle: 10): Device Information
2023-08-14 19:22:11,267 __main__ INFO:   [Characteristic] 00002a29-0000-1000-8000-00805f9b34fb (Handle: 11): Manufacturer Name String (read), Value: bytearray(b'Apple Inc.')
2023-08-14 19:22:11,294 __main__ INFO:   [Characteristic] 00002a24-0000-1000-8000-00805f9b34fb (Handle: 13): Model Number String (read), Value: bytearray(b'iPhone12,1')
2023-08-14 19:22:11,295 __main__ INFO: [Service] d0611e78-bbb4-4591-a5f8-487910ae4366 (Handle: 15): Apple Continuity Service
2023-08-14 19:22:11,295 __main__ INFO:   [Characteristic] 8667556c-9a37-4c91-84ed-54ee27d90049 (Handle: 16): Apple Continuity Characteristic (write,notify,extended-properties)  
2023-08-14 19:22:11,322 __main__ INFO:     [Descriptor] 00002900-0000-1000-8000-00805f9b34fb (Handle: 18): Characteristic Extended Properties, Value: bytearray(b'\x01\x00')
2023-08-14 19:22:11,364 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 19): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:22:11,364 __main__ INFO: [Service] 9fa480e0-4967-4542-9390-d343dc5d04ae (Handle: 20): Apple Nearby Service
2023-08-14 19:22:11,364 __main__ INFO:   [Characteristic] af0badb1-5b99-43cd-917a-a77bc549e3cc (Handle: 21): Nearby Characteristic (write,notify,extended-properties)
2023-08-14 19:22:11,404 __main__ INFO:     [Descriptor] 00002900-0000-1000-8000-00805f9b34fb (Handle: 23): Characteristic Extended Properties, Value: bytearray(b'\x01\x00')
2023-08-14 19:22:11,445 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 24): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:22:11,447 __main__ INFO: [Service] 0000180f-0000-1000-8000-00805f9b34fb (Handle: 25): Battery Service
2023-08-14 19:22:11,473 __main__ ERROR:   [Characteristic] 00002a19-0000-1000-8000-00805f9b34fb (Handle: 26): Battery Level (read,notify), Error: Could not read characteristic handle 26: Protocol Error 0x05: Insufficient Authentication
2023-08-14 19:22:11,610 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 28): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:22:11,610 __main__ INFO: [Service] 00001805-0000-1000-8000-00805f9b34fb (Handle: 29): Current Time Service
2023-08-14 19:22:11,829 __main__ ERROR:   [Characteristic] 00002a2b-0000-1000-8000-00805f9b34fb (Handle: 30): Current Time (read,notify), Error: Could not read characteristic handle 30: Protocol Error 0x05: Insufficient Authentication
2023-08-14 19:22:11,916 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 32): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:22:12,001 __main__ ERROR:   [Characteristic] 00002a0f-0000-1000-8000-00805f9b34fb (Handle: 33): Local Time Information (read), Error: Could not read characteristic handle 33: Protocol Error 0x05: Insufficient Authentication
2023-08-14 19:22:12,001 __main__ INFO: [Service] 7905f431-b5ce-4e99-a40f-4b1e122d00d0 (Handle: 35): Apple Notification Center Service
2023-08-14 19:22:12,001 __main__ INFO:   [Characteristic] 69d1d8f3-45e1-49a8-9821-9bbdfdaad9d9 (Handle: 36): Control Point (write,extended-properties)
2023-08-14 19:22:12,133 __main__ INFO:     [Descriptor] 00002900-0000-1000-8000-00805f9b34fb (Handle: 38): Characteristic Extended Properties, Value: bytearray(b'\x01\x00')
2023-08-14 19:22:12,136 __main__ INFO:   [Characteristic] 9fbf120d-6301-42d9-8c58-25e699a21dbd (Handle: 39): Notification Source (notify)
2023-08-14 19:22:12,220 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 41): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:22:12,220 __main__ INFO:   [Characteristic] 22eac6e9-24d6-4bb5-be44-b36ace7c7bfb (Handle: 42): Data Source (notify)
2023-08-14 19:22:12,397 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 44): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:22:12,397 __main__ INFO: [Service] 89d3502b-0f36-433a-8ef4-c502ad55f8dc (Handle: 45): Apple Media Service
2023-08-14 19:22:12,397 __main__ INFO:   [Characteristic] 9b3c81d8-57b1-4a8a-b8df-0e56f7ca51c2 (Handle: 46): Remote Command (write,notify,extended-properties)
2023-08-14 19:22:12,529 __main__ INFO:     [Descriptor] 00002900-0000-1000-8000-00805f9b34fb (Handle: 48): Characteristic Extended Properties, Value: bytearray(b'\x01\x00')
2023-08-14 19:22:12,610 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 49): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:22:12,610 __main__ INFO:   [Characteristic] 2f7cabce-808d-411f-9a0c-bb92ba96c102 (Handle: 50): Entity Update (write,notify,extended-properties)
2023-08-14 19:22:12,740 __main__ INFO:     [Descriptor] 00002900-0000-1000-8000-00805f9b34fb (Handle: 52): Characteristic Extended Properties, Value: bytearray(b'\x01\x00')
2023-08-14 19:22:12,835 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 53): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-08-14 19:22:13,009 __main__ ERROR:   [Characteristic] c6b2f38c-23ab-46d8-a6ab-a3a870bbd5d7 (Handle: 54): Entity Attribute (read,write,extended-properties), Error: Could not read characteristic handle 54: Protocol Error 0x05: Insufficient Authentication
2023-08-14 19:22:13,140 __main__ INFO:     [Descriptor] 00002900-0000-1000-8000-00805f9b34fb (Handle: 56): Characteristic Extended Properties, Value: bytearray(b'\x01\x00')
2023-08-14 19:22:13,140 __main__ INFO: disconnecting from device 7B:78:87:6A:84:96: None
2023-08-14 19:22:16,163 __main__ INFO: connecting to device 49:09:09:A8:A7:DC: None...
Traceback (most recent call last):
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\venv\Lib\site-packages\bleak\backends\winrt\client.py", line 440, in connect
    self.services = await self.get_services(
                    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\venv\Lib\site-packages\bleak\backends\winrt\client.py", line 671, in get_services
    await FutureLike(self._requester.get_gatt_services_async(*srv_args)),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\venv\Lib\site-packages\bleak\backends\winrt\client.py", line 1102, in __await__
    yield self  # This tells Task to wait for completion.
    ^^^^^^^^^^
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\venv\Lib\site-packages\bleak\backends\winrt\client.py", line 1042, in result
    raise asyncio.CancelledError
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\src\BLEScanDetect\pruabas_vuln.py", line 94, in <module>
    asyncio.run(main(args))
  File "C:\Users\vgarc\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\vgarc\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vgarc\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\src\BLEScanDetect\pruabas_vuln.py", line 67, in main
    await process_device(device, args)
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\src\BLEScanDetect\pruabas_vuln.py", line 13, in process_device
    async with BleakClient(
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\venv\Lib\site-packages\bleak\__init__.py", line 491, in __aenter__
    await self.connect()
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\venv\Lib\site-packages\bleak\__init__.py", line 531, in connect
    return await self._backend.connect(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\venv\Lib\site-packages\bleak\backends\winrt\client.py", line 396, in connect
    async with async_timeout(timeout):
  File "C:\Users\vgarc\AppData\Local\Programs\Python\Python311\Lib\asyncio\timeouts.py", line 111, in __aexit__
    raise TimeoutError from exc_val
TimeoutError

I don't have wireshark packet

dlech commented 10 months ago

Is this still a problem if you use bleak from the develop branch?

NeverWe1come commented 10 months ago

Hi, thank you very much for the quick response, uninstall the version of bleak I have in use and install the version of the develop branch, but the failure continues.

Logs:

2023-08-14 20:28:44,771 __main__ INFO: starting scan...
2023-08-14 20:28:49,813 __main__ INFO: discovered 11 devices
2023-08-14 20:28:49,813 __main__ INFO: connecting to device D0:03:DF:E9:57:A9: None...
Traceback (most recent call last):
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\venv\Lib\site-packages\bleak\bleak\backends\winrt\client.py", line 435, in connect
    self.services = await self.get_services(
                    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\venv\Lib\site-packages\bleak\bleak\backends\winrt\client.py", line 663, in get_services
    await FutureLike(self._requester.get_gatt_services_async(*srv_args)),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\venv\Lib\site-packages\bleak\bleak\backends\winrt\client.py", line 1072, in __await__
    yield self  # This tells Task to wait for completion.
    ^^^^^^^^^^
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\venv\Lib\site-packages\bleak\bleak\backends\winrt\client.py", line 1012, in result
    raise asyncio.CancelledError
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\src\BLEScanDetect\pruabas_vuln.py", line 94, in <module>
    asyncio.run(main(args))
  File "C:\Users\vgarc\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\vgarc\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vgarc\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete  
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\src\BLEScanDetect\pruabas_vuln.py", line 67, in main
    await process_device(device, args)
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\src\BLEScanDetect\pruabas_vuln.py", line 13, in process_device
    async with BleakClient(
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\venv\Lib\site-packages\bleak\bleak\__init__.py", line 523, in __aenter__
    await self.connect()
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\venv\Lib\site-packages\bleak\bleak\__init__.py", line 563, in connect
    return await self._backend.connect(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vgarc\Desktop\TFM\ProgramaEscritorio\BLEScanDetect-Desktop\venv\Lib\site-packages\bleak\bleak\backends\winrt\client.py", line 391, in connect
    async with async_timeout(timeout):
  File "C:\Users\vgarc\AppData\Local\Programs\Python\Python311\Lib\asyncio\timeouts.py", line 111, in __aexit__
    raise TimeoutError from exc_val
TimeoutError
dlech commented 10 months ago

We would need wireshark logs for further insight. For example, it could be the same issue as #1359.

NeverWe1come commented 10 months ago

Yeah i have read that issue too, but the solve didnt work for me. You know any way to captura that problem or exception, to avoid that the rest of the execution finishes?

dlech commented 10 months ago

I would just make the timeout shorter or add an advertisement filter to avoid trying to connect to the problematic device in the first place. You could try to put each BleakClient in a separate task so they can run in parallel, but you would probably overload the Bluetooth adapter by trying to do too many things at the same time and cause other errors.

NeverWe1come commented 10 months ago

I have tried to get a wireshark capture of bluettoth interface, but it doesn't capture anything, so i can't give more info than my code and the problem. Talking about your avises, first advise, i think it doesn't work, but i will try, and the filter, it's not possible because i don't think its only one device (I don't know exactly but i don't think so). For the second advise, thats not good for my project, the code i hace post here is a small example, my project have another concurrent scanner so its complicated trying not to overload the Buetooth interface. THank you very much for your advises

NeverWe1come commented 10 months ago

Also i have another question. i don't understand why i scan the enviroment looking for BLE Devices, but when the scanner finished and try to connect to some devices, the responses are things like this:

2023-08-14 22:47:36,535 main INFO: connecting to device D4:9D:C0:3F:9C:CF: None... 2023-08-14 22:47:36,546 main ERROR: Error while processing device D4:9D:C0:3F:9C:CF: None: Device with address D4:9D:C0:3F:9C:CF was not found.

How its possible?

dlech commented 10 months ago

It was too long between the scan and the connect and the device went to sleep or something like that.

dlech commented 1 month ago

Is this still a problem with Bleak v0.22.0?