no.nordicsemi.android.ble.data.Data Java Examples

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

	final Integer offset = readTimeZone(data, 0);
	if (offset == null) {
		onInvalidDataReceived(device, data);
		return;
	}

	if (offset == -128) {
		onUnknownTimeZoneReceived(device);
	} else if (offset < -48 || offset > 56) {
		onInvalidDataReceived(device, data);
	} else {
		onTimeZoneReceived(device, offset * 15);
	}
}
 
Example #2
Source File: TemplateManager.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
protected void onDeviceReady() {
	super.onDeviceReady();

	// Initialization is now ready.
	// The service or activity has been notified with TemplateManagerCallbacks#onDeviceReady().
	// TODO Do some extra logic here, of remove onDeviceReady().

	// Device is ready, let's read something here. Usually there is nothing else to be done
	// here, as all had been done during initialization.
	readCharacteristic(optionalCharacteristic)
			.with((device, data) -> {
				// Characteristic value has been read
				// Let's do some magic with it.
				if (data.size() > 0) {
					final Integer value = data.getIntValue(Data.FORMAT_UINT8, 0);
					log(LogContract.Log.Level.APPLICATION, "Value '" + value + "' has been read!");
				} else {
					log(Log.WARN, "Value is empty!");
				}
			})
			.enqueue();
}
 
Example #3
Source File: SensorLocationDataCallbackTest.java    From Android-BLE-Common-Library with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void onSensorLocationReceived() {
	final ProfileReadResponse callback = new SensorLocationDataCallback() {
		@Override
		public void onSensorLocationReceived(@NonNull final BluetoothDevice device, final int location) {
			called = true;
			assertEquals("Location", SensorLocationCallback.SENSOR_LOCATION_REAR_WHEEL, location);
		}
	};

	called = false;
	final Data data = new Data(new byte[] { 12 });
	callback.onDataReceived(null, data);
	assertTrue(called);
	assertTrue(callback.isValid());
}
 
Example #4
Source File: GlucoseMeasurementContextDataCallbackTest.java    From Android-BLE-Common-Library with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void onGlucoseMeasurementContextReceived_full() {
	final MutableData data = new MutableData(new byte[17]);
	data.setValue(0xFF, Data.FORMAT_UINT8, 0); // Flags
	data.setValue(0, Data.FORMAT_UINT16, 1); // Sequence number
	data.setValue(0xb3, Data.FORMAT_UINT8, 3); // Extended flags - ignored
	data.setValue(GlucoseMeasurementContextCallback.Carbohydrate.DINNER.value, Data.FORMAT_UINT8, 4); // Carbohydrate
	data.setValue(100.0f, Data.FORMAT_SFLOAT, 5); // Carbohydrate Amount
	data.setValue(GlucoseMeasurementContextCallback.Meal.CASUAL.value, Data.FORMAT_UINT8, 7); // Meal
	data.setValue(0x12, Data.FORMAT_UINT8, 8); // Tester and Health (health care practitioner, minor issues)
	data.setValue(60, Data.FORMAT_UINT16, 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
	data.setValue(34.5f, Data.FORMAT_SFLOAT, 15); // HbA1c = 34.5%
	callback.onDataReceived(null, data);
	assertTrue(success);
	assertEquals(0, number);
}
 
Example #5
Source File: RecordAccessControlPointData.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static Data create(final byte opCode, final byte operator,
						   @NonNull final FilterType filter,
						   @Data.IntFormat final int formatType,
						   final int... parameters) {
	final int parameterLen = formatType & 0x0F;

	final MutableData data = new MutableData(new byte[2 + 1 + parameters.length * parameterLen]);
	data.setByte(opCode, 0);
	data.setByte(operator, 1);
	if (parameters.length > 0) {
		data.setByte(filter.type, 2);
		data.setValue(parameters[0], formatType, 3);
	}
	if (parameters.length == 2) {
		data.setValue(parameters[1], formatType, 3 + parameterLen);
	}
	return data;
}
 
Example #6
Source File: DSTOffsetDataCallbackTest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void onDSTOffsetReceived_daylight() {
	final Data data = new Data(new byte[] { 4 });
	callback.onDataReceived(null, data);
	assertTrue(success);
	assertSame(DSTOffsetCallback.DSTOffset.DAYLIGHT_TIME, result);
}
 
Example #7
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 #8
Source File: RecordAccessControlPointDataCallbackTest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void onRecordAccessOperationError_invalidOperator() {
	final Data data = new Data(new byte[] { 6, 0, 1, 3 });
	callback.onDataReceived(null, data);
	assertEquals(error, 3);
	assertEquals(1, requestCode);
}
 
Example #9
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 #10
Source File: SpeedAndCadenceControlPointData.java    From Android-BLE-Common-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Update to the location of the sensor with the value sent as parameter to this op code.
 *
 * @param location new location id (UINT8). See SENSOR_LOCATION_* constants.
 * @return Data object.
 */
public static Data updateSensorLocation(@SensorLocation final int location) {
	final MutableData data =  new MutableData(new byte[2]);
	data.setByte(SC_OP_CODE_UPDATE_SENSOR_LOCATION, 0);
	data.setValue(location, Data.FORMAT_UINT8, 1);
	return data;
}
 
Example #11
Source File: GlucoseFeatureDataCallbackTest.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) {
	success = false;
	invalidData = false;
	result = null;
	super.onDataReceived(device, data);
}
 
Example #12
Source File: DSTOffsetDataCallbackTest.java    From Android-BLE-Common-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void onDSTOffsetReceived_standard() {
	final Data data = new Data(new byte[] { 0 });
	callback.onDataReceived(null, data);
	assertTrue(success);
	assertSame(DSTOffsetCallback.DSTOffset.STANDARD_TIME, result);
}
 
Example #13
Source File: CGMSpecificOpsControlPointDataCallbackTest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void onCGMSpecificOpsOperationError_withCrc() {
	final Data data = new Data(new byte[] { 28, 2, 2, (byte) 0xA7, (byte) 0x09});
	callback.onDataReceived(null, data);
	assertEquals(error, 2);
	assertTrue(secured);
	assertEquals(2, requestCode);
}
 
Example #14
Source File: CGMSessionRunTimeResponse.java    From Android-BLE-Common-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onContinuousGlucoseMonitorSessionRunTimeReceivedWithCrcError(@NonNull final BluetoothDevice device,
																		 @NonNull final Data data) {
	onInvalidDataReceived(device, data);
	this.secured = true;
	this.crcValid = false;
}
 
Example #15
Source File: DSTOffsetDataCallbackTest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void onDSTOffsetReceived_unknown() {
	final Data data = new Data(new byte[] {(byte) 255});
	callback.onDataReceived(null, data);
	assertTrue(success);
	assertSame(DSTOffsetCallback.DSTOffset.UNKNOWN, result);
}
 
Example #16
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 #17
Source File: SensorLocationDataCallbackTest.java    From Android-BLE-Common-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void onInvalidDataReceived() {
	final ProfileReadResponse callback = new SensorLocationDataCallback() {
		@Override
		public void onSensorLocationReceived(@NonNull final BluetoothDevice device, final int location) {
			called = true;
		}
	};

	called = false;
	final Data data = new Data(new byte[] { 0x01, 0x02 });
	callback.onDataReceived(null, data);
	assertFalse(called);
	assertFalse(callback.isValid());
}
 
Example #18
Source File: CGMSpecificOpsControlPointDataCallbackTest.java    From Android-BLE-Common-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void onCGMSpecificOpsOperationCompleted_withCrc() {
	final Data data = new Data(new byte[] { 28, 2, 1, (byte) 0x3C, (byte) 0x3B});
	callback.onDataReceived(null, data);
	assertTrue(success);
	assertTrue(secured);
	assertEquals(2, requestCode);
}
 
Example #19
Source File: CGMFeatureResponse.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onContinuousGlucoseMonitorFeaturesReceivedWithCrcError(@NonNull final BluetoothDevice device,
																   @NonNull final Data data) {
	onInvalidDataReceived(device, data);
	this.secured = true;
	this.crcValid = false;
}
 
Example #20
Source File: CGMSpecificOpsControlPointDataCallbackTest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void onContinuousGlucoseCommunicationIntervalReceived_notSecured() {
	final Data data = new Data(new byte[] { 3, 11 });
	callback.onDataReceived(null, data);
	assertEquals("Interval", 11, interval);
	assertFalse(secured);
}
 
Example #21
Source File: HeartRateMeasurementDataCallbackTest.java    From Android-BLE-Common-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void onHeartRateMeasurementReceived_simple() {
	success = false;
	final Data data = new Data(new byte[] { 0, 85 });
	response.onDataReceived(null, data);
	assertTrue(response.isValid());
	assertTrue(success);
	assertEquals(85, heartRate);
	assertNull(contactDetected);
	assertNull(energyExpanded);
	assertNull(rrIntervals);
}
 
Example #22
Source File: TemperatureMeasurementDataCallbackTest.java    From Android-BLE-Common-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void onTemperatureMeasurementReceived() {
	final ProfileReadResponse response = new TemperatureMeasurementDataCallback() {
		@Override
		public void onTemperatureMeasurementReceived(@NonNull final BluetoothDevice device,
													 final float temperature, final int unit,
													 @Nullable final Calendar calendar,
													 @Nullable final Integer type) {
			called = true;
			assertEquals("Temperature", 37.60f, temperature, 0.001f);
			assertEquals("Unit", TemperatureMeasurementCallback.UNIT_C, unit);
			assertNotNull("Calendar present", calendar);
			assertTrue("Year set", calendar.isSet(Calendar.YEAR));
			assertEquals("Year", calendar.get(Calendar.YEAR), 2012);
			assertTrue("Month set", calendar.isSet(Calendar.MONTH));
			assertEquals("Month", calendar.get(Calendar.MONTH), Calendar.DECEMBER);
			assertTrue("Day set", calendar.isSet(Calendar.DATE));
			assertEquals("Day", 5, calendar.get(Calendar.DATE));
			assertEquals("Hour", 11, calendar.get(Calendar.HOUR_OF_DAY));
			assertEquals("Minute", 50, calendar.get(Calendar.MINUTE));
			assertEquals("Seconds", 27, calendar.get(Calendar.SECOND));
			assertEquals("Milliseconds", 0, calendar.get(Calendar.MILLISECOND));
			assertNotNull("Type present", type);
			assertEquals(TemperatureMeasurementCallback.TYPE_FINGER, type.intValue());
		}
	};

	final Data data = new Data(new byte[] { 0x06, (byte) 0xB0, 0x0E, 0x00, (byte) 0xFE, (byte) 0xDC, 0x07, 0x0C, 0x05, 0x0B, 0x32, 0x1B, 0x04 });
	called = false;
	response.onDataReceived(null, data);
	assertTrue(called);
	assertTrue(response.isValid());
}
 
Example #23
Source File: TemperatureMeasurementDataCallbackTest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void onTemperatureMeasurementReceived() {
	final ProfileReadResponse response = new TemperatureMeasurementDataCallback() {
		@Override
		public void onTemperatureMeasurementReceived(@NonNull final BluetoothDevice device,
													 final float temperature, final int unit,
													 @Nullable final Calendar calendar,
													 @Nullable final Integer type) {
			called = true;
			assertEquals("Temperature", 37.60f, temperature, 0.001f);
			assertEquals("Unit", TemperatureMeasurementCallback.UNIT_C, unit);
			assertNotNull("Calendar present", calendar);
			assertTrue("Year set", calendar.isSet(Calendar.YEAR));
			assertEquals("Year", calendar.get(Calendar.YEAR), 2012);
			assertTrue("Month set", calendar.isSet(Calendar.MONTH));
			assertEquals("Month", calendar.get(Calendar.MONTH), Calendar.DECEMBER);
			assertTrue("Day set", calendar.isSet(Calendar.DATE));
			assertEquals("Day", 5, calendar.get(Calendar.DATE));
			assertEquals("Hour", 11, calendar.get(Calendar.HOUR_OF_DAY));
			assertEquals("Minute", 50, calendar.get(Calendar.MINUTE));
			assertEquals("Seconds", 27, calendar.get(Calendar.SECOND));
			assertEquals("Milliseconds", 0, calendar.get(Calendar.MILLISECOND));
			assertNotNull("Type present", type);
			assertEquals(TemperatureMeasurementCallback.TYPE_FINGER, type.intValue());
		}
	};

	final Data data = new Data(new byte[] { 0x06, (byte) 0xB0, 0x0E, 0x00, (byte) 0xFE, (byte) 0xDC, 0x07, 0x0C, 0x05, 0x0B, 0x32, 0x1B, 0x04 });
	called = false;
	response.onDataReceived(null, data);
	assertTrue(called);
	assertTrue(response.isValid());
}
 
Example #24
Source File: CGMSpecificOpsControlPointDataCallbackTest.java    From Android-BLE-Common-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void onContinuousGlucosePatientHighAlertReceived() {
	final Data data = new Data(new byte[] { 9, 12, -16});
	callback.onDataReceived(null, data);
	assertEquals("Level", 1.2f, patientHighAlertLevel, 0.01);
	assertFalse(secured);
}
 
Example #25
Source File: HeartRateMeasurementDataCallbackTest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void onHeartRateMeasurementReceived_simple() {
	success = false;
	final Data data = new Data(new byte[] { 0, 85 });
	response.onDataReceived(null, data);
	assertTrue(response.isValid());
	assertTrue(success);
	assertEquals(85, heartRate);
	assertNull(contactDetected);
	assertNull(energyExpanded);
	assertNull(rrIntervals);
}
 
Example #26
Source File: CGMSpecificOpsControlPointDataCallbackTest.java    From Android-BLE-Common-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void onCGMSpecificOpsOperationCompleted() {
	final Data data = new Data(new byte[] { 28, 2, 1});
	callback.onDataReceived(null, data);
	assertTrue(success);
	assertFalse(secured);
	assertEquals(2, requestCode);
}
 
Example #27
Source File: TemplateDataCallback.java    From Android-nRF-Toolbox 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) {
	if (data.size() < 2) {
		onInvalidDataReceived(device, data);
		return;
	}

	// Read flags
	int offset = 0;
	final int flags = data.getIntValue(Data.FORMAT_UINT8, offset);
	final int hearRateType = (flags & 0x01) == 0 ? Data.FORMAT_UINT8 : Data.FORMAT_UINT16;
	offset += 1;

	// Validate packet length. The type's lower nibble is its length.
	if (data.size() < 1 + (hearRateType & 0x0F)) {
		onInvalidDataReceived(device, data);
		return;
	}

	final int value = data.getIntValue(hearRateType, offset);
	// offset += hearRateType & 0xF;

	// ...

	// Report the parsed value(s)
	onSampleValueReceived(device, value);
}
 
Example #28
Source File: BodySensorLocationDataCallbackTest.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void onInvalidDataReceived() {
	success = false;
	final Data data = new Data();
	response.onDataReceived(null, data);
	assertFalse(response.isValid());
	assertFalse(success);
}
 
Example #29
Source File: HeartRateMeasurementDataCallbackTest.java    From Android-BLE-Common-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void onHeartRateMeasurementReceived_noContact() {
	success = false;
	final Data data = new Data(new byte[] {0x4, (byte) 0xFF});
	response.onDataReceived(null, data);
	assertTrue(response.isValid());
	assertTrue(success);
	assertEquals(255, heartRate);
	assertNotNull(contactDetected);
	assertFalse(contactDetected);
	assertNull(energyExpanded);
	assertNull(rrIntervals);
}
 
Example #30
Source File: GlucoseMeasurementContextDataCallbackTest.java    From Android-BLE-Common-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void onGlucoseMeasurementContextReceived_empty() {
	final MutableData data = new MutableData(new byte[17]);
	data.setValue(0x00, Data.FORMAT_UINT8, 0); // Flags
	data.setValue(1, Data.FORMAT_UINT16, 1);
	callback.onDataReceived(null, data);
	assertTrue(success);
	assertEquals(1, number);
}