I want to program a BLE central in python 3.11. to control a BLE peripheral (Nuki smart lock). To communicate over the D-Bus I am using the dasbus package. The standard communication is via connecting the device, and therefore it is paired with the central. This is already implemented and works well. However, the lock can also be operated directly (by pushing a button). This state change is announced by the peripheral by sending changing one Byte in a regularly sent iBeacon advertisement. As the central should update it's knowledge about the lock state as quickly as possible without permanently sending a request to the peripheral (this would drain the lock's batteries very quickly), the central should regularly scan for the iBeacon and request the state if a state change is advertised.
All solutions I found and tried so far are basically using the following approach:
import dasbus
from dasbus.connection import SystemMessageBus
from dasbus.loop import EventLoop
from gi.repository import GLib
def new_interface_handler(*args) -> None:
"""Do whatever needs to be done after receiving the advertisement"""
for arg in args:
print(arg)
def stop_scan():
"""Stop scanning for new devices and quit event loop"""
adapter.StopDiscovery()
loop.quit()
return False
bus = SystemMessageBus()
loop = EventLoop()
BLUEZ_SERVICE = 'org.bluez'
ADAPTER_PATH = '/org/bluez/hci0'
adapter = self.__bus.get_proxy(self.__BLUEZ_SERVICE, self.__ADAPTER_PATH)
manager = self.__bus.get_proxy(self.__BLUEZ_SERVICE, '/')
manager.InterfacesAdded.connect(new_interface_handler)
adapter.StartDiscovery()
GLib.timeout_add_seconds(interval=20, function=self._stop_scan)
loop.run()
This code is also basically working and detects advertisements sent by other peripherals like a nearby BLE thermometer. However, I think the problem lies in the org.freedesktop.DBus.ObjectManager. According to the documentation the InterfacesAdded signal is not emitted for changes on properties on existing interfaces. As the peripheral has to be paired to the central I don't see advertisements from the lock.
Do you have suggestions for another approach here?