3333from bleak .args .bluez import BlueZScannerArgs
3434from bleak .args .corebluetooth import CBScannerArgs , CBStartNotifyArgs
3535from bleak .args .winrt import WinRTClientArgs
36+ from bleak .backends import BleakBackend
3637from bleak .backends .characteristic import BleakGATTCharacteristic
3738from bleak .backends .client import BaseBleakClient , get_platform_client_backend_type
3839from bleak .backends .descriptor import BleakGATTDescriptor
6061
6162
6263# prevent tasks from being garbage collected
63- _background_tasks = set [asyncio .Task [None ]]()
64+ _background_tasks : set [asyncio .Task [None ]] = set ()
6465
6566
6667class BleakScanner :
@@ -123,8 +124,10 @@ def __init__(
123124 backend : Optional [type [BaseBleakScanner ]] = None ,
124125 ** kwargs : Any ,
125126 ) -> None :
126- PlatformBleakScanner = (
127- get_platform_scanner_backend_type () if backend is None else backend
127+ PlatformBleakScanner , backend_id = (
128+ get_platform_scanner_backend_type ()
129+ if backend is None
130+ else (backend , backend .__name__ )
128131 )
129132
130133 self ._backend = PlatformBleakScanner (
@@ -135,6 +138,19 @@ def __init__(
135138 cb = cb ,
136139 ** kwargs ,
137140 ) # type: ignore
141+ self ._backend_id = backend_id
142+
143+ @property
144+ def backend_id (self ) -> BleakBackend | str :
145+ """
146+ Gets the identifier of the backend in use.
147+
148+ The value is one of the :class:`BleakBackend` enum values in case of
149+ built-in backends, or a string identifying a custom backend.
150+
151+ .. versionadded:: 2.0
152+ """
153+ return self ._backend_id
138154
139155 async def __aenter__ (self ) -> Self :
140156 await self ._backend .start ()
@@ -149,7 +165,17 @@ async def __aexit__(
149165 await self ._backend .stop ()
150166
151167 async def start (self ) -> None :
152- """Start scanning for devices"""
168+ """
169+ Start scanning for devices.
170+
171+ Raises:
172+ BleakBluetoothNotAvailableError:
173+ if Bluetooth is not currently available
174+
175+ .. versionchanged:: 2.0
176+ Now raises :class:`BleakBluetoothNotAvailableError` instead of :class:`BleakError`
177+ when Bluetooth is not currently available.
178+ """
153179 await self ._backend .start ()
154180
155181 async def stop (self ) -> None :
@@ -170,7 +196,7 @@ async def advertisement_data(
170196
171197 .. versionadded:: 0.21
172198 """
173- devices = asyncio .Queue [tuple [BLEDevice , AdvertisementData ]]()
199+ devices : asyncio .Queue [tuple [BLEDevice , AdvertisementData ]] = asyncio . Queue ()
174200
175201 unregister_callback = self ._backend .register_detection_callback (
176202 lambda bd , ad : devices .put_nowait ((bd , ad ))
@@ -490,8 +516,10 @@ def __init__(
490516 backend : Optional [type [BaseBleakClient ]] = None ,
491517 ** kwargs : Any ,
492518 ) -> None :
493- PlatformBleakClient = (
494- get_platform_client_backend_type () if backend is None else backend
519+ PlatformBleakClient , backend_id = (
520+ get_platform_client_backend_type ()
521+ if backend is None
522+ else (backend , backend .__name__ )
495523 )
496524
497525 self ._backend = PlatformBleakClient (
@@ -509,6 +537,19 @@ def __init__(
509537 ** kwargs ,
510538 )
511539 self ._pair_before_connect = pair
540+ self ._backend_id = backend_id
541+
542+ @property
543+ def backend_id (self ) -> BleakBackend | str :
544+ """
545+ Gets the identifier of the backend in use.
546+
547+ The value is one of the :class:`BleakBackend` enum values in case of
548+ built-in backends, or a string identifying a custom backend.
549+
550+ .. versionadded:: 2.0
551+ """
552+ return self ._backend_id
512553
513554 # device info
514555
@@ -523,6 +564,8 @@ def name(self) -> str:
523564 a Bluetooth address separated with dashes (``-``) instead of colons
524565 (``:``) (or a UUID on Apple devices). It may also be possible to override
525566 the device name using the OS's Bluetooth settings.
567+
568+ .. versionadded:: 1.1
526569 """
527570 return self ._backend .name
528571
@@ -663,7 +706,7 @@ async def read_gatt_char(
663706 The read data.
664707
665708 Raises:
666- BleakGattCharacteristicNotFoundError : if a characteristic with the
709+ BleakCharacteristicNotFoundError : if a characteristic with the
667710 handle or UUID specified by ``char_specifier`` could not be found.
668711 backend-specific exceptions: if the read operation failed.
669712 """
@@ -712,7 +755,7 @@ async def write_gatt_char(
712755 property, which is why an explicit argument is encouraged.
713756
714757 Raises:
715- BleakGattCharacteristicNotFoundError : if a characteristic with the
758+ BleakCharacteristicNotFoundError : if a characteristic with the
716759 handle or UUID specified by ``char_specifier`` could not be found.
717760 backend-specific exceptions: if the write operation failed.
718761
@@ -771,7 +814,7 @@ def callback(sender: BleakGATTCharacteristic, data: bytearray):
771814 CoreBluetooth specific arguments.
772815
773816 Raises:
774- BleakGattCharacteristicNotFoundError : if a characteristic with the
817+ BleakCharacteristicNotFoundError : if a characteristic with the
775818 handle or UUID specified by ``char_specifier`` could not be found.
776819 backend-specific exceptions: if the start notification operation failed.
777820
@@ -813,7 +856,7 @@ async def stop_notify(
813856 BleakGATTCharacteristic object representing it.
814857
815858 Raises:
816- BleakGattCharacteristicNotFoundError : if a characteristic with the
859+ BleakCharacteristicNotFoundError : if a characteristic with the
817860 handle or UUID specified by ``char_specifier`` could not be found.
818861 backend-specific exceptions: if the stop notification operation failed.
819862
0 commit comments