From 2e8a2cdfedd36bb0809ac31916c0b01c96ec9c2c Mon Sep 17 00:00:00 2001 From: OliE Date: Fri, 21 Sep 2018 16:37:28 +0200 Subject: [PATCH] supported body measurements for Yunmai scale --- .../bluetooth/BluetoothYunmaiSE_Mini.java | 27 ++++++++- .../core/bluetooth/lib/YunmaiLib.java | 60 +++++++++++++++++++ 2 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 android_app/app/src/main/java/com/health/openscale/core/bluetooth/lib/YunmaiLib.java diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothYunmaiSE_Mini.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothYunmaiSE_Mini.java index adff58c5..fcd2993d 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothYunmaiSE_Mini.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothYunmaiSE_Mini.java @@ -23,6 +23,7 @@ import android.content.SharedPreferences; import android.preference.PreferenceManager; import com.health.openscale.core.OpenScale; +import com.health.openscale.core.bluetooth.lib.YunmaiLib; import com.health.openscale.core.datatypes.ScaleMeasurement; import com.health.openscale.core.datatypes.ScaleUser; import com.health.openscale.core.utils.Converters; @@ -31,6 +32,8 @@ import java.util.Date; import java.util.Random; import java.util.UUID; +import timber.log.Timber; + public class BluetoothYunmaiSE_Mini extends BluetoothCommunication { private final UUID WEIGHT_MEASUREMENT_SERVICE = UUID.fromString("0000ffe0-0000-1000-8000-00805f9b34fb"); private final UUID WEIGHT_MEASUREMENT_CHARACTERISTIC = UUID.fromString("0000ffe4-0000-1000-8000-00805f9b34fb"); @@ -119,7 +122,7 @@ public class BluetoothYunmaiSE_Mini extends BluetoothCommunication { } private void parseBytes(byte[] weightBytes) { - final ScaleUser selectedUser = OpenScale.getInstance().getSelectedScaleUser(); + final ScaleUser scaleUser = OpenScale.getInstance().getSelectedScaleUser(); ScaleMeasurement scaleBtData = new ScaleMeasurement(); @@ -130,8 +133,26 @@ public class BluetoothYunmaiSE_Mini extends BluetoothCommunication { scaleBtData.setWeight(weight); if (isMini) { - float fat = Converters.fromUnsignedInt16Be(weightBytes, 17) / 100.0f; - scaleBtData.setFat(fat); + int sex; + + if (scaleUser.getGender() == Converters.Gender.MALE) { + sex = 1; + } else { + sex = 0; + } + + YunmaiLib yunmaiLib = new YunmaiLib(sex, scaleUser.getBodyHeight()); + float bodyFat = Converters.fromUnsignedInt16Be(weightBytes, 17) / 100.0f; + int resistance = Converters.fromUnsignedInt16Be(weightBytes, 15); + scaleBtData.setFat(bodyFat); + scaleBtData.setMuscle(yunmaiLib.getMuscle(bodyFat)); + scaleBtData.setWater(yunmaiLib.getWater(bodyFat)); + scaleBtData.setBone(yunmaiLib.getBoneMass(scaleBtData.getMuscle(), weight)); + + Timber.d("received bytes [%s]", byteInHex(weightBytes)); + Timber.d("received decrypted bytes [weight: %.2f, fat: %.2f, resistance: %d]", weight, bodyFat, resistance); + Timber.d("user [%s]", scaleUser); + Timber.d("scale measurement [%s]", scaleBtData); } addScaleData(scaleBtData); diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/lib/YunmaiLib.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/lib/YunmaiLib.java new file mode 100644 index 00000000..c8b0db8a --- /dev/null +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/lib/YunmaiLib.java @@ -0,0 +1,60 @@ +/* Copyright (C) 2018 olie.xdev + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + */ + +package com.health.openscale.core.bluetooth.lib; + +public class YunmaiLib { + private int sex; // male = 1; female = 0 + private float height; + + public YunmaiLib(int sex, float height) { + this.sex = sex; + this.height = height; + } + + public float getWater(float bodyFat) { + return ((100.0f - bodyFat) * 0.726f * 100.0f + 0.5f) / 100.0f; + } + + public float getMuscle(float bodyFat) { + float muscle; + muscle = (100.0f - bodyFat) * 0.67f; + + if (sex == 1) { + muscle = (100.0f - bodyFat) * 0.7f; + } + + muscle = ((muscle * 100.0f) + 0.5f) / 100.0f; + + return muscle; + } + + public float getBoneMass(float muscle, float weight) { + float boneMass; + + float h = height - 170.0f; + + if (sex == 1) { + boneMass = ((weight * (muscle / 100.0f) * 4.0f) / 7.0f * 0.22f * 0.6f) + (h / 100.0f); + } else { + boneMass = ((weight * (muscle / 100.0f) * 4.0f) / 7.0f * 0.34f * 0.45f) + (h / 100.0f); + } + + boneMass = ((boneMass * 10.0f) + 0.5f) / 10.0f; + + return boneMass; + } +}