-
Notifications
You must be signed in to change notification settings - Fork 334
Centralize backend detection #1840
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9200840
centralize backend detection
timrid 3c8d50a
Merge branch 'develop' into centralize-backend-detection
dlech 409558a
rename `get_backend` to `get_default_backend`
timrid 9d7d64b
Added `backend_id`
timrid a911f53
StrEnum is only availabe since 3.11
timrid 935a5a8
changed example to use the new `backend_id`
timrid 0d442e8
Update docs + use UPPER_CAMEL_CASE for enum + use match case
timrid cf221a0
fixed remaining enum
timrid 3eab18c
Merge remote-tracking branch 'origin/develop' into pr/timrid/1840
dlech 7d72bf9
update docs for new backend detection
dlech 8e04019
Update CHANGELOG.rst
dlech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,75 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # Created on 2017-11-19 by hbldh <[email protected]> | ||
| """ | ||
| __init__.py | ||
| Communicating with Bluetooth hardware requires calling OS-specific APIs. These | ||
| are abstracted as "backends" in Bleak. | ||
|
|
||
| The backend will be automatically selected based on the operating system Bleak | ||
| is running on. In some cases, this may also depend on a specific runtime, like | ||
| Pythonista on iOS. | ||
| """ | ||
|
|
||
| import enum | ||
| import os | ||
| import platform | ||
| import sys | ||
|
|
||
| from bleak.exc import BleakError | ||
|
|
||
|
|
||
| class BleakBackend(str, enum.Enum): | ||
timrid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Identifiers for available built-in Bleak backends. | ||
|
|
||
| .. versionadded:: unreleased | ||
| """ | ||
|
|
||
| P4ANDROID = "p4android" | ||
| """ | ||
| Python for Android backend. | ||
| """ | ||
|
|
||
| BLUEZ_DBUS = "bluez_dbus" | ||
| """ | ||
| BlueZ D-Bus backend for Linux. | ||
| """ | ||
|
|
||
| PYTHONISTA_CB = "pythonista_cb" | ||
| """ | ||
| Pythonista CoreBluetooth backend for iOS and macOS. | ||
| """ | ||
|
|
||
| CORE_BLUETOOTH = "core_bluetooth" | ||
| """ | ||
| CoreBluetooth backend for macOS. | ||
| """ | ||
|
|
||
| WIN_RT = "win_rt" | ||
| """ | ||
| Windows Runtime backend for Windows. | ||
| """ | ||
|
|
||
|
|
||
| def get_default_backend() -> BleakBackend: | ||
| """ | ||
| Returns the preferred backend for the current platform/environment. | ||
|
|
||
| .. versionadded:: unreleased | ||
| """ | ||
| if os.environ.get("P4A_BOOTSTRAP") is not None: | ||
| return BleakBackend.P4ANDROID | ||
|
|
||
| if platform.system() == "Linux": | ||
| return BleakBackend.BLUEZ_DBUS | ||
|
|
||
| if sys.platform == "ios" and "Pythonista3.app" in sys.executable: | ||
| # Must be resolved before checking for "Darwin" (macOS), | ||
| # as both the Pythonista app for iOS and macOS | ||
| # return "Darwin" from platform.system() | ||
| return BleakBackend.PYTHONISTA_CB | ||
|
|
||
| if platform.system() == "Darwin": | ||
| return BleakBackend.CORE_BLUETOOTH | ||
|
|
||
| if platform.system() == "Windows": | ||
| return BleakBackend.WIN_RT | ||
|
|
||
| raise BleakError(f"Unsupported platform: {platform.system()}") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.