Skip to content

Commit

Permalink
BLE: code for fetching and parsing battery level (not used).
Browse files Browse the repository at this point in the history
Part of #24.
  • Loading branch information
dennisguse committed May 25, 2023
1 parent 9391918 commit 34b5c2a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
17 changes: 17 additions & 0 deletions src/main/java/de/dennisguse/opentracks/sensors/BluetoothUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;
Expand All @@ -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)
Expand Down Expand Up @@ -102,6 +108,17 @@ public static boolean hasBluetooth(Context context) {
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();
Expand Down

0 comments on commit 34b5c2a

Please sign in to comment.