From 58397e6b4cda548e50f2991d23fd9cf28d0a5b84 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Wed, 31 Dec 2025 00:05:56 +0000 Subject: [PATCH] feat: Cache device names and listen for property updates Co-authored-by: aider (gemini/gemini-2.5-pro) --- scan.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/scan.py b/scan.py index 7966dcf..70fe6ad 100644 --- a/scan.py +++ b/scan.py @@ -8,12 +8,15 @@ logging.basicConfig(stream=sys.stdout, import asyncio from dbus_next.aio import MessageBus from dbus_next.constants import BusType +from dbus_next import Message BLUEZ_SERVICE = 'org.bluez' ADAPTER_IFACE = 'org.bluez.Adapter1' DEVICE_IFACE = 'org.bluez.Device1' +bus = None seen_devices = set() +name_cache = {} async def get_adapter(bus): """Gets the first Bluetooth adapter found.""" @@ -32,15 +35,25 @@ def on_interfaces_added(path, interfaces): """Callback for when a new D-Bus interface is added.""" if DEVICE_IFACE in interfaces: device_properties = interfaces[DEVICE_IFACE] - address = device_properties.get('Address') - alias = device_properties.get('Alias') - if address and alias and alias.value: - addr_str = address.value + address_variant = device_properties.get('Address') + if not address_variant: + return + + addr_str = address_variant.value + alias_variant = device_properties.get('Alias') + alias = alias_variant.value if alias_variant else name_cache.get(addr_str) + + if alias: + # Update cache if we found a new alias + if alias_variant and alias_variant.value: + name_cache[addr_str] = alias_variant.value + if addr_str not in seen_devices: seen_devices.add(addr_str) - logging.info(f"Found: {alias.value} ({addr_str}) (new)") + logging.info(f"Found: {alias} ({addr_str}) (new)") else: - logging.info(f"Found: {alias.value} ({addr_str})") + # Log repeat discoveries only if they have a name + logging.info(f"Found: {alias} ({addr_str})") async def main(): """Main function to run the scanner."""