no.nordicsemi.android.ble.common.callback.DateTimeDataCallback Java Examples

The following examples show how to use no.nordicsemi.android.ble.common.callback.DateTimeDataCallback. 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: TemperatureMeasurementDataCallback.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() < 5) {
		onInvalidDataReceived(device, data);
		return;
	}

	int offset = 0;
	final int flags = data.getIntValue(Data.FORMAT_UINT8, offset);
	final int unit = (flags & 0x01) == UNIT_C ? UNIT_C : UNIT_F;
	final boolean timestampPresent = (flags & 0x02) != 0;
	final boolean temperatureTypePresent = (flags & 0x04) != 0;
	offset += 1;

	if (data.size() < 5 + (timestampPresent ? 7 : 0) + (temperatureTypePresent ? 1 : 0)) {
		onInvalidDataReceived(device, data);
		return;
	}

	final float temperature = data.getFloatValue(Data.FORMAT_FLOAT, 1);
	offset += 4;

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

	Integer type = null;
	if (temperatureTypePresent) {
		type = data.getIntValue(Data.FORMAT_UINT8, offset);
		// offset += 1;
	}

	onTemperatureMeasurementReceived(device, temperature, unit, calendar, type);
}
 
Example #2
Source File: TemperatureMeasurementDataCallback.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() < 5) {
		onInvalidDataReceived(device, data);
		return;
	}

	int offset = 0;
	final int flags = data.getIntValue(Data.FORMAT_UINT8, offset);
	final int unit = (flags & 0x01) == UNIT_C ? UNIT_C : UNIT_F;
	final boolean timestampPresent = (flags & 0x02) != 0;
	final boolean temperatureTypePresent = (flags & 0x04) != 0;
	offset += 1;

	if (data.size() < 5 + (timestampPresent ? 7 : 0) + (temperatureTypePresent ? 1 : 0)) {
		onInvalidDataReceived(device, data);
		return;
	}

	final float temperature = data.getFloatValue(Data.FORMAT_FLOAT, 1);
	offset += 4;

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

	Integer type = null;
	if (temperatureTypePresent) {
		type = data.getIntValue(Data.FORMAT_UINT8, offset);
		// offset += 1;
	}

	onTemperatureMeasurementReceived(device, temperature, unit, calendar, type);
}
 
Example #3
Source File: BloodPressureMeasurementDataCallback.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 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);
	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);
	}

	onBloodPressureMeasurementReceived(device, systolic, diastolic, meanArterialPressure, unit, pulseRate, userId, status, calendar);
}
 
Example #4
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 #5
Source File: GlucoseMeasurementDataCallback.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() < 10) {
		onInvalidDataReceived(device, data);
		return;
	}

	int offset = 0;

	final int flags = data.getIntValue(Data.FORMAT_UINT8, offset++);
	final boolean timeOffsetPresent = (flags & 0x01) != 0;
	final boolean glucoseDataPresent = (flags & 0x02) != 0;
	final boolean unitMolL = (flags & 0x04) != 0;
	final boolean sensorStatusAnnunciationPresent = (flags & 0x08) != 0;
	final boolean contextInformationFollows = (flags & 0x10) != 0;

	if (data.size() < 10 + (timeOffsetPresent ? 2 : 0) + (glucoseDataPresent ? 3 : 0)
		+ (sensorStatusAnnunciationPresent ? 2 : 0)) {
		onInvalidDataReceived(device, data);
		return;
	}

	// Required fields
	final int sequenceNumber = data.getIntValue(Data.FORMAT_UINT16, offset);
	offset += 2;
	final Calendar baseTime = DateTimeDataCallback.readDateTime(data, 3);
	offset += 7;

	if (baseTime == null) {
		onInvalidDataReceived(device, data);
		return;
	}

	// Optional fields
	if (timeOffsetPresent) {
		final int timeOffset = data.getIntValue(Data.FORMAT_SINT16, offset);
		offset += 2;

		baseTime.add(Calendar.MINUTE, timeOffset);
	}

	Float glucoseConcentration = null;
	Integer unit = null;
	Integer type = null;
	Integer sampleLocation = null;
	if (glucoseDataPresent) {
		glucoseConcentration = data.getFloatValue(Data.FORMAT_SFLOAT, offset);
		final int typeAndSampleLocation = data.getIntValue(Data.FORMAT_UINT8, offset + 2);
		offset += 3;

		type = typeAndSampleLocation & 0x0F;
		sampleLocation = typeAndSampleLocation >> 4;
		unit = unitMolL ? UNIT_mol_L : UNIT_kg_L;
	}

	GlucoseStatus status = null;
	if (sensorStatusAnnunciationPresent) {
		final int value = data.getIntValue(Data.FORMAT_UINT16, offset);
		// offset += 2;

		status = new GlucoseStatus(value);
	}

	onGlucoseMeasurementReceived(device, sequenceNumber, baseTime /* with offset */,
			glucoseConcentration, unit, type, sampleLocation, status, contextInformationFollows);
}
 
Example #6
Source File: CGMSessionStartTimeDataCallback.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() != 9 && data.size() != 11) {
			onInvalidDataReceived(device, data);
			return;
		}

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

		final Calendar calendar = DateTimeDataCallback.readDateTime(data, 0);
		final Integer timeZoneOffset = TimeZoneDataCallback.readTimeZone(data, 7); // [minutes]
		final DSTOffsetCallback.DSTOffset dstOffset = DSTOffsetDataCallback.readDSTOffset(data, 8);

		if (calendar == null || timeZoneOffset == null || dstOffset == null) {
			onInvalidDataReceived(device, data);
			return;
		}

		final TimeZone timeZone = new TimeZone() {
			@Override
			public int getOffset(final int era, final int year, final int month, final int day, final int dayOfWeek, final int milliseconds) {
				return (timeZoneOffset + dstOffset.offset) * 60000; // convert minutes to milliseconds
			}

			@Override
			public void setRawOffset(final int offsetMillis) {
				throw new UnsupportedOperationException("Can't set raw offset for this TimeZone");
			}

			@Override
			public int getRawOffset() {
				return timeZoneOffset * 60000;
			}

			@Override
			public boolean useDaylightTime() {
				return true;
			}

			@Override
			public boolean inDaylightTime(final Date date) {
				// Use of DST is dependent on the input data only
				return dstOffset.offset > 0;
			}

			@Override
			public int getDSTSavings() {
				return dstOffset.offset * 60000;
			}

			// TODO add TimeZone ID
//			@Override
//			public String getID() {
//				return super.getID();
//			}
		};

		calendar.setTimeZone(timeZone);

		onContinuousGlucoseMonitorSessionStartTimeReceived(device, calendar, crcPresent);
	}
 
Example #7
Source File: BloodPressureMeasurementDataCallback.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() < 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 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);
	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);
	}

	onBloodPressureMeasurementReceived(device, systolic, diastolic, meanArterialPressure, unit, pulseRate, userId, status, calendar);
}
 
Example #8
Source File: IntermediateCuffPressureDataCallback.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() < 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 #9
Source File: GlucoseMeasurementDataCallback.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() < 10) {
		onInvalidDataReceived(device, data);
		return;
	}

	int offset = 0;

	final int flags = data.getIntValue(Data.FORMAT_UINT8, offset++);
	final boolean timeOffsetPresent = (flags & 0x01) != 0;
	final boolean glucoseDataPresent = (flags & 0x02) != 0;
	final boolean unitMolL = (flags & 0x04) != 0;
	final boolean sensorStatusAnnunciationPresent = (flags & 0x08) != 0;
	final boolean contextInformationFollows = (flags & 0x10) != 0;

	if (data.size() < 10 + (timeOffsetPresent ? 2 : 0) + (glucoseDataPresent ? 3 : 0)
		+ (sensorStatusAnnunciationPresent ? 2 : 0)) {
		onInvalidDataReceived(device, data);
		return;
	}

	// Required fields
	final int sequenceNumber = data.getIntValue(Data.FORMAT_UINT16, offset);
	offset += 2;
	final Calendar baseTime = DateTimeDataCallback.readDateTime(data, 3);
	offset += 7;

	if (baseTime == null) {
		onInvalidDataReceived(device, data);
		return;
	}

	// Optional fields
	if (timeOffsetPresent) {
		final int timeOffset = data.getIntValue(Data.FORMAT_SINT16, offset);
		offset += 2;

		baseTime.add(Calendar.MINUTE, timeOffset);
	}

	Float glucoseConcentration = null;
	Integer unit = null;
	Integer type = null;
	Integer sampleLocation = null;
	if (glucoseDataPresent) {
		glucoseConcentration = data.getFloatValue(Data.FORMAT_SFLOAT, offset);
		final int typeAndSampleLocation = data.getIntValue(Data.FORMAT_UINT8, offset + 2);
		offset += 3;

		type = typeAndSampleLocation & 0x0F;
		sampleLocation = typeAndSampleLocation >> 4;
		unit = unitMolL ? UNIT_mol_L : UNIT_kg_L;
	}

	GlucoseStatus status = null;
	if (sensorStatusAnnunciationPresent) {
		final int value = data.getIntValue(Data.FORMAT_UINT16, offset);
		// offset += 2;

		status = new GlucoseStatus(value);
	}

	onGlucoseMeasurementReceived(device, sequenceNumber, baseTime /* with offset */,
			glucoseConcentration, unit, type, sampleLocation, status, contextInformationFollows);
}
 
Example #10
Source File: CGMSessionStartTimeDataCallback.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() != 9 && data.size() != 11) {
			onInvalidDataReceived(device, data);
			return;
		}

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

		final Calendar calendar = DateTimeDataCallback.readDateTime(data, 0);
		final Integer timeZoneOffset = TimeZoneDataCallback.readTimeZone(data, 7); // [minutes]
		final DSTOffsetCallback.DSTOffset dstOffset = DSTOffsetDataCallback.readDSTOffset(data, 8);

		if (calendar == null || timeZoneOffset == null || dstOffset == null) {
			onInvalidDataReceived(device, data);
			return;
		}

		final TimeZone timeZone = new TimeZone() {
			@Override
			public int getOffset(final int era, final int year, final int month, final int day, final int dayOfWeek, final int milliseconds) {
				return (timeZoneOffset + dstOffset.offset) * 60000; // convert minutes to milliseconds
			}

			@Override
			public void setRawOffset(final int offsetMillis) {
				throw new UnsupportedOperationException("Can't set raw offset for this TimeZone");
			}

			@Override
			public int getRawOffset() {
				return timeZoneOffset * 60000;
			}

			@Override
			public boolean useDaylightTime() {
				return true;
			}

			@Override
			public boolean inDaylightTime(final Date date) {
				// Use of DST is dependent on the input data only
				return dstOffset.offset > 0;
			}

			@Override
			public int getDSTSavings() {
				return dstOffset.offset * 60000;
			}

			// TODO add TimeZone ID
//			@Override
//			public String getID() {
//				return super.getID();
//			}
		};

		calendar.setTimeZone(timeZone);

		onContinuousGlucoseMonitorSessionStartTimeReceived(device, calendar, crcPresent);
	}
 
Example #11
Source File: DateTimeParser.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Parses the date and time info. This data has 7 bytes
 * 
 * @param data
 * @param offset
 *            offset to start reading the time
 * @return time in human readable format
 */
/* package */static String parse(final Data data, final int offset) {
	final Calendar calendar = DateTimeDataCallback.readDateTime(data, offset);
	return String.format(Locale.US, "%1$te %1$tb %1$tY, %1$tH:%1$tM:%1$tS", calendar);
}