From 5dd254f7c2973ba275e1a5ac0ff12162914d3f01 Mon Sep 17 00:00:00 2001 From: OliE Date: Fri, 26 Jan 2018 17:33:43 +0100 Subject: [PATCH] first Bluetooth 1byone scale implementation. --- .../bluetooth/BluetoothCommunication.java | 4 +- .../core/bluetooth/BluetoothHesley.java | 1 + .../core/bluetooth/BluetoothOneByone.java | 112 ++++++++++++++++++ 3 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothOneByone.java diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java index b7e168cf..ea658bc2 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java @@ -41,7 +41,7 @@ public abstract class BluetoothCommunication { public enum BT_MACHINE_STATE {BT_INIT_STATE, BT_CMD_STATE, BT_CLEANUP_STATE} public enum BT_DEVICE_ID {CUSTOM_OPENSCALE, MI_SCALE_V1, MI_SCALE_V2, SANITAS_SBF70, MEDISANA_BS444, DIGOO_DGS038H, EXCELVANT_CF369BLE, - YUNMAI_MINI, YUNMAI_SE, MGB, EXINGTECH_Y1, BEURER_BF700_800, HESLEY} + YUNMAI_MINI, YUNMAI_SE, MGB, EXINGTECH_Y1, BEURER_BF700_800, HESLEY, ONEBYONE} protected Context context; @@ -106,6 +106,8 @@ public abstract class BluetoothCommunication { return new BluetoothBeurerBF700_800(context); case HESLEY: return new BluetoothHesley(context); + case ONEBYONE: + return new BluetoothOneByone(context); } return null; diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothHesley.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothHesley.java index 9c94b70d..30546d63 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothHesley.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothHesley.java @@ -55,6 +55,7 @@ public class BluetoothHesley extends BluetoothCommunication { case 1: byte[] magicBytes = {(byte)0xa5, (byte)0x01, (byte)0x2c, (byte)0xab, (byte)0x50, (byte)0x5a, (byte)0x29}; writeBytes(WEIGHT_MEASUREMENT_SERVICE, CMD_MEASUREMENT_CHARACTERISTIC, magicBytes); + break; default: return false; } diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothOneByone.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothOneByone.java new file mode 100644 index 00000000..846bc16a --- /dev/null +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothOneByone.java @@ -0,0 +1,112 @@ +/* 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; + +import android.bluetooth.BluetoothGatt; +import android.bluetooth.BluetoothGattCharacteristic; +import android.content.Context; +import android.widget.Toast; + +import com.health.openscale.core.datatypes.ScaleMeasurement; + +import java.util.Date; +import java.util.UUID; + +public class BluetoothOneByone extends BluetoothCommunication { + private final UUID WEIGHT_MEASUREMENT_SERVICE_BODY_COMPOSITION = UUID.fromString("0000181B-0000-1000-8000-00805f9b34fb"); + private final UUID WEIGHT_MEASUREMENT_CHARACTERISTIC_BODY_COMPOSITION = UUID.fromString("00002A9C-0000-1000-8000-00805f9b34fb"); // read, indication + + private final UUID WEIGHT_MEASUREMENT_SERVICE = UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb"); + private final UUID CMD_MEASUREMENT_CHARACTERISTIC = UUID.fromString("0000fff1-0000-1000-8000-00805f9b34fb"); // write only + private final UUID WEIGHT_MEASUREMENT_CONFIG = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"); + + public BluetoothOneByone(Context context) { + super(context); + } + + @Override + public String deviceName() { + return "1byone scale"; + } + + @Override + public String defaultDeviceName() { + return "Health Scale"; + } + + @Override + boolean nextInitCmd(int stateNr) { + switch (stateNr) { + case 0: + setIndicationOn(WEIGHT_MEASUREMENT_SERVICE_BODY_COMPOSITION, WEIGHT_MEASUREMENT_CHARACTERISTIC_BODY_COMPOSITION, WEIGHT_MEASUREMENT_CONFIG); + break; + case 1: + byte[] magicBytes = {(byte)0xfd,(byte)0x37,(byte)0x01,(byte)0x01,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0xca}; + writeBytes(WEIGHT_MEASUREMENT_SERVICE, CMD_MEASUREMENT_CHARACTERISTIC, magicBytes); + break; + default: + return false; + } + + return true; + } + + @Override + boolean nextBluetoothCmd(int stateNr) { + return false; + } + + @Override + boolean nextCleanUpCmd(int stateNr) { + return false; + } + + @Override + 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 != null && data.length > 0) { + // if data is valid data + if (data.length == 16) { + parseBytes(data); + } + } + } + + private void parseBytes(byte[] weightBytes) { + float weight = (float) (((weightBytes[4] & 0xFF) << 8) | (weightBytes[5] & 0xFF)) / 10.0f; // kg + float fat = (float)(((weightBytes[6] & 0xFF) << 8) | (weightBytes[7] & 0xFF)) / 10.0f; // % + float bone = (float)(((weightBytes[8] & 0xFF) & 0xFF)) / 10.0f; // % + float muscle = (float)(((weightBytes[9] & 0xFF) << 8) | (weightBytes[10] & 0xFF)) / 10.0f; // % + float visfat = (float)(((weightBytes[11] & 0xFF) & 0xFF)) / 10.0f; // % + float water = (float)(((weightBytes[12] & 0xFF) << 8) | (weightBytes[13] & 0xFF)) / 10.0f; // % + float bmr = (float)(((weightBytes[14] & 0xFF) << 8) | (weightBytes[15] & 0xFF)); // kcal + + ScaleMeasurement scaleBtData = new ScaleMeasurement(); + + scaleBtData.setWeight(weight); + scaleBtData.setFat(fat); + scaleBtData.setMuscle(muscle); + scaleBtData.setWater(water); + scaleBtData.setBone(bone); + scaleBtData.setDateTime(new Date()); + + addScaleData(scaleBtData); + } +}