mirror of
https://github.com/RetroPie/RetroPie-Setup.git
synced 2025-04-02 10:51:41 -04:00
Updated the dependencies and scripts for python3: * `python-dbus` no longer exists in Debian 11 'bullseye' or Ubuntu > 20.04, installl the `python3` version * `python-gobject` has been superseeded by `python(3)-gi`, the current package is just a transitional package that pulls `python-gi` and the old `python-gobject-2` (deprecated). Update the dependencies and the scripts to use the new package. * set python3 as interpreter for the helper scripts. Fixed the dict syntax for getting items
47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
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")
|