From c8986fd2ddc934490e1c729e1dd682a232900f38 Mon Sep 17 00:00:00 2001 From: Brikwerk Date: Thu, 23 Sep 2021 14:41:18 -0700 Subject: [PATCH] Added disconnect functionality --- nxbt/bluez.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/nxbt/bluez.py b/nxbt/bluez.py index bf41e4e..9f96748 100644 --- a/nxbt/bluez.py +++ b/nxbt/bluez.py @@ -316,6 +316,49 @@ def find_devices_by_alias(alias, return_path=False, created_bus=None): return addresses +def disconnect_devices_by_alias(alias, created_bus=None): + """Disconnects all devices matching an alias. + + :param alias: The device's alias + :type alias: string + """ + + if created_bus is not None: + bus = created_bus + else: + bus = dbus.SystemBus() + # Find all connected/paired/discovered devices + devices = find_objects( + bus, + SERVICE_NAME, + DEVICE_INTERFACE) + + addresses = [] + matching_paths = [] + for path in devices: + # Get the device's address and paired status + device_props = dbus.Interface( + bus.get_object(SERVICE_NAME, path), + "org.freedesktop.DBus.Properties") + device_alias = device_props.Get( + DEVICE_INTERFACE, + "Alias").upper() + + # Check for an alias match + if device_alias.upper() == alias.upper(): + device = dbus.Interface( + bus.get_object(SERVICE_NAME, path), + DEVICE_INTERFACE) + try: + device.Disconnect() + except Exception as e: + print(e) + + # Close the dbus connection if we created one + if created_bus is None: + bus.close() + + class BlueZ(): """Exposes the BlueZ D-Bus API as a Python object. """