android - 连接BLE设备后如何获取电池电量?

标签 android bluetooth-lowenergy battery android-4.3-jelly-bean batterylevel

我正在开发一个应用程序,我必须在 Android 4.3 上连接到蓝牙设备。

我想通过使用 Battery_ServiceBattery_Level 获取电池电量。

public class BluetoothLeService extends Service {

    private static final UUID Battery_Service_UUID =
                UUID.fromString("0000180F-0000-1000-8000-00805f9b34fb");

    private static final UUID Battery_Level_UUID =
                UUID.fromString("00002a19-0000-1000-8000-00805f9b34fb");


    public void getbattery() {

        BluetoothGattService batteryService = mBluetoothGatt.getService(Battery_Service_UUID);
        if(batteryService == null) {
            Log.d(TAG, "Battery service not found!");
            return;
        }

        BluetoothGattCharacteristic batteryLevel = batteryService.getCharacteristic(Battery_Level_UUID);
        if(batteryLevel == null) {
            Log.d(TAG, "Battery level not found!");
            return;
        }

         mBluetoothGatt.readCharacteristic(batteryLevel);
         // What should I do that I can get the battery level ??
         Log.d(TAG, "Battery level " + mBluetoothGatt.readCharacteristic(batteryLevel););
    }

}

但是 mBluetoothGatt.readCharacteristic(batteryLevel); 的值不是电池电量值

如何读取电量?

最佳答案

我已经解决了这个问题。

public class BluetoothLeService extends Service {

    private static final UUID Battery_Service_UUID = UUID.fromString("0000180F-0000-1000-8000-00805f9b34fb");
    private static final UUID Battery_Level_UUID = UUID.fromString("00002a19-0000-1000-8000-00805f9b34fb");

    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {

        if(status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        }
    }


    private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) {

        final Intent intent = new Intent(action);   
        Log.v(TAG, "characteristic.getStringValue(0) = " + characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0));
        intent.putExtra(DeviceControl.EXTRAS_DEVICE_BATTERY, characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0));  
        sendBroadcast(intent);
    }

    public void getbattery() {

        BluetoothGattService batteryService = mBluetoothGatt.getService(Battery_Service_UUID);
        if(batteryService == null) {
            Log.d(TAG, "Battery service not found!");
            return;
        }

        BluetoothGattCharacteristic batteryLevel = batteryService.getCharacteristic(Battery_Level_UUID);
        if(batteryLevel == null) {
            Log.d(TAG, "Battery level not found!");
            return;
        }
        mBluetoothGatt.readCharacteristic(batteryLevel);
        Log.v(TAG, "batteryLevel = " + mBluetoothGatt.readCharacteristic(batteryLevel));
    }
}

当您调用函数 getbattery() 时,它会调用 onCharacteristicReadonCharacteristicRead 将调用 broadcastUpdate 并将特征和操作传输给它。

而broadcastUpdate中的characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0)是BLE设备的电池值。

关于android - 连接BLE设备后如何获取电池电量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19539535/

相关文章:

android - 通知有时不会在使用 RxAndroidBle 的通知/写入可观察对象中调用

安卓改造 : Display in Recyclerview

java - 我想在不将图片保存到图库的情况下打开 android 相机

java - 使用 Java 中的方法将类作为类型传递给其他类

android - 如何在 TextView 中水平和垂直居中文本?

iphone - 如何在 iOS 上获取 1% 粒度的实时电池电量

linux - 如何在 Linux 中保持与蓝牙 LE 体重计的连接

java - 安卓 BLE。 device.getUUids() 总是返回 null

Android:如何更准确地获取电池电量状态?

java - 带有监听器的 BroadcastReceiver 在不使用时会耗尽电池