android - 从蓝牙 A2DP 设备 Android 读取电池电量
问题描述
我正在尝试读取我的蓝牙耳机(Skullcandy Method Wireless)的电池电量。到目前为止,我在连接耳机时成功接收连接广播,通过 bluetoothDevice.connectGatt() 连接到 GATT。
//getting device
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
在 BluetoothGattCallback.onConnectionChanged 上,我正在调用 discoverServices() 来发现蓝牙 Battery_Service。但服务列表始终为零。
BluetoothGatt bluetoothGatt = device.connectGatt(context, false, new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
Logger.log(A2dpConnectionStateReceiver.class, "onConnectionStateChange "+newState);
if(newState == BluetoothProfile.STATE_CONNECTED) {
Logger.log(A2dpConnectionStateReceiver.class, "onConnectionStateChange state connected, triggering discover services " + gatt.discoverServices());
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
Logger.log(A2dpConnectionStateReceiver.class, "onServicesDiscovered count = "+gatt.getServices().size()+" status="+ (status == BluetoothGatt.GATT_SUCCESS? "success" : "failed") );
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
Logger.log(A2dpConnectionStateReceiver.class, "onCharacteristicChanged "+characteristic.getUuid());
if(characteristic.getUuid().equals(Battery_Level_UUID)) {
Logger.log(A2dpConnectionStateReceiver.class, "onCharacteristicChanged battery level at "+characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0));
}
}
});
解决方案
推荐阅读
- search - 具有搜索功能的 Roku SceneGraph
- javascript - 错误类型错误:无法读取未定义的cordova-plugin-contact的属性“拆分”
- jquery - JQuery XDSoft datetimepicker插件deafultTime选项不能正常工作
- excel - 如何删除最后一行下面的行包含指定的字符串(最后一个匹配)?通过 VBA
- python - CoxPHFitter、生命线生存分析中的特征(协变量)选择
- python - 将大视频分割成可以分散在不同存储空间的小视频(无论格式如何)?
- visual-studio - Stop On Error Goto in Debug mode for a sub
- scala - Scala.js @JSGlobal - 无法执行运行 sbt
- python - 如何使用 bs4 在 Python 中附加 i 查询的两个结果?
- java - 在列表中重复
- > 爪哇