Skip to content

Commit

Permalink
backends/winrt: check watcher status after starting
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dlech committed Apr 28, 2024
1 parent 7aa5d77 commit 9dfd3a3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Fixed
* Fixed ``KeyError`` in BlueZ ``is_connected()`` and ``get_global_bluez_manager()`` when device is not present. Fixes #1507.
* Fixed BlueZ ``_wait_removed`` completion on invalid object path. Fixes #1489.
* Fixed rare unhandled exception when scanning on macOS when using ``use_bdaddr``. Fixes #1523.
* Fixed scanning silently failing on Windows when Bluetooth is off. Fixes #1535.

`0.21.1`_ (2023-09-08)
======================
Expand Down
10 changes: 10 additions & 0 deletions bleak/backends/winrt/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,16 @@ async def start(self) -> None:

self.watcher.start()

# no events for status changes, so we have to poll :-(
while self.watcher.status == BluetoothLEAdvertisementWatcherStatus.CREATED:
await asyncio.sleep(0.01)

if self.watcher.status == BluetoothLEAdvertisementWatcherStatus.ABORTED:
raise BleakError("Failed to start scanner. Is Bluetooth turned on?")

if self.watcher.status != BluetoothLEAdvertisementWatcherStatus.STARTED:
raise BleakError(f"Unexpected watcher status: {self.watcher.status.name}")

async def stop(self) -> None:
self.watcher.stop()

Expand Down

0 comments on commit 9dfd3a3

Please sign in to comment.