blob: de08cbdcb71221416203344eb08062ec4338556f [file] [log] [blame]
Mikel Astiz6e545912012-12-05 13:51:29 +01001import dbus
2
3SERVICE_NAME = "org.bluez"
Mikel Astiza1633242012-12-05 13:51:42 +01004ADAPTER_INTERFACE = SERVICE_NAME + ".Adapter1"
Mikel Astiz88b483b2012-12-05 13:51:41 +01005DEVICE_INTERFACE = SERVICE_NAME + ".Device1"
Mikel Astiz6e545912012-12-05 13:51:29 +01006
7def get_managed_objects():
8 bus = dbus.SystemBus()
9 manager = dbus.Interface(bus.get_object("org.bluez", "/"),
10 "org.freedesktop.DBus.ObjectManager")
11 return manager.GetManagedObjects()
12
13def find_adapter(pattern=None):
14 return find_adapter_in_objects(get_managed_objects(), pattern)
15
16def find_adapter_in_objects(objects, pattern=None):
17 bus = dbus.SystemBus()
18 for path, ifaces in objects.iteritems():
19 adapter = ifaces.get(ADAPTER_INTERFACE)
20 if adapter is None:
21 continue
Frédéric Danisf6655d0a2012-12-06 17:13:00 -030022 if not pattern or pattern == adapter["Address"] or \
23 path.endswith(pattern):
Mikel Astiz6e545912012-12-05 13:51:29 +010024 obj = bus.get_object(SERVICE_NAME, path)
25 return dbus.Interface(obj, ADAPTER_INTERFACE)
26 raise Exception("Bluetooth adapter not found")
Mikel Astiz428e1e62012-12-05 13:51:36 +010027
28def find_device(device_address, adapter_pattern=None):
29 return find_device_in_objects(get_managed_objects(), device_address,
30 adapter_pattern)
31
32def find_device_in_objects(objects, device_address, adapter_pattern=None):
33 bus = dbus.SystemBus()
34 path_prefix = ""
35 if adapter_pattern:
36 adapter = find_adapter_in_objects(objects, adapter_pattern)
37 path_prefix = adapter.object_path
38 for path, ifaces in objects.iteritems():
39 device = ifaces.get(DEVICE_INTERFACE)
40 if device is None:
41 continue
42 if (device["Address"] == device_address and
43 path.startswith(path_prefix)):
44 obj = bus.get_object(SERVICE_NAME, path)
45 return dbus.Interface(obj, DEVICE_INTERFACE)
46
47 raise Exception("Bluetooth device not found")