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

The following examples show how to use no.nordicsemi.android.ble.data.Data#size() . 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: BatteryLevelDataCallback.java    From Android-BLE-Common-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@SuppressWarnings("ConstantConditions")
@Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
	super.onDataReceived(device, data);

	if (data.size() == 1) {
		final int batteryLevel = data.getIntValue(Data.FORMAT_UINT8, 0);
		if (batteryLevel >= 0 && batteryLevel <= 100) {
			onBatteryLevelChanged(device, batteryLevel);
			return;
		}
	}
	onInvalidDataReceived(device, data);
}
 
Example 2
Source File: RunningSpeedAndCadenceFeatureDataCallback.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() != 2) {
		onInvalidDataReceived(device, data);
		return;
	}

	final int value = data.getIntValue(Data.FORMAT_UINT16, 0);
	final RSCFeatures features = new RSCFeatures(value);
	onRunningSpeedAndCadenceFeaturesReceived(device, features);
}
 
Example 3
Source File: CGMFeatureDataCallback.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() != 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 4
Source File: DateTimeDataCallback.java    From Android-BLE-Common-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: 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 6
Source File: AlertLevelDataCallback.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) {
		final Integer level = data.getIntValue(Data.FORMAT_UINT8, 0);
		if (level != null && level <= AlertLevelCallback.ALERT_HIGH) {
			onAlertLevelChanged(device, level);
			return;
		}
	}
	onInvalidDataReceived(device, data);
}
 
Example 7
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 8
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 9
Source File: CyclingSpeedAndCadenceFeatureDataCallback.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 CSCFeatures features = new CSCFeatures(value);
	onCyclingSpeedAndCadenceFeaturesReceived(device, features);
}
 
Example 10
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 11
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 12
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 13
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 14
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 15
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 16
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 17
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 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: RunningSpeedAndCadenceMeasurementDataCallback.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() < 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 20
Source File: TimeZoneDataCallback.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Offset from UTC in number of 15 minutes increments. A value of -128 means that the time zone
 * offset is not known. The offset defined in this characteristic is constant, regardless
 * whether daylight savings is in effect.
 *
 * @param data   data received.
 * @param offset offset from which the time zone is to be read.
 * @return the time offset in 15 minutes increments, or null if offset is outside ot range.
 */
@Nullable
public static Integer readTimeZone(@NonNull final Data data, final int offset) {
	if (data.size() < offset + 1)
		return null;

	return data.getIntValue(Data.FORMAT_SINT8, offset);
}