diff --git a/src/androidTest/java/de/dennisguse/opentracks/util/BluetoothUtilsTest.java b/src/androidTest/java/de/dennisguse/opentracks/util/BluetoothUtilsTest.java index 4c5e9d4b9..7b94c6311 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/util/BluetoothUtilsTest.java +++ b/src/androidTest/java/de/dennisguse/opentracks/util/BluetoothUtilsTest.java @@ -43,40 +43,40 @@ public class BluetoothUtilsTest { @Test public void parseCyclingSpeedCadence_crankOnly() { BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(BluetoothUtils.CYCLING_SPEED_CADENCE_SERVICE_UUID, 0, 0); - characteristic.setValue(new byte[]{0x01, (byte) 0xC8, 0x00, 0x00, 0x00, 0x06, (byte) 0x99}); + characteristic.setValue(new byte[]{0x02, (byte) 0xC8, 0x00, 0x00, 0x00, 0x06, (byte) 0x99}); // when SensorDataCycling.CadenceAndSpeed sensor = BluetoothUtils.parseCyclingCrankAndWheel("address", "sensorName", characteristic); // then - assertEquals(200, sensor.getCadence().getCrankRevolutionsCount()); assertNull(sensor.getDistanceSpeed()); + assertEquals(200, sensor.getCadence().getCrankRevolutionsCount()); } @Test public void parseCyclingSpeedCadence_wheelOnly() { BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(BluetoothUtils.CYCLING_SPEED_CADENCE_SERVICE_UUID, 0, 0); - characteristic.setValue(new byte[]{0x02, (byte) 0xE1, 0x00, 0x45, (byte) 0x99}); + characteristic.setValue(new byte[]{0x01, (byte) 0xFF, (byte) 0xFF, 0, 1, 0x45, (byte) 0x99}); // when SensorDataCycling.CadenceAndSpeed sensor = BluetoothUtils.parseCyclingCrankAndWheel("address", "sensorName", characteristic); // then + assertEquals(65535 + 16777216, sensor.getDistanceSpeed().getWheelRevolutionsCount()); assertNull(sensor.getCadence()); - assertEquals(225, sensor.getDistanceSpeed().getWheelRevolutionsCount()); } @Test public void parseCyclingSpeedCadence_crankWheel() { BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(BluetoothUtils.CYCLING_SPEED_CADENCE_SERVICE_UUID, 0, 0); - characteristic.setValue(new byte[]{0x03, (byte) 0xC8, 0x00, 0x00, 0x00, 0x06, (byte) 0x99, (byte) 0xE1, 0x00, 0x45, (byte) 0x99}); + characteristic.setValue(new byte[]{0x03, (byte) 0xC8, 0x00, 0x00, 0x01, 0x06, (byte) 0x99, (byte) 0xE1, 0x00, 0x45, (byte) 0x99}); // when SensorDataCycling.CadenceAndSpeed sensor = BluetoothUtils.parseCyclingCrankAndWheel("address", "sensorName", characteristic); // then - assertEquals(200, sensor.getCadence().getCrankRevolutionsCount()); - assertEquals(225, sensor.getDistanceSpeed().getWheelRevolutionsCount()); + assertEquals(200 + 16777216, sensor.getDistanceSpeed().getWheelRevolutionsCount()); + assertEquals(225, sensor.getCadence().getCrankRevolutionsCount()); } @Test diff --git a/src/main/java/de/dennisguse/opentracks/util/BluetoothUtils.java b/src/main/java/de/dennisguse/opentracks/util/BluetoothUtils.java index 8df73aca6..6d7e4418e 100644 --- a/src/main/java/de/dennisguse/opentracks/util/BluetoothUtils.java +++ b/src/main/java/de/dennisguse/opentracks/util/BluetoothUtils.java @@ -107,9 +107,6 @@ public class BluetoothUtils { return characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 2); } - /** - * Documentation: https://www.bluetooth.org/docman/handlers/downloaddoc.ashx?doc_id=261449 - */ public static SensorDataCycling.CadenceAndSpeed parseCyclingCrankAndWheel(String address, String sensorName, @NonNull BluetoothGattCharacteristic characteristic) { // DOCUMENTATION https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.csc_measurement.xml int valueLength = characteristic.getValue().length; @@ -118,26 +115,26 @@ public class BluetoothUtils { } int flags = characteristic.getValue()[0]; - boolean hasCrank = (flags & 0x01) > 0; - boolean hasWheel = (flags & 0x02) > 0; + boolean hasWheel = (flags & 0x01) > 0; + boolean hasCrank = (flags & 0x02) > 0; - SensorDataCycling.Cadence cadence = null; int index = 1; - if (hasCrank && valueLength - index >= 6) { - long crankCount = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT32, index); + SensorDataCycling.DistanceSpeed speed = null; + if (hasWheel && valueLength - index >= 6) { + int wheelTotalRevolutionCount = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT32, index); index += 4; - - int crankTime = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, index); // 1/1024s + int wheelTime = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, index); // 1/1024s + speed = new SensorDataCycling.DistanceSpeed(address, sensorName, wheelTotalRevolutionCount, wheelTime); index += 2; - cadence = new SensorDataCycling.Cadence(address, sensorName, crankCount, crankTime); } - SensorDataCycling.DistanceSpeed speed = null; - if (hasWheel && valueLength - index >= 4) { - int wheelCount = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, index); + SensorDataCycling.Cadence cadence = null; + if (hasCrank && valueLength - index >= 4) { + long crankCount = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, index); index += 2; - int wheelTime = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, index); // 1/1024s - speed = new SensorDataCycling.DistanceSpeed(address, sensorName, wheelCount, wheelTime); + + int crankTime = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, index); // 1/1024s + cadence = new SensorDataCycling.Cadence(address, sensorName, crankCount, crankTime); } return new SensorDataCycling.CadenceAndSpeed(address, sensorName, cadence, speed);