Skip to content

Commit f5f38c6

Browse files
authored
Merge pull request #166 from kevincar/121-friendlier-warning-if-bluez-is-not-installed-on-linux
121 friendlier warning if bluez is not installed on linux
2 parents 99ba836 + 5f45985 commit f5f38c6

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

bless/backends/bluezdbus/dbus/application.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import logging
12
import re
3+
import shutil
4+
import warnings
25

36
import bleak.backends.bluezdbus.defs as defs # type: ignore
47

@@ -20,6 +23,8 @@
2023
)
2124
from bless.backends.bluezdbus.dbus.descriptor import BlueZGattDescriptor # type: ignore
2225

26+
LOGGER = logging.getLogger(__name__)
27+
2328

2429
class BlueZGattApplication(ServiceInterface):
2530
"""
@@ -39,6 +44,7 @@ def __init__(self, name: str, destination: str, bus: MessageBus):
3944
bus : MessageBus
4045
The dbus_next connection
4146
"""
47+
self._ensure_bluez_available()
4248
self.path: str = "/"
4349
self.app_name: str = name
4450
self.destination: str = destination
@@ -68,6 +74,20 @@ def __init__(self, name: str, destination: str, bus: MessageBus):
6874

6975
super(BlueZGattApplication, self).__init__(self.destination)
7076

77+
@staticmethod
78+
def _ensure_bluez_available() -> None:
79+
if shutil.which("bluetoothd") or shutil.which("bluetoothctl"):
80+
return
81+
message = (
82+
"BlueZ utilities not found (bluetoothd/bluetoothctl). "
83+
"Install BlueZ to use the Linux backend. On Debian/Ubuntu: "
84+
"`sudo apt-get install bluez`. On Raspberry Pi, you may also want "
85+
"`pi-bluetooth`."
86+
)
87+
warnings.warn(message, RuntimeWarning)
88+
LOGGER.warning(message)
89+
raise RuntimeError("BlueZ is not available on this system")
90+
7191
async def add_service(self, uuid: str) -> BlueZGattService: # noqa: F821
7292
"""
7393
Add a service to the application

0 commit comments

Comments
 (0)