Skip to content

Commit 9200840

Browse files
committed
centralize backend detection
1 parent 2e7192c commit 9200840

File tree

4 files changed

+78
-28
lines changed

4 files changed

+78
-28
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0
1010
`Unreleased`_
1111
=============
1212

13+
Added
14+
-----
15+
- Added ``bleak.backends.get_backend()`` and ``BleakBackend`` enum for a centralized backend detection.
16+
1317
`1.1.1`_ (2025-09-07)
1418
=====================
1519

bleak/backends/__init__.py

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,58 @@
1-
# -*- coding: utf-8 -*-
2-
# Created on 2017-11-19 by hbldh <[email protected]>
3-
"""
4-
__init__.py
5-
"""
1+
import enum
2+
import os
3+
import platform
4+
import sys
5+
6+
from bleak.exc import BleakError
7+
8+
9+
class BleakBackend(enum.Enum):
10+
P4Android = enum.auto()
11+
"""
12+
Python for Android backend.
13+
"""
14+
15+
BlueZDBus = enum.auto()
16+
"""
17+
BlueZ D-Bus backend for Linux.
18+
"""
19+
20+
PythonistaCB = enum.auto()
21+
"""
22+
Pythonista CoreBluetooth backend for iOS and macOS.
23+
"""
24+
25+
CoreBluetooth = enum.auto()
26+
"""
27+
CoreBluetooth backend for macOS.
28+
"""
29+
30+
WinRT = enum.auto()
31+
"""
32+
Windows Runtime backend for Windows.
33+
"""
34+
35+
36+
def get_backend() -> BleakBackend:
37+
"""
38+
Returns the preferred backend for the current platform/environment.
39+
"""
40+
if os.environ.get("P4A_BOOTSTRAP") is not None:
41+
return BleakBackend.P4Android
42+
43+
if platform.system() == "Linux":
44+
return BleakBackend.BlueZDBus
45+
46+
if sys.platform == "ios" and "Pythonista3.app" in sys.executable:
47+
# Must be resolved before checking for "Darwin" (macOS),
48+
# as both the Pythonista app for iOS and macOS
49+
# return "Darwin" from platform.system()
50+
return BleakBackend.PythonistaCB
51+
52+
if platform.system() == "Darwin":
53+
return BleakBackend.CoreBluetooth
54+
55+
if platform.system() == "Windows":
56+
return BleakBackend.WinRT
57+
58+
raise BleakError(f"Unsupported platform: {platform.system()}")

bleak/backends/client.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
Base class for backend clients.
55
"""
66
import abc
7-
import os
8-
import platform
97
import sys
108
from collections.abc import Callable
119
from typing import Any, Optional, Union
@@ -15,6 +13,7 @@
1513
else:
1614
from collections.abc import Buffer
1715

16+
from bleak.backends import BleakBackend, get_backend
1817
from bleak.backends.characteristic import BleakGATTCharacteristic
1918
from bleak.backends.descriptor import BleakGATTDescriptor
2019
from bleak.backends.device import BLEDevice
@@ -213,20 +212,18 @@ def get_platform_client_backend_type() -> type[BaseBleakClient]:
213212
"""
214213
Gets the platform-specific :class:`BaseBleakClient` type.
215214
"""
216-
if os.environ.get("P4A_BOOTSTRAP") is not None:
215+
backend = get_backend()
216+
if backend == BleakBackend.P4Android:
217217
from bleak.backends.p4android.client import BleakClientP4Android
218218

219219
return BleakClientP4Android
220220

221-
if platform.system() == "Linux":
221+
if backend == BleakBackend.BlueZDBus:
222222
from bleak.backends.bluezdbus.client import BleakClientBlueZDBus
223223

224224
return BleakClientBlueZDBus
225225

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()
226+
if backend == BleakBackend.PythonistaCB:
230227
try:
231228
from bleak_pythonista import BleakClientPythonistaCB
232229

@@ -236,14 +233,14 @@ def get_platform_client_backend_type() -> type[BaseBleakClient]:
236233
"Ensure you have `bleak-pythonista` package installed."
237234
) from e
238235

239-
if platform.system() == "Darwin":
236+
if backend == BleakBackend.CoreBluetooth:
240237
from bleak.backends.corebluetooth.client import BleakClientCoreBluetooth
241238

242239
return BleakClientCoreBluetooth
243240

244-
if platform.system() == "Windows":
241+
if backend == BleakBackend.WinRT:
245242
from bleak.backends.winrt.client import BleakClientWinRT
246243

247244
return BleakClientWinRT
248245

249-
raise BleakError(f"Unsupported platform: {platform.system()}")
246+
raise BleakError(f"Unsupported backend: {backend}")

bleak/backends/scanner.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import abc
22
import asyncio
33
import inspect
4-
import os
5-
import platform
6-
import sys
74
from collections.abc import Callable, Coroutine, Hashable
85
from typing import Any, NamedTuple, Optional
96

7+
from bleak.backends import BleakBackend, get_backend
108
from bleak.backends.device import BLEDevice
119
from bleak.exc import BleakError
1210

@@ -286,20 +284,18 @@ def get_platform_scanner_backend_type() -> type[BaseBleakScanner]:
286284
"""
287285
Gets the platform-specific :class:`BaseBleakScanner` type.
288286
"""
289-
if os.environ.get("P4A_BOOTSTRAP") is not None:
287+
backend = get_backend()
288+
if backend == BleakBackend.P4Android:
290289
from bleak.backends.p4android.scanner import BleakScannerP4Android
291290

292291
return BleakScannerP4Android
293292

294-
if platform.system() == "Linux":
293+
if backend == BleakBackend.BlueZDBus:
295294
from bleak.backends.bluezdbus.scanner import BleakScannerBlueZDBus
296295

297296
return BleakScannerBlueZDBus
298297

299-
if sys.platform == "ios" and "Pythonista3.app" in sys.executable:
300-
# Must be resolved before checking for "Darwin" (macOS),
301-
# as both the Pythonista app for iOS and macOS
302-
# return "Darwin" from platform.system()
298+
if backend == BleakBackend.PythonistaCB:
303299
try:
304300
from bleak_pythonista import BleakScannerPythonistaCB
305301

@@ -309,14 +305,14 @@ def get_platform_scanner_backend_type() -> type[BaseBleakScanner]:
309305
"Ensure you have `bleak-pythonista` package installed."
310306
) from e
311307

312-
if platform.system() == "Darwin":
308+
if backend == BleakBackend.CoreBluetooth:
313309
from bleak.backends.corebluetooth.scanner import BleakScannerCoreBluetooth
314310

315311
return BleakScannerCoreBluetooth
316312

317-
if platform.system() == "Windows":
313+
if backend == BleakBackend.WinRT:
318314
from bleak.backends.winrt.scanner import BleakScannerWinRT
319315

320316
return BleakScannerWinRT
321317

322-
raise BleakError(f"Unsupported platform: {platform.system()}")
318+
raise BleakError(f"Unsupported backend: {backend}")

0 commit comments

Comments
 (0)