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: check scanner for abort #1548

Merged
merged 2 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 15 additions & 1 deletion bleak/backends/winrt/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
)

from ...assigned_numbers import AdvertisementDataType
from ...exc import BleakError
from ...uuids import normalize_uuid_str
from ..scanner import AdvertisementData, AdvertisementDataCallback, BaseBleakScanner

Expand Down Expand Up @@ -83,7 +84,7 @@ def __init__(
):
super(BleakScannerWinRT, self).__init__(detection_callback, service_uuids)

self.watcher = None
self.watcher: Optional[BluetoothLEAdvertisementWatcher] = None
self._advertisement_pairs: Dict[int, _RawAdvData] = {}
self._stopped_event = None

Expand Down Expand Up @@ -220,6 +221,9 @@ def _stopped_handler(self, sender, e):
self._stopped_event.set()

async def start(self) -> None:
if self.watcher:
raise BleakError("Scanner already started")

# start with fresh list of discovered devices
self.seen_devices = {}
self._advertisement_pairs.clear()
Expand All @@ -244,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
Loading