问题
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 Service. However, by entering "menu gatt" in bluetoothctl and then listing the GATT attributes of the device with "list-attributes [dev]", nothing shows up.
A similar question was posted to SO but the OP seems to have found a solution that doesn't work for me. When I run "info [dev]" in bluetoothctl I don't see the UUID for Battery Service.
I would prefer a solution that runs on the command line and is distro-agnostic.
Please let me know if this question should be posted on SuperUser instead.
回答1:
This is such a great question, ahead of development and tools that are available at the moment.
The short answer (in October 2018)
you have to write it yourself! It won't be a one liner in the terminal. I am going to write this for myself in Python, but C has a little more documentation, so if you are skilled with C go for it.
The long answer, but it's more a recommended starting point:
- Tony D: https://youtu.be/5fQR2PHMDWE?t=4644 managed to use
bluetoothctl
to read attributes and send data to a bluetooth device. Definitely check the video information, you will find great links and references: https://learn.adafruit.com/introduction-to-bluetooth-low-energy/gatt - Szymon Janc: https://youtu.be/VMDyebKT5c4 developer and contributer to the LINUX Bluetooth Stack
Definitely check out how this question is answered on Mobile devices. For Android it's the BAS (Battery Service): https://android.stackexchange.com/questions/106073/displaying-bluetooth-gadgets-battery-status-on-the-phone
回答2:
The reason you do not see Battery Level in list of GATT characteristic is because since Bluez v5.48 they move this specific GATT characteristic into DBUS org.bluez.Battery1 interface.
From the command line:
- Connect to your target BLE device with
bluetoothctl
- And then request DBUS:
dbus-send --print-reply=literal --system --dest=org.bluez /org/bluez/hci0/dev_<mac_address_of_your_ble_peripheral> org.freedesktop.DBus.Properties.Get string:"org.bluez.Battery1" string:"Percentage"
In my case with my BLE peripheral with the following MAC address C3:41:A6:C8:93:42
:
$ dbus-send --print-reply=literal --system --dest=org.bluez /org/bluez/hci0/dev_C3_41_A6_C8_93_42 org.freedesktop.DBus.Properties.Get string:"org.bluez.Battery1" string:"Percentage
variant byte 94
Note: You could potentially scan and connect to your device using Bluez DBUS API.
回答3:
In the bluez version you are using the Gatt attributes may be experimental.If so you need to enable the experimental characteristics by running the bluetoothd deamon by -E keyword Like "/usr/libexec/bluetooth/bluetoothd -E" this worked for me.
回答4:
For me running this in terminal worked- upower --dump
回答5:
Here is a way to get battery level via pulseaudio logs with some hack. My bluetooth headset uses proprietary Apple HFP AT commands, HFP/A2DP protocols are handled by pulseaudio directly. It seems the only way to get those values is through pulse.
回答6:
As said by @OlivierM above, the UUID is filtered by bluetoothd. You could undo that and export the UUID just as any other service characteristics by removing the following from the export_service()
function in src/gatt-client.c
if (gatt_db_service_get_claimed(attr))
return;
回答7:
By default Bluez 'hides' the Battery Service UUID. This is because there is a 'battery plugin' loaded at startup of bluetoothd.
If you don't want the battery plugin to be activated and make the Battery Service UUID visible again to bluetoothctl or any other application, then change the startup command for bluetoothd to be like this: 'bluetoothd -P battery'. That will make sure the battery plugin is not loaded. On a Raspberry Pi the bluetooth.service is located in /lib/systemd/system/bluetooth.service so you need to make the change in that file.
来源:https://stackoverflow.com/questions/49078659/check-battery-level-of-connected-bluetooth-device-on-linux