diff options
| -rw-r--r-- | bin/bt_helper.py | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/bin/bt_helper.py b/bin/bt_helper.py index fc87b4e6..91f879af 100644 --- a/bin/bt_helper.py +++ b/bin/bt_helper.py @@ -19,10 +19,11 @@ typical Bluetooth task like scanning for devices and pairing with them. It talks with BlueZ stack using dbus. """ +import logging + import dbus import dbus.service import dbus.mainloop.glib -import logging from gi.repository import GObject logger = logging.getLogger(__file__) @@ -35,10 +36,16 @@ AGENT_IFACE = 'org.bluez.Agent1' dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) +# To get additional Bluetoot CoDs, check +# https://www.bluetooth.com/specifications/assigned-numbers/baseband BT_ANY = 0 BT_KEYBOARD = int('0x2540', 16) +class BtException(Exception): + pass + + class BtManager: """ Main point of contact with dbus factoring bt objects. """ def __init__(self, verbose=False): @@ -113,7 +120,7 @@ class BtManager: def wait(self): self._main_loop.run() - def resume(self): + def quit_loop(self): self._main_loop.quit() def ensure_adapters_powered(self): @@ -185,6 +192,7 @@ class BtDevice: self._if.object_path)[DEVICE_IFACE] self._bt_mgr = bt_mgr self._prop_if = bt_mgr.get_prop_iface(dbus_iface) + self._pair_outcome = None def __str__(self): return "{} ({})".format(self.name, self.address) @@ -203,6 +211,8 @@ class BtDevice: self._if.Pair( reply_handler=self._pair_ok, error_handler=self._pair_error) self._bt_mgr.wait() + if self._pair_outcome: + raise BtException(self._pair_outcome) try: self._if.Connect() except dbus.exceptions.DBusException as exc: @@ -228,11 +238,13 @@ class BtDevice: def _pair_ok(self): logger.info('%s successfully paired', self.name) - self._bt_mgr.resume() + self._pair_outcome = None + self._bt_mgr.quit_loop() def _pair_error(self, error): logger.warning('Pairing of %s device failed. %s', self.name, error) - self._bt_mgr.resume() + self._pair_outcome = error + self._bt_mgr.quit_loop() class Rejected(dbus.DBusException): @@ -240,7 +252,7 @@ class Rejected(dbus.DBusException): class BtAgent(dbus.service.Object): - """Agent authenticating everything what's possible.""" + """Agent authenticating everything that is possible.""" @dbus.service.method(AGENT_IFACE, in_signature="os", out_signature="") def AuthorizeService(self, device, uuid): logger.info("AuthorizeService (%s, %s)", device, uuid) |
