连接到BLE设备后如何获得电池电量?

Wun*_*Wun 11 android battery bluetooth-lowenergy batterylevel android-4.3-jelly-bean

我正在开发一个应用程序,我必须连接到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););
    }

}
Run Code Online (Sandbox Code Playgroud)

但价值mBluetoothGatt.readCharacteristic(batteryLevel);不是电池水平值

怎么读电池????

Wun*_*Wun 21

我已经解决了这个问题.

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));
    }
}
Run Code Online (Sandbox Code Playgroud)

当你调用该函数时getbattery(),它会调用onCharacteristicRead.并且onCharacteristicRead将调用broadcastUpdate和特点,并传输行动吧.

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

  • @MS **00000000-0000-1000-8000-00805f9b34fb**:这是基本的蓝牙文件 UUID。0000**180F**-0000-1000-8000-00805f9b34fb:这是与基本 UUID 强加的公共服务 UUID,具有电池电量数据。您首先获得电池电量服务,然后从该服务中您将获得具有电池电量值的特征 (0000**2a19**-0000-1000-8000-00805f9b34fb)。之后,当您读取特征时, onCharacteristicRead 将获得带有特征的回调。通过 charactistic.getValue()/getIntValue() 获取值 (4认同)
  • 它给出真实的电池电量。 (2认同)