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

winrt: No advertisments received due to BluetoothLEAdvertisementWatcherStatus.ABORTED #1535

Closed
Jackiii1989 opened this issue Apr 9, 2024 · 5 comments · Fixed by #1548
Closed
Labels
Backend: WinRT Issues or PRs relating to the WinRT backend bug Something isn't working

Comments

@Jackiii1989
Copy link

Jackiii1989 commented Apr 9, 2024

Description

I used the bleak library yesterday and everything worked fine. I tried it again today and bleak does not find any BLE devices in the area, even though there are tons of BLE devices. I did a Windows update. Maybe that explains the problem. Am I doing something wrong? Thanks for the help.

  • bleak version:
    bleak 0.21.1
    numpy 1.26.4
    pip 23.2.1
    winrt-runtime 2.0.0b1
    winrt-Windows.Devices.Bluetooth 2.0.0b1
    winrt-Windows.Devices.Bluetooth.Advertisement 2.0.0b1
    winrt-Windows.Devices.Bluetooth.GenericAttributeProfile 2.0.0b1
    winrt-Windows.Devices.Enumeration 2.0.0b1
    winrt-Windows.Foundation 2.0.0b1
    winrt-Windows.Foundation.Collections 2.0.0b1
    winrt-Windows.Storage.Streams 2.0.0b1

  • Python version:
    Python 3.12.1

  • ** Operating System:**
    OS Name: Microsoft Windows 11 Pro
    OS Version: 10.0.22631 N/A Build 22631

What I Did

def test_bleak():

    import logging
    from bleak import BleakScanner
    from bleak.backends.device import BLEDevice
    from bleak.backends.scanner import AdvertisementData

    logger = logging.getLogger(__name__)

    logging.basicConfig(
        level=logging.DEBUG,
        format="%(asctime)-15s %(name)-8s %(levelname)s: %(message)s",
    )

    async def main1():
        logger.info("starting scanner example 1")
        devices = await BleakScanner.discover()
        for d in devices:
            logger.info(d)

    async def main2():

        def simple_callback(device: BLEDevice, advertisement_data: AdvertisementData):
            logger.info("%s: %r", device.address, advertisement_data)

        scanner = BleakScanner(
            simple_callback
        )

        logger.info("starting scanner example 2")
        await scanner.start()
        await asyncio.sleep(19.0)
        await scanner.stop()

    asyncio.run(main1())
    asyncio.run(main2())

    logger.info("Example finished")


if __name__ == '__main__':
    test_bleak()

Logs

(.venv) PS C:\python_workspace\test> $env:BLEAK_LOGGING=1                                           
(.venv) PS C:\python_workspace\test> python.exe .\main.py                                           
2024-04-09 11:57:11,660 asyncio  DEBUG: Using proactor: IocpProactor
2024-04-09 11:57:11,661 __main__ INFO: starting scanner example 1
2024-04-09 11:57:11,784 bleak.backends.winrt.scanner MainThread DEBUG: 0 devices found. Watcher status: <BluetoothLEAdvertisementWatcherStatus.ABORTED: 4>.
2024-04-09 11:57:11,784 bleak.backends.winrt.scanner DEBUG: 0 devices found. Watcher status: <BluetoothLEAdvertisementWatcherStatus.ABORTED: 4>.
2024-04-09 11:57:16,774 bleak.backends.winrt.scanner MainThread DEBUG: skipping waiting for stop because status is <BluetoothLEAdvertisementWatcherStatus.ABORTED: 4>
2024-04-09 11:57:16,774 bleak.backends.winrt.scanner DEBUG: skipping waiting for stop because status is <BluetoothLEAdvertisementWatcherStatus.ABORTED: 4>
2024-04-09 11:57:16,775 asyncio  DEBUG: Using proactor: IocpProactor
2024-04-09 11:57:16,775 __main__ INFO: starting scanner example 2
2024-04-09 11:57:16,776 bleak.backends.winrt.scanner MainThread DEBUG: 0 devices found. Watcher status: <BluetoothLEAdvertisementWatcherStatus.ABORTED: 4>.
2024-04-09 11:57:16,776 bleak.backends.winrt.scanner DEBUG: 0 devices found. Watcher status: <BluetoothLEAdvertisementWatcherStatus.ABORTED: 4>.

This is what I see on Nordic's nRF Connect app:

image

@Jackiii1989 Jackiii1989 changed the title Scannner does not return any BLE devices [backends: winrt] Scannner does not return any BLE devices [backend: winrt] Apr 9, 2024
@dlech dlech added 3rd party issue The issue is with the Bluetooth stack, the BLE device, or other 3rd party code not with Bleak itself Backend: WinRT Issues or PRs relating to the WinRT backend bug Something isn't working and removed 3rd party issue The issue is with the Bluetooth stack, the BLE device, or other 3rd party code not with Bleak itself labels Apr 9, 2024
@dlech
Copy link
Collaborator

dlech commented Apr 9, 2024

asyncio.run(main1())
asyncio.run(main2())

There should only be one asyncio.run() per application. Otherwise things can break easily. Doesn't cause the problem you are seeing though.

Watcher status: <BluetoothLEAdvertisementWatcherStatus.ABORTED: 4>.

This indicates that there is something wrong with your Bluetooth adapter. E.g. it is disabled or something like that.

We should probably catch this and raise and exception instead of failing silently with no advertisements received.

So, yes, likely something related to Windows update or just have Bluetooth switch turned off. But there is some improvement we could make to Bleak to better help diagnose such issues.

@Jackiii1989
Copy link
Author

Hey,

Thanks for the ideas on how to write it better. The problem seemed to be on the Windows side. Now it works again.

We should probably catch this and raise and exception instead of failing silently with no advertisements received.

For my side that would be helpful, or an error message then I can narrow down my problem next.

Cheers,

@dlech
Copy link
Collaborator

dlech commented Apr 10, 2024

For my side that would be helpful, or an error message then I can narrow down my problem next.

Let's leave this one open to remind us to do that.

@dlech dlech reopened this Apr 10, 2024
@dlech dlech changed the title Scannner does not return any BLE devices [backend: winrt] winrt: No advertisments received due to BluetoothLEAdvertisementWatcherStatus.ABORTED Apr 10, 2024
@dlech
Copy link
Collaborator

dlech commented Apr 10, 2024

The problem seemed to be on the Windows side.

If the problem is reproducible, knowing how to reproduce it for testing would be helpful.

@Jackiii1989
Copy link
Author

If the problem is reproducible, knowing how to reproduce it for testing would be helpful.

I do not know how to reproduce the problem. After the second Windows Update, the bug was gone.

dlech added a commit that referenced this issue Apr 28, 2024
If Bluetooth is off or not present, the status will be ABORTED. We can
use this to give a helpful error message.

Also wait some time for the status to change to STARTED before returning.

Fixes: #1535
@dlech dlech linked a pull request Apr 28, 2024 that will close this issue
dlech added a commit that referenced this issue Apr 28, 2024
If Bluetooth is off or not present, the status will be ABORTED. We can
use this to give a helpful error message.

Also wait some time for the status to change to STARTED before returning.

Fixes: #1535
@dlech dlech mentioned this issue May 4, 2024
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 bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants