getWeightData method

WeightData? getWeightData(
  1. List<int>? data
)

Implementation

WeightData? getWeightData(List<int>? data) {
  if (data != null && data.length > 9) {
    int numberId = data[0] & 0xff;
    int status = data[1] & 0xff;

    if (_oldNumberId == numberId && _oldStatus == status) {
      // The data is the same, and the type is the same. It has been processed and will not be processed again.
      return null;
    }

    _oldNumberId = numberId;
    _oldStatus = status;
    // Temperature unit: 0=℃, 1=℉
    int tempUnit = _getBits(data[2], 6, 1);
    // Weight unit:
    // 0000: kg
    // 0001: Jin
    // 0100: st:lb
    // 0110: lb
    int weightUnit = _getBits(data[2], 3, 3);
    // Decimal points in weight:
    // 00: No decimal point
    // 01: 1 decimal point
    // 10: 2 decimal points
    // 11: 3 decimal points
    int weightDecimal = _getBits(data[2], 1, 2);
    // 0: Real-time weight, 1: Stable weight
    int weightStatus = _getBits(data[2], 0, 1);
    // The highest bit = 0: Positive weight, 1: Negative weight
    int weightNegative = _getBits(data[3], 7, 1);
    // Weight, big-endian
    int weight = ((data[3] & 0x7f) << 8) | (data[4] & 0xff);
    // Impedance, big-endian
    int adc = ((data[5] & 0xff) << 8) + (data[6] & 0xff);
    // Body fat scale algorithm ID
    int algorithmId = data[7] & 0xff;
    // The highest bit = 0: Positive temperature, 1: Negative temperature
    int tempNegative = _getBits(data[8], 7, 1);
    int temp = ((data[8] & 0x7f) << 8) + (data[9] & 0xff);

    if (tempNegative == 1 && temp == 4095) {
      // No temperature measurement, indicated by 0xFFFF
      tempNegative = -1;
      temp = -1;
    }
    return WeightData(
      status,
      tempUnit,
      weightUnit,
      _weightUnitToString(weightUnit),
      weightDecimal,
      weightStatus,
      weightNegative,
      weight,
      _realWeight(weight, weightDecimal),
      adc,
      algorithmId,
      tempNegative,
      temp,
    );
  }
  return null;
}