Подтвердить что ты не робот

Как получить уровень заряда аккумулятора после подключения к устройству BLE?

Я разрабатываю приложение, в котором мне нужно подключиться к устройству Bluetooth на Android 4.3.

И я хочу получить уровень заряда батареи с помощью Battery_Service и Battery_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); это не уровень заряда батареи

Как читать батарею?

4b9b3361

Ответ 1

Я решил эту проблему.

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(), она вызывает onCharacteristicRead. и onCharacteristicRead вызовет broadcastUpdate и передаст ему характеристику и действие.

А characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0) в broadcastUpdate является значением батареи устройства BLE.