Added a function for finding connected devices
This commit is contained in:
parent
b48b030b5e
commit
e4c015a4a7
1 changed files with 50 additions and 4 deletions
|
|
@ -265,7 +265,7 @@ def replace_mac_addresses(adapter_paths, addresses):
|
||||||
_run_command(['hciconfig', adapter_id, 'reset'])
|
_run_command(['hciconfig', adapter_id, 'reset'])
|
||||||
|
|
||||||
|
|
||||||
def find_devices_by_alias(alias):
|
def find_devices_by_alias(alias, return_path=False, created_bus=None):
|
||||||
"""Finds the Bluetooth addresses of devices
|
"""Finds the Bluetooth addresses of devices
|
||||||
that have a specified Bluetooth alias. Aliases
|
that have a specified Bluetooth alias. Aliases
|
||||||
are converted to uppercase before comparison
|
are converted to uppercase before comparison
|
||||||
|
|
@ -277,6 +277,9 @@ def find_devices_by_alias(alias):
|
||||||
:rtype: string or None
|
:rtype: string or None
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if created_bus is not None:
|
||||||
|
bus = created_bus
|
||||||
|
else:
|
||||||
bus = dbus.SystemBus()
|
bus = dbus.SystemBus()
|
||||||
# Find all connected/paired/discovered devices
|
# Find all connected/paired/discovered devices
|
||||||
devices = find_objects(
|
devices = find_objects(
|
||||||
|
|
@ -285,6 +288,7 @@ def find_devices_by_alias(alias):
|
||||||
DEVICE_INTERFACE)
|
DEVICE_INTERFACE)
|
||||||
|
|
||||||
addresses = []
|
addresses = []
|
||||||
|
matching_paths = []
|
||||||
for path in devices:
|
for path in devices:
|
||||||
# Get the device's address and paired status
|
# Get the device's address and paired status
|
||||||
device_props = dbus.Interface(
|
device_props = dbus.Interface(
|
||||||
|
|
@ -300,8 +304,15 @@ def find_devices_by_alias(alias):
|
||||||
# Check for an address match
|
# Check for an address match
|
||||||
if device_alias.upper() == alias.upper():
|
if device_alias.upper() == alias.upper():
|
||||||
addresses.append(device_addr)
|
addresses.append(device_addr)
|
||||||
|
matching_paths.append(path)
|
||||||
|
|
||||||
|
# Close the dbus connection if we created one
|
||||||
|
if created_bus is None:
|
||||||
bus.close()
|
bus.close()
|
||||||
|
|
||||||
|
if return_path:
|
||||||
|
return addresses, matching_paths
|
||||||
|
else:
|
||||||
return addresses
|
return addresses
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -828,3 +839,38 @@ class BlueZ():
|
||||||
return path
|
return path
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def find_connected_devices(self, alias_filter=False):
|
||||||
|
"""Finds the D-Bus path to a device that contains the
|
||||||
|
specified address.
|
||||||
|
|
||||||
|
:param address: The Bluetooth MAC address
|
||||||
|
:type address: string
|
||||||
|
:return: The path to the D-Bus object or None
|
||||||
|
:rtype: string or None
|
||||||
|
"""
|
||||||
|
|
||||||
|
devices = find_objects(
|
||||||
|
self.bus,
|
||||||
|
SERVICE_NAME,
|
||||||
|
DEVICE_INTERFACE)
|
||||||
|
conn_devices = []
|
||||||
|
for path in devices:
|
||||||
|
# Get the device's connection status
|
||||||
|
device_props = dbus.Interface(
|
||||||
|
self.bus.get_object(SERVICE_NAME, path),
|
||||||
|
"org.freedesktop.DBus.Properties")
|
||||||
|
device_conn_status = device_props.Get(
|
||||||
|
DEVICE_INTERFACE,
|
||||||
|
"Connected")
|
||||||
|
device_alias = device_props.Get(
|
||||||
|
DEVICE_INTERFACE,
|
||||||
|
"Alias").upper()
|
||||||
|
|
||||||
|
if device_conn_status:
|
||||||
|
if alias_filter and device_alias == alias_filter.upper():
|
||||||
|
conn_devices.append(path)
|
||||||
|
else:
|
||||||
|
conn_devices.append(path)
|
||||||
|
|
||||||
|
return conn_devices
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue