mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-22 08:13:43 +02:00
@@ -19,11 +19,8 @@ package com.health.openscale.core.bluetooth;
|
|||||||
import android.bluetooth.BluetoothGatt;
|
import android.bluetooth.BluetoothGatt;
|
||||||
import android.bluetooth.BluetoothGattCharacteristic;
|
import android.bluetooth.BluetoothGattCharacteristic;
|
||||||
import android.content.Context;
|
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.ScaleMeasurement;
|
||||||
import com.health.openscale.core.datatypes.ScaleUser;
|
|
||||||
import com.health.openscale.core.utils.Converters;
|
import com.health.openscale.core.utils.Converters;
|
||||||
|
|
||||||
import java.util.UUID;
|
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);
|
setIndicationOn(WEIGHT_MEASUREMENT_SERVICE_BODY_COMPOSITION, WEIGHT_MEASUREMENT_CHARACTERISTIC_BODY_COMPOSITION, WEIGHT_MEASUREMENT_CONFIG);
|
||||||
break;
|
break;
|
||||||
case 1:
|
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};
|
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] =
|
magicBytes[magicBytes.length - 1] =
|
||||||
xorChecksum(magicBytes, 0, magicBytes.length - 1);
|
xorChecksum(magicBytes, 0, magicBytes.length - 1);
|
||||||
writeBytes(WEIGHT_MEASUREMENT_SERVICE, CMD_MEASUREMENT_CHARACTERISTIC, magicBytes);
|
writeBytes(WEIGHT_MEASUREMENT_SERVICE, CMD_MEASUREMENT_CHARACTERISTIC, magicBytes);
|
||||||
break;
|
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:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -108,30 +76,18 @@ public class BluetoothOneByone extends BluetoothCommunication {
|
|||||||
public void onBluetoothDataChange(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic gattCharacteristic) {
|
public void onBluetoothDataChange(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic gattCharacteristic) {
|
||||||
final byte[] data = gattCharacteristic.getValue();
|
final byte[] data = gattCharacteristic.getValue();
|
||||||
|
|
||||||
Toast.makeText(context, "Log Data: " + byteInHex(data), Toast.LENGTH_LONG).show();
|
|
||||||
|
|
||||||
// if data is valid data
|
// if data is valid data
|
||||||
if (data != null && data.length == 16) {
|
if (data != null && data.length == 20) {
|
||||||
parseBytes(data);
|
parseBytes(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void parseBytes(byte[] weightBytes) {
|
private void parseBytes(byte[] weightBytes) {
|
||||||
float weight = Converters.fromUnsignedInt16Be(weightBytes, 4) / 10.0f; // kg
|
float weight = Converters.fromUnsignedInt16Le(weightBytes, 11) / 100.0f;
|
||||||
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
|
|
||||||
|
|
||||||
ScaleMeasurement scaleBtData = new ScaleMeasurement();
|
ScaleMeasurement scaleBtData = new ScaleMeasurement();
|
||||||
|
|
||||||
scaleBtData.setWeight(weight);
|
scaleBtData.setWeight(weight);
|
||||||
scaleBtData.setFat(fat);
|
|
||||||
scaleBtData.setMuscle(muscle);
|
|
||||||
scaleBtData.setWater(water);
|
|
||||||
scaleBtData.setBone(bone);
|
|
||||||
|
|
||||||
addScaleData(scaleBtData);
|
addScaleData(scaleBtData);
|
||||||
}
|
}
|
||||||
|
@@ -124,6 +124,12 @@ public class Converters {
|
|||||||
return kg;
|
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) {
|
public static int fromUnsignedInt16Be(byte[] data, int offset) {
|
||||||
int value = (data[offset] & 0xFF) << 8;
|
int value = (data[offset] & 0xFF) << 8;
|
||||||
value += data[offset + 1] & 0xFF;
|
value += data[offset + 1] & 0xFF;
|
||||||
|
Reference in New Issue
Block a user