Skip to content

Instantly share code, notes, and snippets.

@tsjnachos117
Last active January 9, 2024 01:43
Show Gist options
  • Save tsjnachos117/8231f9f8ed08968cc5f1a7f4d3e06b0e to your computer and use it in GitHub Desktop.
Save tsjnachos117/8231f9f8ed08968cc5f1a7f4d3e06b0e to your computer and use it in GitHub Desktop.
Get KDE Connect battery info (from desktop/laptop)

I find the ability to get my android devices' battery info on my desktops via cli to be extremely convenient. I used to be able to this with KDE Connect easily, but things have just changed. Since I can't find any documentation on how to do this, and since I just stumbled on the answer myself, I though I might share what I know here. Please note that in the examples below, I will be using {device-id} as a placeholder for the string that KDE Connect uses to identify to my devices.

That said, I used to be able to get my various devices' battery status through gdbus through the following:

    gdbus call --session --dest org.kde.kdeconnect --object-path /modules/kdeconnect/devices/{device-id} --method org.kde.kdeconnect.device.battery.charge

However on Arch, I now get the following error: Error: GDBus.Error:org.freedesktop.DBus.Error.UnknownInterface: No such interface 'org.kde.kdeconnect.device.battery' at object path '/modules/kdeconnect/devices/b04294f19e8767f5'. I don't get this message on Ubuntu 20.20, or Linux Mint 20. Since Arch - unlike Ubuntu or Mint - is a rolling distro, this to me implies that newer versions of KDE Connect have changed the dbus interface. That said, how do you get this info now? What do you do? Well, the answer involves using a slightly more complicated command than before:

    gdbus call --session --dest org.kde.kdeconnect --object-path /modules/kdeconnect/devices/{device-id}/battery --method org.freedesktop.DBus.Properties.Get org.kde.kdeconnect.device.battery charge

You can also use dbus-send. The code is shorter, but the Borne Again SHell doesn't have any auto-completion, forcing you to type all of the code in by hand. Here is the code:

    dbus-send --print-reply --destorg.kde.kdeconnect /modules/kdeconnect/devices/{device-id}/battery org.freedesktop.DBus.Properties.Get string:org.kde.kdeconnect.device.battery string:charge

You can also use qdbus:

    qdbus org.kde.kdeconnect /modules/kdeconnect/devices/{device-id}/battery org.kde.kdeconnect.device.battery.charge

I hope this post will be useful to someone. Happy hacking!

PS: feel free to replace "charge" with "isCharging" or "refreshed" in any of the above examples.

@rkp-B
Copy link

rkp-B commented Jul 7, 2021

"refreshed" throws an error:
Error: org.freedesktop.DBus.Error.UnknownMethod
No such method 'refreshed' in interface 'org.kde.kdeconnect.device.battery' at object path '/modules/kdeconnect/devices/XXXX/battery' (signature '')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment