@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_LE, 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_LE, 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);
	}
@Test
	public void onInvalidDataReceived() {
		final MutableData data = new MutableData(new byte[5]);
		data.setValue(0xFF, Data.FORMAT_UINT8, 0); // Flags
		data.setValue(0, Data.FORMAT_UINT16_LE, 1); // Sequence number
		data.setValue(0xb3, Data.FORMAT_UINT8, 3); // Extended flags - ignored
		data.setValue(3, Data.FORMAT_UINT8, 4); // Carbohydrate: dinner
		data.setValue(100.0f, Data.FORMAT_SFLOAT, 5); // Carbohydrate Amount
		data.setValue(4, Data.FORMAT_UINT8, 7); // Meal: casual
		data.setValue(0x12, Data.FORMAT_UINT8, 8); // Tester and Health (health care practitioner, minor issues)
		data.setValue(60, Data.FORMAT_UINT16_LE, 9); // 1 minute of exercise
		data.setValue(50, Data.FORMAT_UINT8, 11); // 50%
		data.setValue(4, Data.FORMAT_UINT8, 12); // Long acting insulin
		data.setValue(123.45f, Data.FORMAT_SFLOAT, 13); // 123.45 ml
		callback.onDataReceived(null, data);
		assertTrue(invalidData);
	}