Skip to content

Commit 1ca33bd

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

File tree

4 files changed

+59
-38
lines changed

4 files changed

+59
-38
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: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
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+
BlueZDBus = enum.auto()
12+
PythonistaCB = enum.auto()
13+
CoreBluetooth = enum.auto()
14+
WinRT = enum.auto()
15+
16+
17+
def get_backend() -> BleakBackend:
18+
"""Detect the backend to use for the current platform."""
19+
if os.environ.get("P4A_BOOTSTRAP") is not None:
20+
return BleakBackend.P4Android
21+
22+
if platform.system() == "Linux":
23+
return BleakBackend.BlueZDBus
24+
25+
if sys.platform == "ios" and "Pythonista3.app" in sys.executable:
26+
# Must be resolved before checking for "Darwin" (macOS),
27+
# as both the Pythonista app for iOS and macOS
28+
# return "Darwin" from platform.system()
29+
return BleakBackend.PythonistaCB
30+
31+
if platform.system() == "Darwin":
32+
return BleakBackend.CoreBluetooth
33+
34+
if platform.system() == "Windows":
35+
return BleakBackend.WinRT
36+
37+
raise BleakError(f"Unsupported platform: {platform.system()}")

bleak/backends/client.py

Lines changed: 9 additions & 16 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,16 @@ 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
220-
221-
if platform.system() == "Linux":
220+
elif backend == BleakBackend.BlueZDBus:
222221
from bleak.backends.bluezdbus.client import BleakClientBlueZDBus
223222

224223
return BleakClientBlueZDBus
225-
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()
224+
elif backend == BleakBackend.PythonistaCB:
230225
try:
231226
from bleak_pythonista import BleakClientPythonistaCB
232227

@@ -235,15 +230,13 @@ def get_platform_client_backend_type() -> type[BaseBleakClient]:
235230
raise ImportError(
236231
"Ensure you have `bleak-pythonista` package installed."
237232
) from e
238-
239-
if platform.system() == "Darwin":
233+
elif backend == BleakBackend.CoreBluetooth:
240234
from bleak.backends.corebluetooth.client import BleakClientCoreBluetooth
241235

242236
return BleakClientCoreBluetooth
243-
244-
if platform.system() == "Windows":
237+
elif backend == BleakBackend.WinRT:
245238
from bleak.backends.winrt.client import BleakClientWinRT
246239

247240
return BleakClientWinRT
248-
249-
raise BleakError(f"Unsupported platform: {platform.system()}")
241+
else:
242+
raise BleakError(f"Unsupported backend: {backend}")

bleak/backends/scanner.py

Lines changed: 9 additions & 17 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,16 @@ 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
293-
294-
if platform.system() == "Linux":
292+
elif backend == BleakBackend.BlueZDBus:
295293
from bleak.backends.bluezdbus.scanner import BleakScannerBlueZDBus
296294

297295
return BleakScannerBlueZDBus
298-
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()
296+
elif backend == BleakBackend.PythonistaCB:
303297
try:
304298
from bleak_pythonista import BleakScannerPythonistaCB
305299

@@ -308,15 +302,13 @@ def get_platform_scanner_backend_type() -> type[BaseBleakScanner]:
308302
raise ImportError(
309303
"Ensure you have `bleak-pythonista` package installed."
310304
) from e
311-
312-
if platform.system() == "Darwin":
305+
elif backend == BleakBackend.CoreBluetooth:
313306
from bleak.backends.corebluetooth.scanner import BleakScannerCoreBluetooth
314307

315308
return BleakScannerCoreBluetooth
316-
317-
if platform.system() == "Windows":
309+
elif backend == BleakBackend.WinRT:
318310
from bleak.backends.winrt.scanner import BleakScannerWinRT
319311

320312
return BleakScannerWinRT
321-
322-
raise BleakError(f"Unsupported platform: {platform.system()}")
313+
else:
314+
raise BleakError(f"Unsupported backend: {backend}")

0 commit comments

Comments
 (0)