因此,当为DBus编写vala接口时,它从Object继承。但是,大多数属性如下所示:public string name { owned get; }
据我所知,这意味着,如果Notify的值更改,则Notify永远不会触发(我认为它可以更改,因为它反映了dbus中的接口,大多数当然会改变。)
样本:
[DBus (name ="org.bluez.Device1")]
public interface Device : Object{
[DBus (name = "Connected")]
public abstract bool connected { owned get; }
[DBus (name = "Connect")]
public abstract void connect();
[DBus (name = "Disconnect")]
public abstract void disconnect();
}
public void print_device_status(Device device){
if(device.connected){
stdout.printf("Device is connected");
}else{
stdout.printf("device is disconnected!");
}
}
int main(string[] args){
string mac = "dev_14_A5_1A_7F_61_08";
Device device = Bus.get_proxy_sync(BusType.SYSTEM, "org.bluez", string.join("/","/org/bluez/hci0", mac));
string message = "";
device.notify.connect(() => ( print_device_status(device) ));
if(device.connected){
device.disconnect();
}
device.connect();
device.disconnect();
return 0;
}
编译命令:valac --pkg gio-2.0 <file name> -o test
connect和断开连接方法按预期工作,从不调用print_device_status。我只能假设这是因为notify从不触发,并且我认为这是因为属性上没有设置方法。
我知道我可以制作DBusProxy对象并关注其中的属性更改,但这也不是很正确。
解决此问题的“正确”方法是什么?
答案 0 :(得分:1)
您应该通过调用org.freedesktop.DBus.Introspectable.Introspect
检查该属性是否发出通知信号,然后可以连接到org.freedesktop.DBus.Properties.PropertiesChanged
DBus信号。 official specification和Vala samples