检查linux上连接的蓝牙设备的电池电量

问题描述 投票:20回答:4

如何查看连接的蓝牙设备的电池电量?该设备显示Android上的电池电量,所以我假设该设备支持GATT-based Battery Service。但是,通过在bluetoothctl中输入“menu gatt”,然后使用“list-attributes [dev]”列出设备的GATT属性,则不显示任何内容。

一个similar question was posted to SO,但OP似乎找到了一个对我不起作用的解决方案。当我在bluetoothctl中运行“info [dev]”时,我没有看到电池服务的UUID。

我更喜欢在命令行上运行并且与驱动程序无关的解决方案。

如果这个问题应该发布在SuperUser上,请告诉我。

linux bluetooth bluez
4个回答
8
投票

在开发和目前可用的工具之前,这是一个非常好的问题。

简答(2018年10月)

你必须自己写!它不会是终端中的一个班轮。我将在Python中为自己编写这个,但是C有一些文档,所以如果你熟练使用C,那就去吧。

答案很长,但它更像是一个推荐的起点:

  1. Tony D:https://youtu.be/5fQR2PHMDWE?t=4644设法使用bluetoothctl读取属性并将数据发送到蓝牙设备。绝对检查视频信息,你会发现很棒的链接和参考:https://learn.adafruit.com/introduction-to-bluetooth-low-energy/gatt
  2. Szymon Janc:https://youtu.be/VMDyebKT5c4开发者和LINUX蓝牙堆栈的贡献者
  3. 请务必查看移动设备上如何回答此问题。对于Android,它是BAS(电池服务):https://android.stackexchange.com/questions/106073/displaying-bluetooth-gadgets-battery-status-on-the-phone On Android 8.0.1

4
投票

您没有在GATT特征列表中看到电池电量的原因是因为从Bluez v5.48开始,他们将这个特定的GATT特征移动到DBUS org.bluez.Battery1 interface

从命令行:

  1. 使用bluetoothctl连接到目标BLE设备
  2. 然后请求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"

在我的情况下,我的BLE外设具有以下MAC地址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

注意:您可以使用Bluez DBUS API扫描并连接到您的设备。


1
投票

在bluez版本中你使用的Gatt属性可能是实验性的。如果你需要通过-E关键字运行bluetoothd deamon来启用实验特性像“/ usr / libexec / bluetooth / bluetoothd -E”这对我有用。


0
投票

这是一种通过pulseaudio日志获得电池电量的方法,其中包含一些hack。我的蓝牙耳机使用专有的Apple HFP AT命令,HFP / A2DP协议由pulseaudio直接处理。似乎获得这些价值的唯一方法是通过脉冲。

© www.soinside.com 2019 - 2024. All rights reserved.