BLE: code for fetching and parsing battery level (not used).

Part of #24.
This commit is contained in:
Dennis Guse
2023-05-25 22:09:27 +02:00
parent 93919183be
commit 34b5c2ab18
2 changed files with 35 additions and 0 deletions
@@ -0,0 +1,18 @@
package de.dennisguse.opentracks.data.models;
public class BatteryLevel {
public static BatteryLevel of(int percentage) {
return new BatteryLevel(percentage);
}
private final int percentage;
private BatteryLevel(int percentage) {
this.percentage = percentage;
}
public int getPercentage() {
return percentage;
}
}
@@ -28,6 +28,7 @@ import java.util.Collections;
import java.util.List;
import java.util.UUID;
import de.dennisguse.opentracks.data.models.BatteryLevel;
import de.dennisguse.opentracks.data.models.Cadence;
import de.dennisguse.opentracks.data.models.Distance;
import de.dennisguse.opentracks.data.models.HeartRate;
@@ -48,6 +49,11 @@ public class BluetoothUtils {
public static final UUID CLIENT_CHARACTERISTIC_CONFIG_UUID = new UUID(0x290200001000L, 0x800000805f9b34fbL);
public static final ServiceMeasurementUUID BATTERY = new ServiceMeasurementUUID(
new UUID(0x180F00001000L, 0x800000805f9b34fbL),
new UUID(0x2A1900001000L, 0x800000805f9b34fbL)
);
public static final ServiceMeasurementUUID HEARTRATE = new ServiceMeasurementUUID(
new UUID(0x180D00001000L, 0x800000805f9b34fbL),
new UUID(0x2A3700001000L, 0x800000805f9b34fbL)
@@ -102,6 +108,17 @@ public class BluetoothUtils {
return BluetoothUtils.getAdapter(context) != null;
}
public static BatteryLevel parseBatteryLevel(BluetoothGattCharacteristic characteristic) {
// DOCUMENTATION org.bluetooth.characteristic.battery_level.xml
byte[] raw = characteristic.getValue();
if (raw.length == 0) {
return null;
}
final int batteryLevel = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
return BatteryLevel.of(batteryLevel);
}
public static HeartRate parseHeartRate(BluetoothGattCharacteristic characteristic) {
//DOCUMENTATION https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.heart_rate_measurement.xml
byte[] raw = characteristic.getValue();