13

I use Bluetooth headset that use Bluetooth version 4.1. When I connect with android it shows the battery status. But with Linux (Ubuntu 18.04) I cannot get battery status. I tried with bluetoothctl, looking file in /sys/class/power_supply as in other questions, But they didn't help me.

askubuntu.com/questions/53880/is-there-any-way-to-check-the-battery-percentage-of-apple-wireless-peripherals

stackoverflow.com/questions/49078659/check-battery-level-of-connected-bluetooth-device-on-linux

My bluetooth device don't use GATT profile. It uses A2DP sink for streaming audio.

I looked Bluez documentation. They all said is do with GATT profile and get the attributes.

Is there a way to read battery status even with pragmatically in Linux properly? How does android device get the battery status? Is it a weakness of Linux Bluetooth stack?

Lakindu Akash
  • 964
  • 1
  • 11
  • 28
  • It is likely that your device is using a custom attribute for the battery level. Can you try the following and post the output:- Run "bluetoothctl", then "menu gatt", then "list attributes [dev]" and replace [dev] with the headset's Bluetooth Address. – Youssif Saeed Jan 14 '19 at 06:56
  • 3
    @YoussifSaeed When I type `menu gatt` and the `list-attributes [device-mac]` It prints nothing. – Lakindu Akash Jan 18 '19 at 18:04
  • @mimix2 Maybe have a look to my answer here: https://stackoverflow.com/a/55008142/6267288. It is not clear if the Bluez DBUS org.bluez.Battery1 is specific to GATT or not. Looking quickly at the code, it looks to be GATT dependent but I might be wrong. – OlivierM Mar 05 '19 at 17:20
  • 1
    @LakinduAkash You need to `connect [device-mac]` before `menu gatt`. – OJFord Nov 12 '20 at 18:40

1 Answers1

9

Since this pull request by Dmitry Sharshakov, PipeWire has support for reporting battery status (with devices that use Apple HFP AT commands). It uses bluez's Battery Provider API, which is still experimental and is only available if bluetoothd is started with the -E flag.

On Arch Linux, it should be enough to run

cp /usr/lib/systemd/system/bluetooth.service /etc/systemd/system/
sed -i -r 's/ExecStart=.+/& -E/' /etc/systemd/system/bluetooth.service
systemctl daemon-reload
systemctl restart bluetooth

And the headset battery level should appear. To get the battery level programatically, you can then use UPower's DBus API.

LHLaurini
  • 1,737
  • 17
  • 31