1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-20 23:41:45 +02:00

Refactor class and use common helper methods

This commit is contained in:
Erik Johansson
2018-04-18 23:31:28 +02:00
parent acbbf74bca
commit 4aece33e3d

View File

@@ -23,6 +23,7 @@ import android.content.Context;
import com.health.openscale.core.OpenScale; import com.health.openscale.core.OpenScale;
import com.health.openscale.core.datatypes.ScaleMeasurement; import com.health.openscale.core.datatypes.ScaleMeasurement;
import com.health.openscale.core.datatypes.ScaleUser; import com.health.openscale.core.datatypes.ScaleUser;
import com.health.openscale.core.utils.Converters;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
@@ -56,11 +57,14 @@ public class BluetoothExcelvanCF369BLE extends BluetoothCommunication {
case 0: case 0:
final ScaleUser selectedUser = OpenScale.getInstance(context).getSelectedScaleUser(); final ScaleUser selectedUser = OpenScale.getInstance(context).getSelectedScaleUser();
byte sex = selectedUser.getGender().isMale() ? (byte)0x01 : (byte)0x00; // 01 - male; 00 - female byte userId = (byte) 0x01;
byte height = (byte)(selectedUser.getBodyHeight() & 0xff); // cm byte sex = selectedUser.getGender().isMale() ? (byte) 0x01 : (byte) 0x00;
byte age = (byte)(selectedUser.getAge() & 0xff); // 0x00 = ordinary, 0x01 = amateur, 0x02 = professional
byte unit = 0x01; // kg byte exerciseLevel = (byte) 0x01;
byte height = (byte) selectedUser.getBodyHeight();
byte age = (byte) selectedUser.getAge();
byte unit = 0x01; // kg
switch (selectedUser.getScaleUnit()) { switch (selectedUser.getScaleUnit()) {
case LB: case LB:
unit = 0x02; unit = 0x02;
@@ -70,9 +74,9 @@ public class BluetoothExcelvanCF369BLE extends BluetoothCommunication {
break; break;
} }
byte xor_checksum = (byte)((byte)(0x01) ^ sex ^ (byte)(0x01) ^ height ^ age ^ unit); byte[] configBytes = {(byte) 0xfe, userId, sex, exerciseLevel, height, age, unit, (byte) 0x00};
configBytes[configBytes.length - 1] =
byte[] configBytes = {(byte)(0xfe), (byte)(0x01), sex, (byte)(0x01), height, age, unit, xor_checksum}; xorChecksum(configBytes, 1, configBytes.length - 2);
writeBytes(WEIGHT_MEASUREMENT_SERVICE, WEIGHT_MEASUREMENT_CHARACTERISTIC, configBytes); writeBytes(WEIGHT_MEASUREMENT_SERVICE, WEIGHT_MEASUREMENT_CHARACTERISTIC, configBytes);
break; break;
@@ -108,13 +112,13 @@ public class BluetoothExcelvanCF369BLE extends BluetoothCommunication {
} }
private void parseBytes(byte[] weightBytes) { private void parseBytes(byte[] weightBytes) {
float weight = (float) (((weightBytes[4] & 0xFF) << 8) | (weightBytes[5] & 0xFF)) / 10.0f; float weight = Converters.fromUnsignedInt16Be(weightBytes, 4) / 10.0f;
float fat = (float)(((weightBytes[6] & 0xFF) << 8) | (weightBytes[7] & 0xFF)) / 10.0f; float fat = Converters.fromUnsignedInt16Be(weightBytes, 6) / 10.0f;
float bone = (float)(weightBytes[8]) / 10.0f; float bone = (weightBytes[8] & 0xFF) / 10.0f;
float muscle = (float)(((weightBytes[9] & 0xFF) << 8) | (weightBytes[10] & 0xFF)) / 10.0f; float muscle = Converters.fromUnsignedInt16Be(weightBytes, 9) / 10.0f;
float viscal_fat = (float)(weightBytes[11]); float visceralFat = weightBytes[11] & 0xFF;
float water = (float)(((weightBytes[12] & 0xFF) << 8) | (weightBytes[13] & 0xFF)) / 10.0f; float water = Converters.fromUnsignedInt16Be(weightBytes, 12) / 10.0f;
float bmr = (float)(((weightBytes[14] & 0xFF) << 8) | (weightBytes[15] & 0xFF)); float bmr = Converters.fromUnsignedInt16Be(weightBytes, 14);
ScaleMeasurement scaleBtData = new ScaleMeasurement(); ScaleMeasurement scaleBtData = new ScaleMeasurement();
@@ -125,7 +129,6 @@ public class BluetoothExcelvanCF369BLE extends BluetoothCommunication {
scaleBtData.setMuscle(muscle); scaleBtData.setMuscle(muscle);
scaleBtData.setWater(water); scaleBtData.setWater(water);
scaleBtData.setBone(bone); scaleBtData.setBone(bone);
scaleBtData.setDateTime(new Date());
addScaleData(scaleBtData); addScaleData(scaleBtData);
} }