Class Info |
|---|
public class SamplePoint implements Parcelable Sampling point class, which presents the sampled data of a specific type collected by a specific data collector at a given time or within a time range. It stores the values of each dimension of the data type at the given time (or within the time range) and the start time and end time of the sampling operation. The sampling modes of sampling points are classified into instantaneous sampling and continuous sampling based on the definition of data types. When setting the sampling point time, you are advised to call setSamplingTime() for instantaneous sampling and setTimeInterval() for continuous sampling (make sure to set a valid time duration). setSamplingTime() cannot be used when creating the continuous sampling point. Otherwise, the creation will fail. This API can be used to update the end time of a created continuous sampling point. After the API is called, only the end time will be updated. |
Qualifier and Type | Class Name and Description |
|---|---|
public static class | Builder for SamplePoint. |
Qualifier and Type | Method Name and Description |
|---|---|
void | addMetadata(String key, String value) Sets the metadata in SamplePoint for a key value pair. |
static Builder | builder(DataCollector dataCollector) Creates a SamplePoint object based on the data source. |
final void | Checks the interval validity. |
static SamplePoint | create(DataCollector dataCollector) Builds a sampling point. |
| Obtains the data collector of the sampling point. | |
| Obtains the data type of the sampling point. | |
long | getEndTime(TimeUnit timeUnit) Obtains the end timestamp of the sampling point. |
Value | getFieldValue(Field field) Obtains the attribute value. |
Value | getFieldValue(String fieldName) Obtains the attribute value. |
Map<String, Value> | Obtains the values of all attributes. |
long | getId() Obtains the ID of the sampling point. |
long | getInsertionTime(TimeUnit timeUnit) Obtains the insertion time. |
String | Obtains the metadata from SamplePoint. |
final DataCollector | Obtains the raw data collector of the sampling point. If it is not set, null will be returned. |
final DataCollector | Obtains the raw data collector of the sampling point. |
long | getSamplingTime(TimeUnit timeUnit) Obtains the timestamp of the sampling point. |
long | getStartTime(TimeUnit timeUnit) Obtains the start timestamp of the sampling point. |
boolean | isMerged() Checks whether data is merged. |
void | setId(long id) Sets the ID of the sampling point. |
void | setInsertionTime(long insertTime, TimeUnit timeUnit) Sets the data insertion time. |
void | setMetadata(String metadata) Sets the metadata in SamplePoint directly. |
final SamplePoint | setSamplingTime(long sampleTime, TimeUnit timeUnit) Sets the sampling timestamp of instantaneous data. |
final SamplePoint | setTimeInterval(long start, long end, TimeUnit timeUnit) Sets the sampling timestamp of data within an interval. |
Method |
|---|
public static Builder builder(DataCollector dataCollector) Creates a SamplePoint object based on the data source. |
Parameters
Name | Description |
|---|---|
dataCollector | Data collector. |
Returns
Type | Description |
|---|---|
Builder | Internal builder object for the data sampling point. |
Method |
|---|
public static SamplePoint create(DataCollector dataCollector) Builds a sampling point. |
Parameters
Name | Description |
|---|---|
dataCollector | Data collector. |
Returns
Type | Description |
|---|---|
SamplePoint | Sampling point. |
Method |
|---|
public final void checkIntervalValidity() Rules for verifying the interval validity:
|
Method |
|---|
public DataCollector getDataCollector() Obtains the data collector of the sampling point. |
Returns
Type | Description |
|---|---|
Data collector. |
Method |
|---|
public DataType getDataType() Obtains the data type of the sampling point. |
Returns
Type | Description |
|---|---|
Data type. |
Method |
|---|
public long getEndTime(TimeUnit timeUnit) Obtains the end timestamp of the sampling point. |
Parameters
Name | Description |
|---|---|
timeUnit | Time unit. |
Returns
Type | Description |
|---|---|
long | End time. |
Method |
|---|
public Value getFieldValue(Field field) Obtains the attribute value. |
Parameters
Name | Description |
|---|---|
field | Data attribute. |
Returns
Type | Description |
|---|---|
Value | Attribute value. |
Method |
|---|
public Value getFieldValue(String fieldName) Obtains the attribute value. |
Parameters
Name | Description |
|---|---|
fieldName | Data attribute name. |
Returns
Type | Description |
|---|---|
Value | Attribute value. |
Method |
|---|
public Map<String, Value> getFieldValues() Obtains the values of all attributes. |
Returns
Type | Description |
|---|---|
Map<String, Value> | Values of all attributes. |
Method |
|---|
public long getId() Obtains the ID of the sampling point. |
Returns
Type | Description |
|---|---|
long | ID of the sampling point. |
Method |
|---|
public long getInsertionTime(TimeUnit timeUnit) Obtains the insertion time. |
Parameters
Name | Description |
|---|---|
timeUnit | Time unit. |
Returns
Type | Description |
|---|---|
long | Insertion time. |
Method |
|---|
public final DataCollector getNullableRawCollector() Obtains the raw data collector of the sampling point. If it is not set, null will be returned. |
Returns
Type | Description |
|---|---|
Data collector. |
Method |
|---|
public final DataCollector getRawDataCollector() Obtains the raw data collector of the sampling point. |
Returns
Type | Description |
|---|---|
Data collector. |
Method |
|---|
public long getSamplingTime(TimeUnit timeUnit) Obtains the timestamp of the sampling point. |
Parameters
Name | Description |
|---|---|
timeUnit | Time unit. |
Returns
Type | Description |
|---|---|
long | Timestamp. |
Method |
|---|
public long getStartTime(TimeUnit timeUnit) Obtains the start timestamp of the sampling point. |
Parameters
Name | Description |
|---|---|
timeUnit | Time unit. |
Returns
Type | Description |
|---|---|
long | Start time. |
Method |
|---|
public boolean isMerged() Checks whether data is merged. |
Returns
Type | Description |
|---|---|
boolean | Whether data is merged. |
Method |
|---|
public void setId(long id) Sets the ID of the sampling point. |
Parameters
Name | Description |
|---|---|
ID of the sampling point. | ID of the sampling point. |
Method |
|---|
public void setInsertionTime(long insertTime, TimeUnit timeUnit) Sets the data insertion time. |
Parameters
Name | Description |
|---|---|
insertTime | Data insertion time. |
timeUnit | Time unit. |
Method |
|---|
public final SamplePoint setSamplingTime(long sampleTime, TimeUnit timeUnit) Sets the sampling timestamp of instantaneous data. |
Parameters
Name | Description |
|---|---|
sampleTime | Timestamp. |
timeUnit | Time unit. |
Returns
Type | Description |
|---|---|
SamplePoint | Sampling point. |
Method |
|---|
public final SamplePoint setTimeInterval(long start, long end, TimeUnit timeUnit) Sets the sampling timestamp of data within an interval. |
Parameters
Name | Description |
|---|---|
start | Start time. |
end | End time. |
timeUnit | Time unit. |
Returns
Type | Description |
|---|---|
SamplePoint | Sampling point. |
Method |
|---|
public String getMetadata() Obtains the metadata from SamplePoint. |
Returns
Type | Description |
|---|---|
String | Metadata. |
Method |
|---|
public void setMetadata(String metadata) Sets the metadata in SamplePoint. |
Parameters
Name | Description |
|---|---|
metadata | Metadata, which can be null, an empty string, or a string of 1 to 10000 characters. |
See Also:
Method |
|---|
public void addMetadata(String key, String value) Sets the metadata in SamplePoint for a key value pair. |
Parameters
Name | Description |
|---|---|
key | Key of the metadata. |
value | Value of the metadata. |
If the data type of the created SamplePoint is step count or blood glucose, and you want to display the data written to Health Service Kit on the home screen of the Huawei Health app, you need to set the metadata in SamplePoint based on the following key-value pairs.
The following example shows how to create a SampleSet for the step count data type, in order to call the API for adding fitness and health data.
// Step 1: Build a DataCollector object.
DataCollector dataCollector = new DataCollector.Builder().setPackageName(getApplicationContext())
.setDataType(DataType.DT_CONTINUOUS_STEPS_DELTA)
.setDataStreamName("STEPS_DELTA")
.setDataGenerateType(DataCollector.DATA_TYPE_RAW)
.build();
// Step 2: Create a sampling dataset based on the data collector.
final SampleSet sampleSet = SampleSet.create(dataCollector);
// Step 3: Build the start time, end time, and incremental step count for a DT_CONTINUOUS_STEPS_DELTA sampling point.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = null;
Date endDate = null;
try {
// Enter the start time and end time. The standard UNIX timestamp is used for storage, without considering the time zone differences.
// Note that the start time and end time of the sampling point data of the inserted fitness and health data must be later than the UNIX timestamp corresponding to January 1, 2014.
startDate = dateFormat.parse("2020-03-17 09:00:00");
endDate = dateFormat.parse("2020-03-17 09:05:00");
} catch (ParseException e) {
logger("Time parsing error");
}
int stepsDelta = 200;
// Step 4: Build a DT_CONTINUOUS_STEPS_DELTA sampling point.
SamplePoint samplePoint = sampleSet.createSamplePoint()
.setTimeInterval(startDate.getTime(), endDate.getTime(), TimeUnit.MILLISECONDS);
samplePoint.getFieldValue(Field.FIELD_STEPS_DELTA).setIntValue(stepsDelta);
// To display the written step count data in the Huawei Health app, add the following metadata to the sampling points:
samplePoint.addMetadata("motion_type", "RUN");
// Step 5: Save a DT_CONTINUOUS_STEPS_DELTA sampling point to the sampling dataset. You can repeat steps 3 to 5 to add more sampling points to the sampling dataset.
sampleSet.addSample(samplePoint);