Check battery level of connected bluetooth device on linux

后端 未结 9 1761
盖世英雄少女心
盖世英雄少女心 2021-01-30 02:09

How can I check the battery level of a connected bluetooth device? The device shows the battery level on Android so I\'m assuming the device supports the GATT-based Battery Serv

9条回答
  •  被撕碎了的回忆
    2021-01-30 02:44

    (This answer is specific to headphones/headsets)

    I'd been using the Python program from clst's answer for some time and although it worked, it required me to connect, then disconnect and run it again. If I understand the problem correctly, that happens because only one program can open a socket to talk to the bluetooth device, so it ends up fighting with PulseAudio over it.

    I've recently found out about hsphfpd.

    hsphfpd is specification with some prototype implementation used for connecting Bluetooth devices with HSP and HFP profiles on Linux operating system.

    Basically, since only one program can communicate with the headset at once and it wouldn't make sense to implement battery level reporting in an audio server, nor implement audio in a power management software, it moves that functionality to an external daemon. That way, PulseAudio and whatever can both use the headset at the same time. There is a version of PulseAudio patched to use hsphfpd. Even though these are both still prototypes, they seem to work very well.

    hsphfpd reports battery status (and other stuff) through DBus, so to get it from the command line, you can just do

    dbus-send --system --dest=org.hsphfpd --print-reply /org/hsphfpd/hci0/dev_XX_XX_XX_XX_XX_XX/hsp_hs org.freedesktop.DBus.Properties.Get string:org.hsphfpd.Endpoint string:BatteryLevel
    

    or even call it from a program.

    Both of these are available in the AUR, if you use Arch Linux.

提交回复
热议问题