diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothBeurerBF700_800.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothBeurerBF700_800.java index ca80f9ec..7ad6c73c 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothBeurerBF700_800.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothBeurerBF700_800.java @@ -30,7 +30,6 @@ import com.health.openscale.core.utils.Converters; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.nio.ByteBuffer; import java.text.ParseException; import java.util.Arrays; import java.util.Collections; @@ -523,7 +522,7 @@ public class BluetoothBeurerBF700_800 extends BluetoothCommunication { throw new ParseException("Parse scala data: unexpected length", 0); } - long timestamp = ByteBuffer.wrap(data, 0, 4).getInt() * 1000L; + long timestamp = Converters.fromUnsignedInt32Be(data, 0) * 1000; float weight = getKiloGram(data, 4); int impedance = Converters.fromUnsignedInt16Be(data, 6); float fat = getPercent(data, 8); @@ -553,10 +552,11 @@ public class BluetoothBeurerBF700_800 extends BluetoothCommunication { private void updateDateTimeBeurer() { // Update date/time of the scale long unixTime = System.currentTimeMillis() / 1000L; - byte[] unixTimeBytes = ByteBuffer.allocate(Long.SIZE / 8).putLong(unixTime).array(); + byte[] unixTimeBytes = Converters.toUnsignedInt32Be(unixTime); Log.d(TAG, "Write new Date/Time:" + unixTime + " " + byteInHex(unixTimeBytes)); - writeBytes(new byte[]{(byte) getAlternativeStartByte(9), unixTimeBytes[4], unixTimeBytes[5], unixTimeBytes[6], unixTimeBytes[7]}); + writeBytes(new byte[]{(byte) getAlternativeStartByte(9), + unixTimeBytes[0], unixTimeBytes[1], unixTimeBytes[2], unixTimeBytes[3]}); } private void setUnitCommand() { diff --git a/android_app/app/src/main/java/com/health/openscale/core/utils/Converters.java b/android_app/app/src/main/java/com/health/openscale/core/utils/Converters.java index 7213a802..836d3158 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/utils/Converters.java +++ b/android_app/app/src/main/java/com/health/openscale/core/utils/Converters.java @@ -129,4 +129,21 @@ public class Converters { value += data[offset + 1] & 0xFF; return value; } + + public static long fromUnsignedInt32Be(byte[] data, int offset) { + long value = (long) (data[offset] & 0xFF) << 24; + value += (data[offset + 1] & 0xFF) << 16; + value += (data[offset + 2] & 0xFF) << 8; + value += data[offset + 3] & 0xFF; + return value; + } + + public static byte[] toUnsignedInt32Be(long value) { + byte[] data = new byte[4]; + data[0] = (byte) ((value >> 24) & 0xFF); + data[1] = (byte) ((value >> 16) & 0xFF); + data[2] = (byte) ((value >> 8) & 0xFF); + data[3] = (byte) (value & 0xFF); + return data; + } } diff --git a/android_app/app/src/test/java/com/health/openscale/ConvertersTest.java b/android_app/app/src/test/java/com/health/openscale/ConvertersTest.java index 22e666e6..ad344232 100644 --- a/android_app/app/src/test/java/com/health/openscale/ConvertersTest.java +++ b/android_app/app/src/test/java/com/health/openscale/ConvertersTest.java @@ -23,6 +23,7 @@ import org.junit.Test; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertTrue; +import static org.junit.Assert.assertArrayEquals; public class ConvertersTest { @@ -88,4 +89,21 @@ public class ConvertersTest { assertEquals(0xfc10, Converters.fromUnsignedInt16Be(data, 2)); assertEquals(0x107f, Converters.fromUnsignedInt16Be(data, 3)); } + + @Test + public void unsignedInt32Converters() throws Exception { + byte[] data = new byte[]{(byte) 0xf1, (byte) 0xf2, (byte) 0xf3, (byte) 0x7f, (byte) 0x7e}; + assertEquals(0xf1f2f37fL, Converters.fromUnsignedInt32Be(data, 0)); + assertEquals(0xf2f37f7eL, Converters.fromUnsignedInt32Be(data, 1)); + + data = new byte[]{(byte) 0x80, (byte) 0x00, (byte) 0x01, (byte) 0xff, (byte) 0x00}; + assertEquals(0x800001ffL, Converters.fromUnsignedInt32Be(data, 0)); + assertEquals(0x1ff00L, Converters.fromUnsignedInt32Be(data, 1)); + + data = new byte[]{(byte) 0xff, (byte) 0xfe, (byte) 0xfd, (byte) 0xfc}; + assertArrayEquals(data, Converters.toUnsignedInt32Be(0xfffefdfcL)); + assertEquals(0xffffffffL, + Converters.fromUnsignedInt32Be( + Converters.toUnsignedInt32Be(0xffffffffL), 0)); + } }