@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_LE, 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_LE, offset);
			offset += 2;
		}

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

		onRSCMeasurementReceived(device, statusRunning, speed, cadence, strideLength, totalDistance);
	}
@Test
	public void onInvalidDataReceived() {
		final ProfileReadResponse response = new RunningSpeedAndCadenceMeasurementDataCallback() {
			@Override
			public void onRSCMeasurementReceived(@NonNull final BluetoothDevice device, final boolean running,
												 final float instantaneousSpeed, final int instantaneousCadence,
												 @Nullable final Integer strideLength,
												 @Nullable final Long totalDistance) {
				called = true;
			}

			@Override
			public void onInvalidDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
				super.onInvalidDataReceived(device, data);
				invalidData = true;
			}
		};

		final MutableData data = new MutableData(new byte[9]); // too short
		data.setByte(0x7, 0);
		data.setValue(3 * 256, Data.FORMAT_UINT16_LE, 1);
		data.setValue(18, Data.FORMAT_UINT8, 3);
		data.setValue(86, Data.FORMAT_UINT16_LE, 4);
		data.setValue(0, Data.FORMAT_UINT24_LE, 6);
		called = false;
		invalidData = false;
		response.onDataReceived(null, data);
		assertFalse(response.isValid());
		assertFalse(called);
		assertTrue(invalidData);
	}