feat: Subscribe to PropertiesChanged for dynamic device naming
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
39
scan.py
39
scan.py
@@ -55,8 +55,39 @@ def on_interfaces_added(path, interfaces):
|
|||||||
# Log repeat discoveries only if they have a name
|
# Log repeat discoveries only if they have a name
|
||||||
logging.info(f"Found: {alias} ({addr_str})")
|
logging.info(f"Found: {alias} ({addr_str})")
|
||||||
|
|
||||||
|
async def on_properties_changed(message: Message):
|
||||||
|
"""Callback for when a device's properties change (e.g., Alias appears)."""
|
||||||
|
if not message.body or message.body[0] != DEVICE_IFACE:
|
||||||
|
return
|
||||||
|
|
||||||
|
changed_properties = message.body[1]
|
||||||
|
alias_variant = changed_properties.get('Alias')
|
||||||
|
if not alias_variant or not alias_variant.value:
|
||||||
|
return
|
||||||
|
|
||||||
|
alias = alias_variant.value
|
||||||
|
device_path = message.path
|
||||||
|
|
||||||
|
try:
|
||||||
|
introspection = await bus.introspect(BLUEZ_SERVICE, device_path)
|
||||||
|
device_obj = bus.get_proxy_object(BLUEZ_SERVICE, device_path, introspection)
|
||||||
|
device_props_iface = device_obj.get_interface('org.freedesktop.DBus.Properties')
|
||||||
|
|
||||||
|
address_variant = await device_props_iface.call_get(DEVICE_IFACE, 'Address')
|
||||||
|
addr_str = address_variant.value
|
||||||
|
|
||||||
|
# Update cache
|
||||||
|
name_cache[addr_str] = alias
|
||||||
|
|
||||||
|
if addr_str not in seen_devices:
|
||||||
|
seen_devices.add(addr_str)
|
||||||
|
logging.info(f"Found: {alias} ({addr_str}) (new)")
|
||||||
|
except Exception as e:
|
||||||
|
logging.debug(f"Could not process property change for {device_path}: {e}")
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
"""Main function to run the scanner."""
|
"""Main function to run the scanner."""
|
||||||
|
global bus
|
||||||
bus = await MessageBus(bus_type=BusType.SYSTEM).connect()
|
bus = await MessageBus(bus_type=BusType.SYSTEM).connect()
|
||||||
adapter = await get_adapter(bus)
|
adapter = await get_adapter(bus)
|
||||||
if not adapter:
|
if not adapter:
|
||||||
@@ -71,6 +102,14 @@ async def main():
|
|||||||
obj_manager_iface = obj_manager.get_interface('org.freedesktop.DBus.ObjectManager')
|
obj_manager_iface = obj_manager.get_interface('org.freedesktop.DBus.ObjectManager')
|
||||||
obj_manager_iface.on_interfaces_added(on_interfaces_added)
|
obj_manager_iface.on_interfaces_added(on_interfaces_added)
|
||||||
|
|
||||||
|
# Subscribe to PropertiesChanged signal to catch late-arriving device names
|
||||||
|
bus.add_signal_receiver(
|
||||||
|
on_properties_changed,
|
||||||
|
signal_name='PropertiesChanged',
|
||||||
|
dbus_namespace='org.freedesktop.DBus.Properties',
|
||||||
|
path_namespace='/org/bluez'
|
||||||
|
)
|
||||||
|
|
||||||
logging.info("Starting Bluetooth scan... Press Ctrl+C to stop.")
|
logging.info("Starting Bluetooth scan... Press Ctrl+C to stop.")
|
||||||
try:
|
try:
|
||||||
await adapter_iface.call_start_discovery()
|
await adapter_iface.call_start_discovery()
|
||||||
|
|||||||
Reference in New Issue
Block a user