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

The following examples show how to use no.nordicsemi.android.ble.data.Data#getByte() . 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: CyclingSpeedAndCadenceMeasurementDataCallback.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() < 1) {
		onInvalidDataReceived(device, data);
		return;
	}

	// Decode the new data
	int offset = 0;
	final int flags = data.getByte(offset);
	offset += 1;

	final boolean wheelRevPresent = (flags & 0x01) != 0;
	final boolean crankRevPreset = (flags & 0x02) != 0;

	if (data.size() < 1 + (wheelRevPresent ? 6 : 0) + (crankRevPreset ? 4 : 0)) {
		onInvalidDataReceived(device, data);
		return;
	}

	if (wheelRevPresent) {
		final long wheelRevolutions = data.getIntValue(Data.FORMAT_UINT32, offset) & 0xFFFFFFFFL;
		offset += 4;

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

		if (mInitialWheelRevolutions < 0)
			mInitialWheelRevolutions = wheelRevolutions;

		// Notify listener about the new measurement
		onWheelMeasurementReceived(device, wheelRevolutions, lastWheelEventTime);
	}

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

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

		// Notify listener about the new measurement
		onCrankMeasurementReceived(device, crankRevolutions, lastCrankEventTime);
	}
}
 
Example 2
Source File: CyclingSpeedAndCadenceMeasurementDataCallback.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() < 1) {
		onInvalidDataReceived(device, data);
		return;
	}

	// Decode the new data
	int offset = 0;
	final int flags = data.getByte(offset);
	offset += 1;

	final boolean wheelRevPresent = (flags & 0x01) != 0;
	final boolean crankRevPreset = (flags & 0x02) != 0;

	if (data.size() < 1 + (wheelRevPresent ? 6 : 0) + (crankRevPreset ? 4 : 0)) {
		onInvalidDataReceived(device, data);
		return;
	}

	if (wheelRevPresent) {
		final long wheelRevolutions = data.getIntValue(Data.FORMAT_UINT32, offset) & 0xFFFFFFFFL;
		offset += 4;

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

		if (mInitialWheelRevolutions < 0)
			mInitialWheelRevolutions = wheelRevolutions;

		// Notify listener about the new measurement
		onWheelMeasurementReceived(device, wheelRevolutions, lastWheelEventTime);
	}

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

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

		// Notify listener about the new measurement
		onCrankMeasurementReceived(device, crankRevolutions, lastCrankEventTime);
	}
}
 
Example 3
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();
}