|
4 | 4 | Base class for backend clients. |
5 | 5 | """ |
6 | 6 | import abc |
7 | | -import os |
8 | | -import platform |
9 | 7 | import sys |
10 | 8 | from collections.abc import Callable |
11 | 9 | from typing import Any, Optional, Union |
|
15 | 13 | else: |
16 | 14 | from collections.abc import Buffer |
17 | 15 |
|
| 16 | +from bleak.backends import BleakBackend, get_default_backend |
18 | 17 | from bleak.backends.characteristic import BleakGATTCharacteristic |
19 | 18 | from bleak.backends.descriptor import BleakGATTDescriptor |
20 | 19 | from bleak.backends.device import BLEDevice |
@@ -209,41 +208,41 @@ async def stop_notify(self, characteristic: BleakGATTCharacteristic) -> None: |
209 | 208 | raise NotImplementedError() |
210 | 209 |
|
211 | 210 |
|
212 | | -def get_platform_client_backend_type() -> type[BaseBleakClient]: |
| 211 | +def get_platform_client_backend_type() -> tuple[type[BaseBleakClient], BleakBackend]: |
213 | 212 | """ |
214 | 213 | Gets the platform-specific :class:`BaseBleakClient` type. |
215 | 214 | """ |
216 | | - if os.environ.get("P4A_BOOTSTRAP") is not None: |
217 | | - from bleak.backends.p4android.client import BleakClientP4Android |
| 215 | + backend = get_default_backend() |
| 216 | + match backend: |
| 217 | + case BleakBackend.P4ANDROID: |
| 218 | + from bleak.backends.p4android.client import BleakClientP4Android |
218 | 219 |
|
219 | | - return BleakClientP4Android |
| 220 | + return (BleakClientP4Android, backend) |
220 | 221 |
|
221 | | - if platform.system() == "Linux": |
222 | | - from bleak.backends.bluezdbus.client import BleakClientBlueZDBus |
| 222 | + case BleakBackend.BLUEZ_DBUS: |
| 223 | + from bleak.backends.bluezdbus.client import BleakClientBlueZDBus |
223 | 224 |
|
224 | | - return BleakClientBlueZDBus |
| 225 | + return (BleakClientBlueZDBus, backend) |
225 | 226 |
|
226 | | - if sys.platform == "ios" and "Pythonista3.app" in sys.executable: |
227 | | - # Must be resolved before checking for "Darwin" (macOS), |
228 | | - # as both the Pythonista app for iOS and macOS |
229 | | - # return "Darwin" from platform.system() |
230 | | - try: |
231 | | - from bleak_pythonista import BleakClientPythonistaCB |
| 227 | + case BleakBackend.PYTHONISTA_CB: |
| 228 | + try: |
| 229 | + from bleak_pythonista import BleakClientPythonistaCB |
232 | 230 |
|
233 | | - return BleakClientPythonistaCB |
234 | | - except ImportError as e: |
235 | | - raise ImportError( |
236 | | - "Ensure you have `bleak-pythonista` package installed." |
237 | | - ) from e |
| 231 | + return (BleakClientPythonistaCB, backend) |
| 232 | + except ImportError as e: |
| 233 | + raise ImportError( |
| 234 | + "Ensure you have `bleak-pythonista` package installed." |
| 235 | + ) from e |
238 | 236 |
|
239 | | - if platform.system() == "Darwin": |
240 | | - from bleak.backends.corebluetooth.client import BleakClientCoreBluetooth |
| 237 | + case BleakBackend.CORE_BLUETOOTH: |
| 238 | + from bleak.backends.corebluetooth.client import BleakClientCoreBluetooth |
241 | 239 |
|
242 | | - return BleakClientCoreBluetooth |
| 240 | + return (BleakClientCoreBluetooth, backend) |
243 | 241 |
|
244 | | - if platform.system() == "Windows": |
245 | | - from bleak.backends.winrt.client import BleakClientWinRT |
| 242 | + case BleakBackend.WIN_RT: |
| 243 | + from bleak.backends.winrt.client import BleakClientWinRT |
246 | 244 |
|
247 | | - return BleakClientWinRT |
| 245 | + return (BleakClientWinRT, backend) |
248 | 246 |
|
249 | | - raise BleakError(f"Unsupported platform: {platform.system()}") |
| 247 | + case _: |
| 248 | + raise BleakError(f"Unsupported backend: {backend}") |
0 commit comments