fix: Correct D-Bus signal subscription for properties changed
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
23
scan.py
23
scan.py
@@ -7,7 +7,7 @@ logging.basicConfig(stream=sys.stdout,
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from dbus_next.aio import MessageBus
|
from dbus_next.aio import MessageBus
|
||||||
from dbus_next.constants import BusType
|
from dbus_next.constants import BusType, MessageType
|
||||||
from dbus_next import Message
|
from dbus_next import Message
|
||||||
|
|
||||||
BLUEZ_SERVICE = 'org.bluez'
|
BLUEZ_SERVICE = 'org.bluez'
|
||||||
@@ -55,6 +55,14 @@ 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})")
|
||||||
|
|
||||||
|
def properties_changed_handler(message: Message):
|
||||||
|
"""Sync handler to dispatch async task for property changes."""
|
||||||
|
if message.message_type == MessageType.SIGNAL and \
|
||||||
|
message.member == 'PropertiesChanged' and \
|
||||||
|
message.interface == 'org.freedesktop.DBus.Properties':
|
||||||
|
# Further filtering is done in the async handler
|
||||||
|
asyncio.create_task(on_properties_changed(message))
|
||||||
|
|
||||||
async def on_properties_changed(message: Message):
|
async def on_properties_changed(message: Message):
|
||||||
"""Callback for when a device's properties change (e.g., Alias appears)."""
|
"""Callback for when a device's properties change (e.g., Alias appears)."""
|
||||||
if not message.body or message.body[0] != DEVICE_IFACE:
|
if not message.body or message.body[0] != DEVICE_IFACE:
|
||||||
@@ -103,13 +111,12 @@ async def main():
|
|||||||
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
|
# Subscribe to PropertiesChanged signal to catch late-arriving device names
|
||||||
await bus.add_match(
|
rule = "type='signal',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',path_namespace='/org/bluez'"
|
||||||
on_properties_changed,
|
introspection = await bus.introspect('org.freedesktop.DBus', '/org/freedesktop/DBus')
|
||||||
mtype='signal',
|
proxy_obj = bus.get_proxy_object('org.freedesktop.DBus', '/org/freedesktop/DBus', introspection)
|
||||||
iface='org.freedesktop.DBus.Properties',
|
dbus_interface = proxy_obj.get_interface('org.freedesktop.DBus')
|
||||||
member='PropertiesChanged',
|
await dbus_interface.call_add_match(rule)
|
||||||
path_namespace='/org/bluez'
|
bus.add_message_handler(properties_changed_handler)
|
||||||
)
|
|
||||||
|
|
||||||
logging.info("Starting Bluetooth scan... Press Ctrl+C to stop.")
|
logging.info("Starting Bluetooth scan... Press Ctrl+C to stop.")
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user