aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--parrot_zik/bluetooth_paired_devices.py42
-rw-r--r--parrot_zik/bluezutils.py48
2 files changed, 76 insertions, 14 deletions
diff --git a/parrot_zik/bluetooth_paired_devices.py b/parrot_zik/bluetooth_paired_devices.py
index 626c5e6..edeb5f4 100644
--- a/parrot_zik/bluetooth_paired_devices.py
+++ b/parrot_zik/bluetooth_paired_devices.py
@@ -4,6 +4,9 @@ from subprocess import Popen, PIPE, STDOUT
from .resource_manager import GenericResourceManager
+import dbus
+from . import bluezutils
+
if sys.platform == "darwin":
from binplist import binplist
import lightblue
@@ -29,26 +32,37 @@ class BluetoothDeviceManager(object):
class BluezBluetoothDeviceManager(BluetoothDeviceManager):
def is_bluetooth_on(self):
- pipe = Popen(['bluez-test-adapter', 'powered'], stdout=PIPE, stdin=PIPE,
- stderr=STDOUT)
- try:
- stdout, stderr = pipe.communicate()
- except dbus.exceptions.DBusException:
- pass
- else:
- return bool(stdout.strip())
+ bus = dbus.SystemBus()
+ adapter_path = bluezutils.find_adapter().object_path
+ adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path), "org.freedesktop.DBus.Properties")
+
+ return adapter.Get("org.bluez.Adapter1", "Powered")
+
def get_mac(self):
- pipe = Popen(['bluez-test-device', 'list'], stdout=PIPE, stdin=PIPE,
- stderr=STDOUT)
+ addresses = []
try:
- stdout, stderr = pipe.communicate()
+ bus = dbus.SystemBus()
+ adapter = bluezutils.find_adapter()
+ adapter_path = adapter.object_path
+
+ om = dbus.Interface(bus.get_object("org.bluez", "/"),
+ "org.freedesktop.DBus.ObjectManager")
+ objects = om.GetManagedObjects()
+
+ for path, interfaces in objects.items():
+ if "org.bluez.Device1" not in interfaces:
+ continue
+ properties = interfaces["org.bluez.Device1"]
+ if properties["Adapter"] != adapter_path:
+ continue;
+ addresses.append(properties)
except dbus.exceptions.DBusException:
pass
else:
- res = p.findall(stdout)
- if len(res) > 0:
- return res[0]
+ res = next(item for item in addresses if re.search(p, item["Address"]))
+ if res['Connected']:
+ return res['Address']
else:
raise DeviceNotConnected
diff --git a/parrot_zik/bluezutils.py b/parrot_zik/bluezutils.py
new file mode 100644
index 0000000..6f49f2b
--- /dev/null
+++ b/parrot_zik/bluezutils.py
@@ -0,0 +1,48 @@
+import dbus
+
+SERVICE_NAME = "org.bluez"
+ADAPTER_INTERFACE = SERVICE_NAME + ".Adapter1"
+DEVICE_INTERFACE = SERVICE_NAME + ".Device1"
+
+def get_managed_objects():
+ bus = dbus.SystemBus()
+ manager = dbus.Interface(bus.get_object("org.bluez", "/"),
+ "org.freedesktop.DBus.ObjectManager")
+ return manager.GetManagedObjects()
+
+def find_adapter(pattern=None):
+ return find_adapter_in_objects(get_managed_objects(), pattern)
+
+def find_adapter_in_objects(objects, pattern=None):
+ bus = dbus.SystemBus()
+ for path, ifaces in objects.items():
+ adapter = ifaces.get(ADAPTER_INTERFACE)
+ if adapter is None:
+ continue
+ if not pattern or pattern == adapter["Address"] or \
+ path.endswith(pattern):
+ obj = bus.get_object(SERVICE_NAME, path)
+ return dbus.Interface(obj, ADAPTER_INTERFACE)
+ raise Exception("Bluetooth adapter not found")
+
+def find_device(device_address, adapter_pattern=None):
+ return find_device_in_objects(get_managed_objects(), device_address,
+ adapter_pattern)
+
+def find_device_in_objects(objects, device_address, adapter_pattern=None):
+ bus = dbus.SystemBus()
+ path_prefix = ""
+ if adapter_pattern:
+ adapter = find_adapter_in_objects(objects, adapter_pattern)
+ path_prefix = adapter.object_path
+ for path, ifaces in objects.items():
+ device = ifaces.get(DEVICE_INTERFACE)
+ if device is None:
+ continue
+ if (device["Address"] == device_address and
+ path.startswith(path_prefix)):
+ obj = bus.get_object(SERVICE_NAME, path)
+ return dbus.Interface(obj, DEVICE_INTERFACE)
+
+ raise Exception("Bluetooth device not found")
+