Java Code Examples for no.nordicsemi.android.ble.data.Data#getIntValue()

The following examples show how to use no.nordicsemi.android.ble.data.Data#getIntValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CGMSessionRunTimeDataCallback.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
	super.onDataReceived(device, data);

	if (data.size() != 2 && data.size() != 4) {
		onInvalidDataReceived(device, data);
		return;
	}

	final int sessionRunTime = data.getIntValue(Data.FORMAT_UINT16, 0);

	final boolean crcPresent = data.size() == 4;
	if (crcPresent) {
		final int actualCrc = CRC16.MCRF4XX(data.getValue(), 0, 2);
		final int expectedCrc = data.getIntValue(Data.FORMAT_UINT16, 2);
		if (actualCrc != expectedCrc) {
			onContinuousGlucoseMonitorSessionRunTimeReceivedWithCrcError(device, data);
			return;
		}
	}

	onContinuousGlucoseMonitorSessionRunTimeReceived(device, sessionRunTime, crcPresent);
}
 
Example 2
Source File: BodySensorLocationParser.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String parse(final Data data) {
	final int value = data.getIntValue(Data.FORMAT_UINT8, 0);

	switch (value) {
		case 6: return "Foot";
		case 5: return "Ear Lobe";
		case 4: return "Hand";
		case 3: return "Finger";
		case 2: return "Wrist";
		case 1: return "Chest";
		case 0:
		default: return "Other";
	}
}
 
Example 3
Source File: DSTOffsetDataCallback.java    From Android-BLE-Common-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns the Daylight Saving Time Offset, or null if offset is out of data range.
 *
 * @param data input data.
 * @param offset offset to read DST Offset from.
 * @return DSTOffset field or null.
 */
@Nullable
public static DSTOffset readDSTOffset(@NonNull final Data data, final int offset) {
	if (data.size() < offset + 1)
		return null;

	final int o = data.getIntValue(Data.FORMAT_UINT8, offset);
	return DSTOffset.from(o);
}
 
Example 4
Source File: DateTimeDataCallback.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns a Gregorian Calendar object with YEAR, MONTH, DATE, HOUR, MINUTE, SECONDS set from
 * the data at given offset using the Date Time characteristic format.
 * MILLISECONDS are set to 0. Time Zone and DST offset are from the local time zone.
 * <p>
 * If YEAR, MONTH or DATE are set to 0 in the data, the corresponding fields in the calendar are 'unset',
 * that is {@code calendar.isSet(Calendar.YEAR)} returns false.
 * </p>
 *
 * @param data input data (7 bytes required).
 * @param offset offset to read from.
 * @return Calendar object or null.
 */
@Nullable
public static Calendar readDateTime(@NonNull final Data data, final int offset) {
	if (data.size() < offset + 7)
		return null;

	final Calendar calendar = Calendar.getInstance();
	final int year = data.getIntValue(Data.FORMAT_UINT16, offset);
	final int month = data.getIntValue(Data.FORMAT_UINT8, offset + 2);
	final int day = data.getIntValue(Data.FORMAT_UINT8, offset + 3);
	if (year > 0)
		calendar.set(Calendar.YEAR, year);
	else
		calendar.clear(Calendar.YEAR);
	if (month > 0)
		calendar.set(Calendar.MONTH, month - 1); // months are 1-based in Date Time characteristic
	else
		calendar.clear(Calendar.MONTH);
	if (day > 0)
		calendar.set(Calendar.DATE, day);
	else
		calendar.clear(Calendar.DATE);
	calendar.set(Calendar.HOUR_OF_DAY, data.getIntValue(Data.FORMAT_UINT8, offset + 4));
	calendar.set(Calendar.MINUTE, data.getIntValue(Data.FORMAT_UINT8, offset + 5));
	calendar.set(Calendar.SECOND, data.getIntValue(Data.FORMAT_UINT8, offset + 6));
	calendar.set(Calendar.MILLISECOND, 0);
	return calendar;
}
 
Example 5
Source File: SensorLocationDataCallback.java    From Android-BLE-Common-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
	super.onDataReceived(device, data);

	if (data.size() != 1) {
		onInvalidDataReceived(device, data);
		return;
	}

	final int location = data.getIntValue(Data.FORMAT_UINT8, 0);
	onSensorLocationReceived(device, location);
}
 
Example 6
Source File: CGMFeatureDataCallback.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
	super.onDataReceived(device, data);

	if (data.size() != 6) {
		onInvalidDataReceived(device, data);
		return;
	}

	final int featuresValue = data.getIntValue(Data.FORMAT_UINT24, 0);
	final int typeAndSampleLocation = data.getIntValue(Data.FORMAT_UINT8, 3);
	final int expectedCrc = data.getIntValue(Data.FORMAT_UINT16, 4);

	final CGMFeatures features = new CGMFeatures(featuresValue);
	if (features.e2eCrcSupported) {
		final int actualCrc = CRC16.MCRF4XX(data.getValue(), 0, 4);
		if (actualCrc != expectedCrc) {
			onContinuousGlucoseMonitorFeaturesReceivedWithCrcError(device, data);
			return;
		}
	} else {
		// If the device doesn't support E2E-safety the value of the field shall be set to 0xFFFF.
		if (expectedCrc != 0xFFFF) {
			onInvalidDataReceived(device, data);
			return;
		}
	}

	@SuppressLint("WrongConstant")
	final int type = typeAndSampleLocation & 0x0F; // least significant nibble
	final int sampleLocation = typeAndSampleLocation >> 4; // most significant nibble

	onContinuousGlucoseMonitorFeaturesReceived(device, features, type, sampleLocation, features.e2eCrcSupported);
}
 
Example 7
Source File: AlertLevelDataCallback.java    From Android-BLE-Common-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
	super.onDataReceived(device, data);

	if (data.size() == 1) {
		final Integer level = data.getIntValue(Data.FORMAT_UINT8, 0);
		if (level != null && level <= AlertLevelCallback.ALERT_HIGH) {
			onAlertLevelChanged(device, level);
			return;
		}
	}
	onInvalidDataReceived(device, data);
}
 
Example 8
Source File: RSCMeasurementParser.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String parse(final Data data) {
	int offset = 0;
	final int flags = data.getValue()[offset]; // 1 byte
	offset += 1;

	final boolean islmPresent = (flags & INSTANTANEOUS_STRIDE_LENGTH_PRESENT) > 0;
	final boolean tdPreset = (flags & TOTAL_DISTANCE_PRESENT) > 0;
	final boolean running = (flags & WALKING_OR_RUNNING_STATUS_BITS) > 0;
	final boolean walking = !running;

	final float instantaneousSpeed = (float) data.getIntValue(Data.FORMAT_UINT16, offset) / 256.0f; // 1/256 m/s
	offset += 2;

	final int instantaneousCadence = data.getIntValue(Data.FORMAT_UINT8, offset);
	offset += 1;

	float instantaneousStrideLength = 0;
	if (islmPresent) {
		instantaneousStrideLength = (float) data.getIntValue(Data.FORMAT_UINT16, offset) / 100.0f; // 1/100 m
		offset += 2;
	}

	float totalDistance = 0;
	if (tdPreset) {
		totalDistance = (float) data.getIntValue(Data.FORMAT_UINT32, offset) / 10.0f;
		// offset += 4;
	}

	final StringBuilder builder = new StringBuilder();
	builder.append(String.format(Locale.US, "Speed: %.2f m/s, Cadence: %d RPM,\n", instantaneousSpeed, instantaneousCadence));
	if (islmPresent)
		builder.append(String.format(Locale.US, "Instantaneous Stride Length: %.2f m,\n", instantaneousStrideLength));
	if (tdPreset)
		builder.append(String.format(Locale.US, "Total Distance: %.1f m,\n", totalDistance));
	if (walking)
		builder.append("Status: WALKING");
	else
		builder.append("Status: RUNNING");
	return builder.toString();
}
 
Example 9
Source File: SensorLocationDataCallback.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
	super.onDataReceived(device, data);

	if (data.size() != 1) {
		onInvalidDataReceived(device, data);
		return;
	}

	final int location = data.getIntValue(Data.FORMAT_UINT8, 0);
	onSensorLocationReceived(device, location);
}
 
Example 10
Source File: TemplateDataCallback.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
	if (data.size() < 2) {
		onInvalidDataReceived(device, data);
		return;
	}

	// Read flags
	int offset = 0;
	final int flags = data.getIntValue(Data.FORMAT_UINT8, offset);
	final int hearRateType = (flags & 0x01) == 0 ? Data.FORMAT_UINT8 : Data.FORMAT_UINT16;
	offset += 1;

	// Validate packet length. The type's lower nibble is its length.
	if (data.size() < 1 + (hearRateType & 0x0F)) {
		onInvalidDataReceived(device, data);
		return;
	}

	final int value = data.getIntValue(hearRateType, offset);
	// offset += hearRateType & 0xF;

	// ...

	// Report the parsed value(s)
	onSampleValueReceived(device, value);
}
 
Example 11
Source File: BlinkyLedDataCallback.java    From Android-nRF-Blinky with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void parse(@NonNull final BluetoothDevice device, @NonNull final Data data) {
    if (data.size() != 1) {
        onInvalidDataReceived(device, data);
        return;
    }

    final int state = data.getIntValue(Data.FORMAT_UINT8, 0);
    if (state == STATE_ON) {
        onLedStateChanged(device, true);
    } else if (state == STATE_OFF) {
        onLedStateChanged(device, false);
    } else {
        onInvalidDataReceived(device, data);
    }
}
 
Example 12
Source File: GlucoseFeatureDataCallback.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
	super.onDataReceived(device, data);

	if (data.size() != 2) {
		onInvalidDataReceived(device, data);
		return;
	}

	final int value = data.getIntValue(Data.FORMAT_UINT16, 0);
	final GlucoseFeatures features = new GlucoseFeatures(value);
	onGlucoseFeaturesReceived(device, features);
}
 
Example 13
Source File: BodySensorLocationDataCallback.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
	super.onDataReceived(device, data);

	if (data.size() < 1) {
		onInvalidDataReceived(device, data);
		return;
	}

	final int sensorLocation = data.getIntValue(Data.FORMAT_UINT8, 0);
	onBodySensorLocationReceived(device, sensorLocation);
}
 
Example 14
Source File: RunningSpeedAndCadenceMeasurementDataCallback.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
	super.onDataReceived(device, data);

	if (data.size() < 4) {
		onInvalidDataReceived(device, data);
		return;
	}

	int offset = 0;
	final int flags = data.getIntValue(Data.FORMAT_UINT8, offset);
	final boolean instantaneousStrideLengthPresent = (flags & 0x01) != 0;
	final boolean totalDistancePresent = (flags & 0x02) != 0;
	final boolean statusRunning = (flags & 0x04) != 0;
	offset += 1;

	final float speed = data.getIntValue(Data.FORMAT_UINT16, offset) / 256.f; // [m/s]
	offset += 2;
	final int cadence = data.getIntValue(Data.FORMAT_UINT8, offset);
	offset += 1;

	if (data.size() < 4
			+ (instantaneousStrideLengthPresent ? 2 : 0)
			+ (totalDistancePresent ? 4 : 0)) {
		onInvalidDataReceived(device, data);
		return;
	}

	Integer strideLength = null;
	if (instantaneousStrideLengthPresent) {
		strideLength = data.getIntValue(Data.FORMAT_UINT16, offset);
		offset += 2;
	}

	Long totalDistance = null;
	if (totalDistancePresent) {
		totalDistance = data.getLongValue(Data.FORMAT_UINT32, offset);
		// offset += 4;
	}

	onRSCMeasurementReceived(device, statusRunning, speed, cadence, strideLength, totalDistance);
}
 
Example 15
Source File: GlucoseMeasurementContextParser.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static String parse(final Data data) {
	final StringBuilder builder = new StringBuilder();

	int offset = 0;
	final int flags = data.getIntValue(Data.FORMAT_UINT8, offset);
	offset += 1;

	final boolean carbohydratePresent = (flags & 0x01) > 0;
	final boolean mealPresent = (flags & 0x02) > 0;
	final boolean testerHealthPresent = (flags & 0x04) > 0;
	final boolean exercisePresent = (flags & 0x08) > 0;
	final boolean medicationPresent = (flags & 0x10) > 0;
	final int medicationUnit = (flags & 0x20) > 0 ? UNIT_l : UNIT_kg;
	final boolean hbA1cPresent = (flags & 0x40) > 0;
	final boolean moreFlagsPresent = (flags & 0x80) > 0;

	final int sequenceNumber = data.getIntValue(Data.FORMAT_UINT16, offset);
	offset += 2;

	if (moreFlagsPresent) // not supported yet
		offset += 1;

	builder.append("Sequence number: ").append(sequenceNumber);

	if (carbohydratePresent) {
		final int carbohydrateId = data.getIntValue(Data.FORMAT_UINT8, offset);
		final float carbohydrateUnits = data.getFloatValue(Data.FORMAT_SFLOAT, offset + 1);
		builder.append("\nCarbohydrate: ").append(getCarbohydrate(carbohydrateId)).append(" (").append(carbohydrateUnits).append(carbohydrateUnits == UNIT_kg ? "kg" : "l").append(")");
		offset += 3;
	}

	if (mealPresent) {
		final int meal = data.getIntValue(Data.FORMAT_UINT8, offset);
		builder.append("\nMeal: ").append(getMeal(meal));
		offset += 1;
	}

	if (testerHealthPresent) {
		final int testerHealth = data.getIntValue(Data.FORMAT_UINT8, offset);
		final int tester = (testerHealth & 0xF0) >> 4;
		final int health = (testerHealth & 0x0F);
		builder.append("\nTester: ").append(getTester(tester));
		builder.append("\nHealth: ").append(getHealth(health));
		offset += 1;
	}

	if (exercisePresent) {
		final int exerciseDuration = data.getIntValue(Data.FORMAT_UINT16, offset);
		final int exerciseIntensity = data.getIntValue(Data.FORMAT_UINT8, offset + 2);
		builder.append("\nExercise duration: ").append(exerciseDuration).append("s (intensity ").append(exerciseIntensity).append("%)");
		offset += 3;
	}

	if (medicationPresent) {
		final int medicationId = data.getIntValue(Data.FORMAT_UINT8, offset);
		final float medicationQuantity = data.getFloatValue(Data.FORMAT_SFLOAT, offset + 1);
		builder.append("\nMedication: ").append(getMedicationId(medicationId)).append(" (").append(medicationQuantity).append(medicationUnit == UNIT_kg ? "kg" : "l");
		offset += 3;
	}

	if (hbA1cPresent) {
		final float HbA1c = data.getFloatValue(Data.FORMAT_SFLOAT, offset);
		builder.append("\nHbA1c: ").append(HbA1c).append("%");
	}
	return builder.toString();
}
 
Example 16
Source File: BloodPressureMeasurementParser.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static String parse(final Data data) {
	final StringBuilder builder = new StringBuilder();

	// first byte - flags
	int offset = 0;
	final int flags = data.getIntValue(Data.FORMAT_UINT8, offset++);

	final int unitType = flags & 0x01;
	final boolean timestampPresent = (flags & 0x02) > 0;
	final boolean pulseRatePresent = (flags & 0x04) > 0;
	final boolean userIdPresent = (flags & 0x08) > 0;
	final boolean statusPresent = (flags & 0x10) > 0;

	// following bytes - systolic, diastolic and mean arterial pressure 
	final float systolic = data.getFloatValue(Data.FORMAT_SFLOAT, offset);
	final float diastolic = data.getFloatValue(Data.FORMAT_SFLOAT, offset + 2);
	final float meanArterialPressure = data.getFloatValue(Data.FORMAT_SFLOAT, offset + 4);
	final String unit = unitType == 0 ? " mmHg" : " kPa";
	offset += 6;
	builder.append("Systolic: ").append(systolic).append(unit);
	builder.append("\nDiastolic: ").append(diastolic).append(unit);
	builder.append("\nMean AP: ").append(meanArterialPressure).append(unit);

	// parse timestamp if present
	if (timestampPresent) {
		builder.append("\nTimestamp: ").append(DateTimeParser.parse(data, offset));
		offset += 7;
	}

	// parse pulse rate if present
	if (pulseRatePresent) {
		final float pulseRate = data.getFloatValue(Data.FORMAT_SFLOAT, offset);
		offset += 2;
		builder.append("\nPulse: ").append(pulseRate).append(" bpm");
	}

	if (userIdPresent) {
		final int userId = data.getIntValue(Data.FORMAT_UINT8, offset);
		offset += 1;
		builder.append("\nUser ID: ").append(userId);
	}

	if (statusPresent) {
		final int status = data.getIntValue(Data.FORMAT_UINT16, offset);
		// offset += 2;
		if ((status & 0x0001) > 0)
			builder.append("\nBody movement detected");
		if ((status & 0x0002) > 0)
			builder.append("\nCuff too lose");
		if ((status & 0x0004) > 0)
			builder.append("\nIrregular pulse detected");
		if ((status & 0x0018) == 0x0008)
			builder.append("\nPulse rate exceeds upper limit");
		if ((status & 0x0018) == 0x0010)
			builder.append("\nPulse rate is less than lower limit");
		if ((status & 0x0018) == 0x0018)
			builder.append("\nPulse rate range: Reserved for future use ");
		if ((status & 0x0020) > 0)
			builder.append("\nImproper measurement position");
	}

	return builder.toString();
}
 
Example 17
Source File: IntermediateCuffPressureDataCallback.java    From Android-BLE-Common-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
	super.onDataReceived(device, data);

	if (data.size() < 7) {
		onInvalidDataReceived(device, data);
		return;
	}
	// First byte: flags
	int offset = 0;
	final int flags = data.getIntValue(Data.FORMAT_UINT8, offset++);

	// See UNIT_* for unit options
	final int unit = (flags & 0x01) == UNIT_mmHg ? UNIT_mmHg : UNIT_kPa;
	final boolean timestampPresent         = (flags & 0x02) != 0;
	final boolean pulseRatePresent         = (flags & 0x04) != 0;
	final boolean userIdPresent            = (flags & 0x08) != 0;
	final boolean measurementStatusPresent = (flags & 0x10) != 0;

	if (data.size() < 7
			+ (timestampPresent ? 7 : 0) + (pulseRatePresent ? 2 : 0)
			+ (userIdPresent ? 1 : 0) + (measurementStatusPresent ? 2 : 0)) {
		onInvalidDataReceived(device, data);
		return;
	}

	// Following bytes - systolic, diastolic and mean arterial pressure
	final float cuffPressure = data.getFloatValue(Data.FORMAT_SFLOAT, offset);
	// final float ignored_1 = data.getFloatValue(Data.FORMAT_SFLOAT, offset + 2);
	// final float ignored_2 = data.getFloatValue(Data.FORMAT_SFLOAT, offset + 4);
	offset += 6;

	// Parse timestamp if present
	Calendar calendar = null;
	if (timestampPresent) {
		calendar = DateTimeDataCallback.readDateTime(data, offset);
		offset += 7;
	}

	// Parse pulse rate if present
	Float pulseRate = null;
	if (pulseRatePresent) {
		pulseRate = data.getFloatValue(Data.FORMAT_SFLOAT, offset);
		offset += 2;
	}

	// Read user id if present
	Integer userId = null;
	if (userIdPresent) {
		userId = data.getIntValue(Data.FORMAT_UINT8, offset);
		offset += 1;
	}

	// Read measurement status if present
	BPMStatus status = null;
	if (measurementStatusPresent) {
		final int measurementStatus = data.getIntValue(Data.FORMAT_UINT16, offset);
		// offset += 2;
		status = new BPMStatus(measurementStatus);
	}

	onIntermediateCuffPressureReceived(device, cuffPressure, unit, pulseRate, userId, status, calendar);
}
 
Example 18
Source File: RecordAccessControlPointDataCallback.java    From Android-BLE-Common-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
	super.onDataReceived(device, data);

	if (data.size() < 3) {
		onInvalidDataReceived(device, data);
		return;
	}

	final int opCode = data.getIntValue(Data.FORMAT_UINT8, 0);
	if (opCode != OP_CODE_NUMBER_OF_STORED_RECORDS_RESPONSE && opCode != OP_CODE_RESPONSE_CODE) {
		onInvalidDataReceived(device, data);
		return;
	}

	final int operator = data.getIntValue(Data.FORMAT_UINT8, 1);
	if (operator != OPERATOR_NULL) {
		onInvalidDataReceived(device, data);
		return;
	}

	switch (opCode) {
		case OP_CODE_NUMBER_OF_STORED_RECORDS_RESPONSE: {
			// Field size is defined per service
			int numberOfRecords;

			switch (data.size() - 2) {
				case 1:
					numberOfRecords = data.getIntValue(Data.FORMAT_UINT8, 2);
					break;
				case 2:
					numberOfRecords = data.getIntValue(Data.FORMAT_UINT16, 2);
					break;
				case 4:
					numberOfRecords = data.getIntValue(Data.FORMAT_UINT32, 2);
					break;
				default:
					// Other field sizes are not supported
					onInvalidDataReceived(device, data);
					return;
			}
			onNumberOfRecordsReceived(device, numberOfRecords);
			break;
		}
		case OP_CODE_RESPONSE_CODE: {
			if (data.size() != 4) {
				onInvalidDataReceived(device, data);
				return;
			}

			final int requestCode = data.getIntValue(Data.FORMAT_UINT8, 2);
			final int responseCode = data.getIntValue(Data.FORMAT_UINT8, 3);
			if (responseCode == RACP_RESPONSE_SUCCESS) {
				onRecordAccessOperationCompleted(device, requestCode);
			} else if (responseCode == RACP_ERROR_NO_RECORDS_FOUND) {
				onRecordAccessOperationCompletedWithNoRecordsFound(device, requestCode);
			} else {
				onRecordAccessOperationError(device, requestCode, responseCode);
			}
			break;
		}
	}
}
 
Example 19
Source File: CSCMeasurementParser.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static String parse(final Data data) {
	int offset = 0;
	final int flags = data.getByte(offset); // 1 byte
	offset += 1;

	final boolean wheelRevPresent = (flags & WHEEL_REV_DATA_PRESENT) > 0;
	final boolean crankRevPreset = (flags & CRANK_REV_DATA_PRESENT) > 0;

	int wheelRevolutions = 0;
	int lastWheelEventTime = 0;
	if (wheelRevPresent) {
		wheelRevolutions = data.getIntValue(Data.FORMAT_UINT32, offset);
		offset += 4;

		lastWheelEventTime = data.getIntValue(Data.FORMAT_UINT16, offset); // 1/1024 s
		offset += 2;
	}

	int crankRevolutions = 0;
	int lastCrankEventTime = 0;
	if (crankRevPreset) {
		crankRevolutions = data.getIntValue(Data.FORMAT_UINT16, offset);
		offset += 2;

		lastCrankEventTime = data.getIntValue(Data.FORMAT_UINT16, offset);
		//offset += 2;
	}

	final StringBuilder builder = new StringBuilder();
	if (wheelRevPresent) {
		builder.append("Wheel rev: ").append(wheelRevolutions).append(",\n");
		builder.append("Last wheel event time: ").append(lastWheelEventTime).append(",\n");
	}
	if (crankRevPreset) {
		builder.append("Crank rev: ").append(crankRevolutions).append(",\n");
		builder.append("Last crank event time: ").append(lastCrankEventTime).append(",\n");
	}
	if (!wheelRevPresent && !crankRevPreset) {
		builder.append("No wheel or crank data");
	}
	builder.setLength(builder.length() - 2);
	return builder.toString();
}
 
Example 20
Source File: GlucoseMeasurementContextDataCallback.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
	super.onDataReceived(device, data);
	
	if (data.size() < 3) {
		onInvalidDataReceived(device, data);
		return;
	}

	int offset = 0;

	final int flags = data.getIntValue(Data.FORMAT_UINT8, offset++);
	final boolean carbohydratePresent = (flags & 0x01) != 0;
	final boolean mealPresent = (flags & 0x02) != 0;
	final boolean testerHealthPresent = (flags & 0x04) != 0;
	final boolean exercisePresent = (flags & 0x08) != 0;
	final boolean medicationPresent = (flags & 0x10) != 0;
	final boolean medicationUnitLiter = (flags & 0x20) != 0;
	final boolean HbA1cPresent = (flags & 0x40) != 0;
	final boolean extendedFlagsPresent = (flags & 0x80) != 0;

	if (data.size() < 3 + (carbohydratePresent ? 3 : 0) + (mealPresent ? 1 : 0) + (testerHealthPresent ? 1 : 0)
			+ (exercisePresent ? 3 : 0) + (medicationPresent ? 3 : 0) + (HbA1cPresent ? 2 : 0)
			+ (extendedFlagsPresent ? 1 : 0)) {
		onInvalidDataReceived(device, data);
		return;
	}

	final int sequenceNumber = data.getIntValue(Data.FORMAT_UINT16, offset);
	offset += 2;

	// Optional fields
	if (extendedFlagsPresent) {
		// ignore extended flags
		offset += 1;
	}

	Carbohydrate carbohydrate = null;
	Float carbohydrateAmount = null;
	if (carbohydratePresent) {
		final int carbohydrateId = data.getIntValue(Data.FORMAT_UINT8, offset);
		carbohydrate = Carbohydrate.from(carbohydrateId);
		carbohydrateAmount = data.getFloatValue(Data.FORMAT_SFLOAT, offset + 1); // in grams
		offset += 3;
	}

	Meal meal = null;
	if (mealPresent) {
		final int mealId = data.getIntValue(Data.FORMAT_UINT8, offset);
		meal = Meal.from(mealId);
		offset += 1;
	}

	Tester tester = null;
	Health health = null;
	if (testerHealthPresent) {
		final int testerAndHealth = data.getIntValue(Data.FORMAT_UINT8, offset);
		tester = Tester.from(testerAndHealth & 0x0F);
		health = Health.from(testerAndHealth >> 4);
		offset += 1;
	}

	Integer exerciseDuration = null;
	Integer exerciseIntensity = null;
	if (exercisePresent) {
		exerciseDuration = data.getIntValue(Data.FORMAT_UINT16, offset); // in seconds
		exerciseIntensity = data.getIntValue(Data.FORMAT_UINT8, offset + 2); // in percentage
		offset += 3;
	}

	Medication medication = null;
	Float medicationAmount = null;
	Integer medicationUnit = null;
	if (medicationPresent) {
		final int medicationId = data.getIntValue(Data.FORMAT_UINT8, offset);
		medication = Medication.from(medicationId);
		medicationAmount = data.getFloatValue(Data.FORMAT_SFLOAT, offset + 1); // mg or ml
		medicationUnit = medicationUnitLiter ? UNIT_ml : UNIT_mg;
		offset += 3;
	}

	Float HbA1c = null;
	if (HbA1cPresent) {
		HbA1c = data.getFloatValue(Data.FORMAT_SFLOAT, offset);
		// offset += 2;
	}

	onGlucoseMeasurementContextReceived(device, sequenceNumber, carbohydrate, carbohydrateAmount,
			meal, tester, health, exerciseDuration, exerciseIntensity,
			medication, medicationAmount, medicationUnit, HbA1c);
}