读取电池电量 - Broadcom BLE 堆栈和 HTC 1x+
Posted
技术标签:
【中文标题】读取电池电量 - Broadcom BLE 堆栈和 HTC 1x+【英文标题】:Read battery level - Broadcom BLE stack and HTC 1x+ 【发布时间】:2013-03-28 17:59:49 【问题描述】:我有 BleFindMeClient 与 TI Mini Keyfob 一起在 HTC 1X+ 上工作。我正在尝试稍微扩展它以读取电池电量(不注册电池更新)。我可以从 BTool 执行此操作,但我的 android prog 失败,并且我收到调试消息:
getCharacteristic - 未找到服务数据
这是什么意思?我在哪里可以找到此错误消息和其他错误消息的含义?
显然我可以写特征,因为我可以设置警报。但是关于阅读特性,有一些我没有掌握的相当基本的东西,但是我找不到示例代码。
谁能给我一个更好的代码片段,或者在下面发现一些愚蠢的东西?
public class BatteryProfileClient extends BleClientProfile
private static String TAG = "BatteryProfileClient";
static public BleGattID myUuid = new BleGattID("0000180f-0000-1000-8000-00805f9b34fb");
private static final BleGattID BATTERY_CHARACTERISTIC = new BleGattID("00002a19-0000-1000-8000-00805f9b34fb");
private BatteryServiceClient mBatteryServiceClient = new BatteryServiceClient();
public BatteryProfileClient(Context context)
super(context, myUuid);
Log.d(TAG, "Instantiated");
ArrayList<BleClientService> services = new ArrayList<BleClientService>();
services.add(mBatteryServiceClient);
init(services, null);
public void batt(BluetoothDevice device)
BleCharacteristic battLevelCharacteristic = mBatteryServiceClient.getCharacteristic(device, BATTERY_CHARACTERISTIC);
byte b[] = battLevelCharacteristic.getValue();
Log.d(TAG, "battery " + b);
【问题讨论】:
【参考方案1】:不知道我是否迟到了,但这应该可以解决您的问题 -
public int getbattery(BluetoothGatt mBluetoothGatt)
BluetoothGattService batteryService = mBluetoothGatt
.getService(BATTERY_SERVICE_UUID);
if (batteryService == null)
Log.d(TAG, "Battery service not found!");
return 0;
BluetoothGattCharacteristic batteryLevel = batteryService
.getCharacteristic(BATTERY_LEVEL_UUID);
if (batteryLevel == null)
Log.d(TAG, "Battery level not found!");
return 0;
mBluetoothGatt.readCharacteristic(batteryLevel);
return batteryLevel.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT8, 0);
【讨论】:
嗨,兄弟,你能在单一服务中分享电池和 HR 监视器的完整代码吗?【参考方案2】:Visit https://***.com/questions/19539535/how-to-get-the-battery-level-after-connect-to-the-ble-device
同样的问题 使用 BluetoothGattCallback 用于回调并读取电池值
【讨论】:
以上是关于读取电池电量 - Broadcom BLE 堆栈和 HTC 1x+的主要内容,如果未能解决你的问题,请参考以下文章