1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-20 07:21:40 +02:00

New attempt at getting weight from 1byone scale

Issue #159
This commit is contained in:
Erik Johansson
2018-04-20 23:03:32 +02:00
parent 6a6ff471c1
commit 062db32266
2 changed files with 8 additions and 46 deletions

View File

@@ -19,11 +19,8 @@ package com.health.openscale.core.bluetooth;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Context;
import android.widget.Toast;
import com.health.openscale.core.OpenScale;
import com.health.openscale.core.datatypes.ScaleMeasurement;
import com.health.openscale.core.datatypes.ScaleUser;
import com.health.openscale.core.utils.Converters;
import java.util.UUID;
@@ -53,40 +50,11 @@ public class BluetoothOneByone extends BluetoothCommunication {
setIndicationOn(WEIGHT_MEASUREMENT_SERVICE_BODY_COMPOSITION, WEIGHT_MEASUREMENT_CHARACTERISTIC_BODY_COMPOSITION, WEIGHT_MEASUREMENT_CONFIG);
break;
case 1:
setNotificationOn(WEIGHT_MEASUREMENT_SERVICE, CMD_MEASUREMENT_CUSTOM_CHARACTERISTIC, WEIGHT_MEASUREMENT_CONFIG);
break;
case 2:
byte[] magicBytes = {(byte)0xfd,(byte)0x37,(byte)0x01,(byte)0x01,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00};
magicBytes[magicBytes.length - 1] =
xorChecksum(magicBytes, 0, magicBytes.length - 1);
writeBytes(WEIGHT_MEASUREMENT_SERVICE, CMD_MEASUREMENT_CHARACTERISTIC, magicBytes);
break;
case 3:
final ScaleUser selectedUser = OpenScale.getInstance(context).getSelectedScaleUser();
byte userId = (byte) 0x01;
byte sex = selectedUser.getGender().isMale() ? (byte) 0x01 : (byte) 0x00;
// 0x00 = ordinary, 0x01 = amateur, 0x02 = professional
byte exerciseLevel = (byte) 0x00;
byte height = (byte) selectedUser.getBodyHeight();
byte age = (byte) selectedUser.getAge();
byte unit = 0x01; // kg
switch (selectedUser.getScaleUnit()) {
case LB:
unit = 0x02;
break;
case ST:
unit = 0x04;
break;
}
byte[] magicBytes2 = {(byte) 0xfe, userId, sex, exerciseLevel, height, age, unit, (byte) 0x00};
magicBytes2[magicBytes2.length - 1] =
xorChecksum(magicBytes2, 1, magicBytes2.length - 2);
writeBytes(WEIGHT_MEASUREMENT_SERVICE, CMD_MEASUREMENT_CHARACTERISTIC, magicBytes2);
break;
default:
return false;
}
@@ -108,30 +76,18 @@ public class BluetoothOneByone extends BluetoothCommunication {
public void onBluetoothDataChange(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic gattCharacteristic) {
final byte[] data = gattCharacteristic.getValue();
Toast.makeText(context, "Log Data: " + byteInHex(data), Toast.LENGTH_LONG).show();
// if data is valid data
if (data != null && data.length == 16) {
if (data != null && data.length == 20) {
parseBytes(data);
}
}
private void parseBytes(byte[] weightBytes) {
float weight = Converters.fromUnsignedInt16Be(weightBytes, 4) / 10.0f; // kg
float fat = Converters.fromUnsignedInt16Be(weightBytes, 6) / 10.0f; // %
float bone = weightBytes[8] & 0xFF; // %
float muscle = Converters.fromUnsignedInt16Be(weightBytes, 9) / 10.0f; // %
float visceralFat = weightBytes[11] & 0xFF; // %
float water = Converters.fromUnsignedInt16Be(weightBytes, 12) / 10.0f; // %
float bmr = Converters.fromUnsignedInt16Be(weightBytes, 14); // kCal
float weight = Converters.fromUnsignedInt16Le(weightBytes, 11) / 100.0f;
ScaleMeasurement scaleBtData = new ScaleMeasurement();
scaleBtData.setWeight(weight);
scaleBtData.setFat(fat);
scaleBtData.setMuscle(muscle);
scaleBtData.setWater(water);
scaleBtData.setBone(bone);
addScaleData(scaleBtData);
}

View File

@@ -124,6 +124,12 @@ public class Converters {
return kg;
}
public static int fromUnsignedInt16Le(byte[] data, int offset) {
int value = (data[offset + 1] & 0xFF) << 8;
value += data[offset] & 0xFF;
return value;
}
public static int fromUnsignedInt16Be(byte[] data, int offset) {
int value = (data[offset] & 0xFF) << 8;
value += data[offset + 1] & 0xFF;