在 gjs 中通过 DBus 访问 UPower 电池百分比:访问被拒绝

在 gjs 中通过 DBus 访问 UPower 电池百分比:访问被拒绝

我正在尝试制作一个扩展程序,当电池电量达到特定值时显示通知。为了获取百分比级别,我尝试实现方法,但由于我没有带背光的键盘,我修改了示例以使用我找到的信息报告电池百分比和状态(是否正在充电)这里关于 UPower,它最终看起来像这样:

const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;

// This the D-Bus interface as XML
const batInterface = '<node>\
<interface name="org.freedesktop.UPower.DisplayDevice"> \
    <method name="State"> \
        <arg name="State" type="u" direction="out"/> \
    </method> \
    <method name="Percentage"> \
        <arg name="Percentage" type="d" direction="out"/> \
    </method> \
</interface> \
</node>';

// Declare the proxy class based on the interface
const batProxy = Gio.DBusProxy.makeProxyWrapper(batInterface);

// Get the /org/freedesktop/UPower/KbdBacklight instance from the bus
let batInstanceProxy = new batProxy(
    Gio.DBus.system,
    "org.freedesktop.UPower",
    "/org/freedesktop/UPower/devices/DisplayDevice"
);

// You can use proxy.<method>Sync syntax to 
// call the D-Bus method in a Sync way
print("The percentage is " + batInstanceProxy.PercentageSync());

let loop = new GLib.MainLoop(null, false);
loop.run();

但运行时gjs test.js我得到以下输出:


(gjs:66204): Gjs-WARNING **: 20:35:16.865: JS ERROR: Gio.DBusError: GDBus.Error:org.freedesktop.DBus.Error.AccessDenied: Rejected send message, 1 matched rules; type="method_call", sender=":1.689" (uid=1000 pid=66204 comm="gjs test.js " label="unconfined") interface="org.freedesktop.UPower.DisplayDevice" member="Percentage" error name="(unset)" requested_reply="0" destination=":1.41" (uid=0 pid=1322 comm="/usr/lib/upower/upowerd " label="unconfined")
_proxyInvoker@resource:///org/gnome/gjs/modules/core/overrides/Gio.js:139:46
_makeProxyMethod/<@resource:///org/gnome/gjs/modules/core/overrides/Gio.js:164:30
@test.js:28:47


(gjs:66204): Gjs-CRITICAL **: 20:35:16.865: Script test.js threw an exception

有人能指出这里可能有什么问题吗?我对 gjs、DBus 通信还不熟悉,对 JS 也比较陌生,但我以前做过编程

相关内容