目录

DataController

Class Info

public class DataController

Defines the API for data management. You can use this API to insert, delete, update, and read data, as well as query the data statistics of the current day and past days.

You can obtain DataController via the HuaweiHiHealth class.

Sample code for obtaining a DataController object:
Java
// Obtain a dataController object.
// Note that this refers to an Activity object.
DataController dataController = HuaweiHiHealth.getDataController(this);
Kotlin
// Obtain a dataController object.
// Note that this refers to an Activity object.
val dataController = HuaweiHiHealth.getDataController(this)

Public Method Summary

Qualifier and Type

Method Name and Description

Task<Void>

clearAll()

Clears all data inserted by the app from the Health Service Kit Android side and REST side.

Task<Void>

delete(DeleteOptions options)

Deletes inserted sampling datasets by specifying a time range or deletes them all. It can also be used to delete exercise records.

Task<Void>

insert(SampleSet sampleSet)

Inserts a sampling dataset to Health Service Kit.

Task<ReadReply>

read(ReadOptions readOptions)

Reads user data. You can read data by time, device, data collector, and more by specifying the related parameters in ReadOptions.

Task<SampleSet>

readDailySummation(DataType dataType, int startTime, int endTime)

Reads the daily statistics of a specified data type. You can set the data type, start time, and end time to read the daily statistics in the specified period. If the related data type does not support polymerization statistics, an exception will be thrown.

Task<List<SampleSet>>

readDailySummation(List<DataType> dataTypes, int startTime, int endTime)

Reads the daily statistics of multiple data types. You can set the data type list, start time, and end time to read the daily statistics in the specified period. If any data type does not support polymerization statistics, an exception will be thrown.

Task<Map<DataType, SamplePoint>>

readLatestData(List<DataType> dataTypes)

Reads the latest data points of a specified data type. If the specified data type is not supported, an exception will be thrown.

Task<SampleSet>

readTodaySummation(DataType dataType)

Reads the statistics of a specified data type of the current day. If the specified data type does not support polymerization statistics, an exception will be thrown.

Task<List<SampleSet>>

readTodaySummation(List<DataType> dataTypes)

Reads the statistics of multiple data types of the current day. If any data type does not support polymerization statistics, an exception will be thrown.

Task<Void>

update(UpdateOptions options)

Updates existing data. If the target to be updated does not exist, a new entry of data is inserted.

Public Methods

[h2]clearAll()

Method

public Task<Void> clearAll()

Clears all data inserted by the app from the Health Service Kit Android side and REST side.

Returns

Type

Description

Task<Void>

Task object, which is used to check whether a request is successful, fails, or is complete.

The sample code is as follows:
Java
// 1. Call clearAll to delete all the data inserted by the app from the Health Service Kit Android side and REST side.
Task<Void> clearTask = dataController.clearAll(); 
 
// 2. Calling clearAll to clear user data is an asynchronous operation. Therefore, a listener needs to be registered to monitor whether the clearance is successful or not.
clearTask.addOnSuccessListener(new OnSuccessListener<Void>() { 
    @Override 
    public void onSuccess(Void result) { 
        logger("clearAll success"); 
    } 
}).addOnFailureListener(new OnFailureListener() { 
    @Override 
    public void onFailure(Exception e) { 
        String errorCode = e.getMessage(); 
        Pattern pattern = Pattern.compile("[0-9]*"); 
        Matcher isNum = pattern.matcher(errorCode); 
        if (isNum.matches()) { 
            String errorMsg = HiHealthStatusCodes.getStatusCodeMessage(Integer.parseInt(errorCode)); 
            logger("clearAll failure " + errorCode + ":" + errorMsg); 
        } else { 
            logger("clearAll failure " + errorCode); 
        } 
    } 
});
Kotlin
// 1. Call clearAll to delete all the data inserted by the app from the Health Service Kit Android side and REST side.
val clearTask = dataController!!.clearAll()

// 2. Calling clearAll to clear user data is an asynchronous operation. Therefore, a listener needs to be registered to monitor whether the clearance is successful or not.
clearTask!!.addOnSuccessListener {
    logger("clearAll success")
    logger(SPLIT)
}.addOnFailureListener { e -> logger(e.toString()) }

If you directly copy and use the preceding sample code, the following two errors will be reported. The related solutions are provided as follows:

  1. dataController is not defined.

    Solution: See dataController is not defined.

  2. The helper function logger is not defined.

    Solution: See The helper function logger is not defined.

[h2]delete(DeleteOptions options)

Method

public Task<Void> delete(DeleteOptions options)

Deletes inserted sampling datasets by specifying a time range or deletes them all. It can also be used to delete exercise records.

Parameters

Name

Description

options

DeleteOptions type, used to specify the data collector, data type, and time range of data to be deleted.

Returns

Type

Description

Task<Void>

Task object, which is used to check whether a request is successful, fails, or is complete.

Sample code for deleting inserted sampling datasets:
Java
// Build the condition for data deletion: 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(); 
 
// Build the time range for the deletion: start time and end time.
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. Data points within the specified timestamp range will be deleted.
    startDate = dateFormat.parse("2020-03-17 09:00:00");
    endDate = dateFormat.parse("2020-03-17 09:05:00");
} catch (ParseException exception) {
    logger("Time parsing error");
}
 
// Build a parameter object as the conditions for the deletion.
DeleteOptions deleteOptions = new DeleteOptions.Builder().addDataCollector(dataCollector) 
    .setTimeInterval(startDate.getTime(), endDate.getTime(), TimeUnit.MILLISECONDS) 
    .build(); 
 
// Use the specified deletion object to call the delete API to delete the sampling dataset.
Task<Void> deleteTask = dataController.delete(deleteOptions); 
 
// Calling the delete API to delete the sampling dataset is an asynchronous operation. Therefore, a listener needs to be registered to monitor whether the data deletion is successful or not.
deleteTask.addOnSuccessListener(new OnSuccessListener<Void>() { 
    @Override 
    public void onSuccess(Void result) { 
        logger("Sample data is successfully deleted from the HMS core."); 
    } 
}).addOnFailureListener(new OnFailureListener() { 
    @Override 
    public void onFailure(Exception e) { 
        Log.i("DataController","Failed to delete sample data from HMS core" + e.getMessage());
    } 
});
Kotlin
// Build the condition for data deletion: a DataCollector object.
val dataCollector = DataCollector.Builder().setPackageName(applicationContext)
    .setDataType(DataType.DT_CONTINUOUS_STEPS_DELTA)
    .setDataStreamName("STEPS_DELTA")
    .setDataGenerateType(DataCollector.DATA_TYPE_RAW)
    .build()

// Build the time range for the deletion: start time and end time.
val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE)
var startDate: Date? = null
var endDate: Date? = null
try {
    startDate = dateFormat.parse("2020-08-27 09:00:00")
    endDate = dateFormat.parse("2020-08-27 09:05:00")
} catch (e: ParseException) {
    logger("Time parsing error")
}

// Build a parameter object as the conditions for the deletion.
val deleteOptions = DeleteOptions.Builder().addDataCollector(dataCollector)
    .setTimeInterval(startDate.time, endDate.time, TimeUnit.MILLISECONDS)
    .build()

// Use the specified deletion object to call the delete API to delete the sampling dataset.
val deleteTask = dataController!!.delete(deleteOptions)

// Calling the delete API to delete the sampling dataset is an asynchronous operation. Therefore, a listener needs to be registered to monitor whether the data deletion is successful or not.
deleteTask!!.addOnSuccessListener {
    logger("Success delete sample data from HMS core")
}!!.addOnFailureListener { e -> logger(e.toString()) }

If you directly copy and use the preceding sample code, the following two errors will be reported. The related solutions are provided as follows:

  1. dataController is not defined.

    Solution: See dataController is not defined.

  2. The helper function logger is not defined.

    Solution: See The helper function logger is not defined.

[h2]insert(SampleSet sampleSet)

Method

public Task<Void> insert(SampleSet sampleSet)

Inserts a sampling dataset to Health Service Kit.

Parameters

Name

Description

sampleSet

Sampling dataset to be inserted to Health Service Kit.

Returns

Type

Description

Task<Void>

Task object, which is used to check whether a request is successful, fails, or is complete.

Sample code for inserting a sampling dataset:
Java
// 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(); 
 
// 2. Create a sampling dataset based on the data collector.
final SampleSet sampleSet = SampleSet.create(dataCollector); 
 
// 3. Build the start time and end time 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 end time of the inserted health and fitness sampling point 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 exception) {
    logger("Time parsing error");
}
int stepsDelta = 200; 
 
// 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); 
// If the written step count data needs to be displayed on the home screen of the Huawei Health app, use addMetadata to add the following metadata to the sampling points:
samplePoint.addMetadata("motion_type", "RUN");
 
// 5. Add a step count increment sampling point to the sampling dataset. Repeat steps 3 through 5 to add more sampling points to the sampling dataset, if needed.
sampleSet.addSample(samplePoint); 
 
// 6. Call the insert API to insert the sampling dataset into Health Service Kit.
Task<Void> insertTask = dataController.insert(sampleSet); 
 
// 7. Calling the insert API to insert the sampling dataset is an asynchronous operation. Therefore, a listener needs to be registered to monitor whether the data insertion is successful or not.
insertTask.addOnSuccessListener(new OnSuccessListener<Void>() { 
    @Override 
    public void onSuccess(Void result) { 
        logger("Successfully insert a SampleSet into HMS core"); 
        showSampleSet(sampleSet); 
    } 
}).addOnFailureListener(new OnFailureListener() { 
    @Override 
    public void onFailure(Exception e) { 
        Log.i("DataController","Failed to insert a SampleSet into HMS core" + e.getMessage());
    } 
});
Kotlin
// 1. Build a DataCollector object.
val dataCollector = DataCollector.Builder().setPackageName(applicationContext)
    .setDataType(DataType.DT_CONTINUOUS_STEPS_DELTA)
    .setDataStreamName("STEPS_DELTA")
    .setDataGenerateType(DataCollector.DATA_TYPE_RAW)
    .build()

// 2. Create a sampling dataset based on the data collector.
val sampleSet = SampleSet.create(dataCollector)

// 3. Build the start time and end time for a DT_CONTINUOUS_STEPS_DELTA sampling point.
val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE)
var startDate: Date? = null
var endDate: Date? = null
try {
    startDate = dateFormat.parse("2020-08-27 09:00:00")
    endDate = dateFormat.parse("2020-08-27 09:05:00")
} catch (e: ParseException) {
    logger("Time parsing error")
}
val stepsDelta = 1000

// 4. Build a DT_CONTINUOUS_STEPS_DELTA sampling point.
val samplePoint = sampleSet.createSamplePoint()
    .setTimeInterval(startDate!!.time, endDate!!.time, TimeUnit.MILLISECONDS)
samplePoint.getFieldValue(Field.FIELD_STEPS_DELTA).setIntValue(stepsDelta)

// 5. Add a DT_CONTINUOUS_STEPS_DELTA sampling point to the sampling dataset. Repeat steps 3 through 5 to add more sampling points to the sampling dataset, if needed.
sampleSet.addSample(samplePoint)

// 6. Call the insert API to insert the sampling dataset into Health Service Kit.
val insertTask = dataController!!.insert(sampleSet)

// 7. Calling the insert API to insert the sampling dataset is an asynchronous operation. Therefore, a listener needs to be registered to monitor whether the data insertion is successful or not.
insertTask!!.addOnSuccessListener {
    logger("Successfully insert a SampleSet into HMS core")
    showSampleSet(sampleSet)
}!!.addOnFailureListener { e -> logger(e.toString()) }

If you directly copy and use the preceding sample code, the following three errors will be reported. The related solutions are provided as follows:

  1. dataController is not defined.

    Solution: dataController is an object of the DataController class. You can obtain the DataController via the HuaweiHiHealth class.

    Java
    // Note that this refers to an Activity object.
    DataController dataController = HuaweiHiHealth.getDataController(this);
    Kotlin
    val dataController = HuaweiHiHealth.getDataController(this)
  2. The helper function showSampleSet is not defined.

    Solution: Define an empty showSampleSet function and do not perform any operation. Refer to the following sample code:

    Java
    /** 
      * Print the SamplePoint in the SampleSet object as an output.
      * 
      * @param sampleSet Sampling dataset.
      */ 
    private void showSampleSet(SampleSet sampleSet) { 
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    
        for (SamplePoint samplePoint : sampleSet.getSamplePoints()) { 
            logger("Sample point type: " + samplePoint.getDataType().getName()); 
            logger("Start: " + dateFormat.format(new Date(samplePoint.getStartTime(TimeUnit.MILLISECONDS)))); 
            logger("End: " + dateFormat.format(new Date(samplePoint.getEndTime(TimeUnit.MILLISECONDS)))); 
            for (Field field : samplePoint.getDataType().getFields()) { 
                logger("Field: " + field.getName() + " Value: " + samplePoint.getFieldValue(field)); 
            } 
        } 
    }
    Kotlin
    /**
     * Print the SamplePoint in the SampleSet object as an output.
     *
      * @param sampleSet Sampling dataset.
     */
    private fun showSampleSet(sampleSet: SampleSet) {
        val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE)
    
        for (samplePoint in sampleSet.samplePoints) {
            logger("Sample point type: " + samplePoint.dataType.name)
            logger("Start: " + dateFormat.format(Date(samplePoint.getStartTime(TimeUnit.MILLISECONDS))))
            logger("End: " + dateFormat.format(Date(samplePoint.getEndTime(TimeUnit.MILLISECONDS))))
            for (field in samplePoint.dataType.fields) {
                logger("Field: " + field.name + " Value: " + samplePoint.getFieldValue(field))
            }
        }
    }
  3. The helper function logger is not defined.

    Solution: Define an empty logger function and do not perform any operation. Refer to the following sample code:

    Java
    private static final String TAG = "DataController";
    
    /** 
      * Also send operation result logs to the logcat.
     * 
      * @param string Log string.
     */ 
    private void logger(String string) { 
        Log.i(TAG, string); 
    }
    Kotlin
    private val TAG = "DataController"
    
    /**
     * Also send operation result logs to the logcat.
     *
     * @param string Log string.
     */
    private fun logger(string: String) {
        Log.i(TAG, string)
    }

[h2]read(ReadOptions readOptions)

Method

public Task<ReadReply> read(ReadOptions readOptions)

Reads user data. You can read data by time, device, data collector, and more by specifying the related parameters in ReadOptions.

Parameters

Name

Description

readOptions

ReadOptions class, for specifying parameters in the data reading request. It is used to specify the data collector, data type, the time range for the requested data, and more. For details, see the definition of ReadOptions.

Returns

Type

Description

Task<ReadReply>

Read data. The start time or end time of the returned data points is within the specified time range.

Sample code for reading user data:
Java
// 1. Build the time range for the query: start time and end time.
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. Data points within the specified timestamp range will be queried.
    startDate = dateFormat.parse("2020-03-17 09:00:00");
    endDate = dateFormat.parse("2020-03-17 09:05:00");
} catch (ParseException exception) {
    logger("Time parsing error");
}
 
// 2. Build the condition-based query object.
// The following takes reading detailed step count (DT_CONTINUOUS_STEPS_DELTA) within a specific time range as an example.
// You can add other types of latest data you want to read.
ReadOptions readOptions = new ReadOptions.Builder().read(DataType.DT_CONTINUOUS_STEPS_DELTA) 
    .setTimeRange(startDate.getTime(), endDate.getTime(), TimeUnit.MILLISECONDS) 
    .build(); 
 
// 3. Use the specified condition query object to call the read API to query the sampling dataset.
Task<ReadReply> readReplyTask = dataController.read(readOptions); 
 
// 4. Calling the read API to query the sampling dataset is an asynchronous operation. Therefore, a listener needs to be registered to monitor whether the data query is successful or not.
readReplyTask.addOnSuccessListener(new OnSuccessListener<ReadReply>() { 
    @Override 
    public void onSuccess(ReadReply readReply) { 
        logger("Successfully read a SampleSet from HMS core"); 
        for (SampleSet sampleSet : readReply.getSampleSets()) { 
            showSampleSet(sampleSet); 
        } 
    } 
}).addOnFailureListener(new OnFailureListener() { 
    @Override 
    public void onFailure(Exception e) { 
        Log.i("DataController","Failed to read a SampleSet from HMS core" + e.getMessage());
    } 
});
Kotlin
// 1. Build the time range for the query: start time and end time.
val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE)
var startDate: Date? = null
var endDate: Date? = null
try {
    // Enter the start time and end time. The standard UNIX timestamp is used for storage, without considering the time zone differences. Data points within the specified timestamp range will be queried.
    startDate = dateFormat.parse("2020-08-27 09:00:00")
    endDate = dateFormat.parse("2020-08-27 09:05:00")
} catch (e: ParseException) {
    logger("Time parsing error")
}

// 2. Build the condition-based query object.
// The following takes reading detailed step count (DT_CONTINUOUS_STEPS_DELTA) within a specific time range as an example.
// You can add other types of latest data you want to read.
val readOptions = ReadOptions.Builder().read(DataType.DT_CONTINUOUS_STEPS_DELTA)
    .setTimeRange(startDate!!.time, endDate!!.time, TimeUnit.MILLISECONDS)
    .build()

// 3. Use the specified condition query object to call the read API to query the sampling dataset.
val readReplyTask = dataController!!.read(readOptions)

// 4. Calling the read API to query the sampling dataset is an asynchronous operation. Therefore, a listener needs to be registered to monitor whether the data query is successful or not.
readReplyTask!!.addOnSuccessListener { readReply ->
    logger("Successfully read a SampleSet from HMS core")
    for (sampleSet in readReply.sampleSets) {
        showSampleSet(sampleSet)
    }
}!!.addOnFailureListener { e -> logger(e.toString()) }

If you directly copy and use the preceding sample code, the following three errors will be reported. The related solutions are provided as follows:

  1. dataController is not defined.

    Solution: See dataController is not defined.

  2. The helper function showSampleSet is not defined.

    Solution: See The helper function showSampleSet is not defined.

  3. The helper function logger is not defined.

    Solution: See The helper function logger is not defined.

[h2]readDailySummation(DataType dataType, int startTime, int endTime)

Method

public Task<SampleSet> readDailySummation(DataType dataType, int startTime, int endTime)

Reads the daily statistics of a specified data type.

You can set the data type, start time, and end time to read the daily statistics in the specified period. If the related data type does not support polymerization statistics, an exception will be thrown.

Parameters

Name

Description

dataType

Data types to be read.

Currently, only the following data types are supported:

Calories: DT_CONTINUOUS_CALORIES_BURNT

Distance: DT_CONTINUOUS_DISTANCE_DELTA

Heart rate: DT_INSTANTANEOUS_HEART_RATE

Incremental step count: DT_CONTINUOUS_STEPS_DELTA

Weight: DT_INSTANTANEOUS_BODY_WEIGHT

Stress: DT_INSTANTANEOUS_STRESS

Moderate to high intensity: DT_CONTINUOUS_EXERCISE_INTENSITY_V2

Resting heart rate: DT_INSTANTANEOUS_RESTING_HEART_RATE

Body temperature: DT_INSTANTANEOUS_BODY_TEMPERATURE

Skin temperature: DT_INSTANTANEOUS_SKIN_TEMPERATURE

Jump: DT_CONTINUOUS_JUMP

Exercise heart rate: DT_INSTANTANEOUS_EXERCISE_HEART_RATE

Altitude: DT_INSTANTANEOUS_ALTITUDE

Rope jumping speed: DT_INSTANTANEOUS_SKIP_SPEED

Blood pressure: DT_INSTANTANEOUS_BLOOD_PRESSURE

Water consumed: DT_INSTANTANEOUS_HYDRATE

Stroke rate in rowing: DT_INSTANTANEOUS_STROKE_RATE

Pedaling rate: DT_INSTANTANEOUS_PEDALING_RATE

SpO2: DT_INSTANTANEOUS_SPO2

Stroke rate in swimming: DT_INSTANTANEOUS_SWIMMING_STROKE_RATE

SWOLF: DT_INSTANTANEOUS_SWIMMING_SWOLF

Maximum oxygen uptake: DT_VO2MAX

Continuous blood glucose: DT_CGM_BLOOD_GLUCOSE

startTime

Start time.

An 8-digit integer in the format of YYYYMMDD, for example, 20200803.

The value must be later than January 1, 2014 (represented by 20140101).

endTime

End time.

An 8-digit integer in the format of YYYYMMDD, for example, 20200903.

The value must be later than January 1, 2014 (represented by 20140101).

Returns

Type

Description

Task<SampleSet>

Statistical value of the data type.

All data points with the start time or end time being in the specified time range will be queried. The sum value of the queried data points will be returned.

Different from the readTodaySummation API, the readDailySummation API can be used to query the data of multiple days, with a statistical value provided for each day.

Sample code for reading the daily statistics of a specified data type:
Java
// 1. Initialize the start time and end time, in the format of YYYYMMDD.
int startTime = 20200818;
int endTime = 20200827;

// 2. Use the specified data type (DT_CONTINUOUS_STEPS_DELTA), start time, and end time to call readDailySummation.
// The following takes reading the step count (DT_CONTINUOUS_STEPS_DELTA) statistics of multiple days as an example.
// You can add other types of statistics you want to read. (For details, please refer to read the statistics of multiple days.)
Task<SampleSet> daliySummationTask =
    dataController.readDailySummation(DataType.DT_CONTINUOUS_STEPS_DELTA, startTime, endTime);

daliySummationTask.addOnSuccessListener(new OnSuccessListener<SampleSet>() {
    @Override
    public void onSuccess(SampleSet sampleSet) {
        logger("Successfully read daily summation from HMS core");
        if (sampleSet != null) {
            showSampleSet(sampleSet);
        }
    }
});
daliySummationTask.addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(Exception e) {
        logger("readTodaySummation" + e.toString());
    }
});
Kotlin
// 1. Initialize the start time and end time, in the format of YYYYMMDD.
val endTime = 20200827
val startTime = 20200818

// 2. Use the specified data type (DT_CONTINUOUS_STEPS_DELTA), start time, and end time to call readDailySummation.
// The following takes reading the step count (DT_CONTINUOUS_STEPS_DELTA) statistics of multiple days as an example.
// You can add other types of statistics you want to read. (For details, please refer to read the statistics of multiple days.)
val daliySummationTask =
    dataController!!.readDailySummation(DataType.DT_CONTINUOUS_STEPS_DELTA, startTime, endTime)

daliySummationTask.addOnSuccessListener { sampleSet ->
    logger("Success read daily summation from HMS core")
    if (sampleSet != null) {
        showSampleSet(sampleSet)
    }
}
daliySummationTask.addOnFailureListener { e -> logger(e.toString()) }

If you directly copy and use the preceding sample code, the following three errors will be reported. The related solutions are provided as follows:

  1. dataController is not defined.

    Solution: See dataController is not defined.

  2. The helper function showSampleSet is not defined.

    Solution: See The helper function showSampleSet is not defined.

  3. The helper function logger is not defined.

    Solution: See The helper function logger is not defined.

[h2]readDailySummation(List<DataType> dataTypes, int startTime, int endTime)

Method

public Task<List<SampleSet>> readDailySummation(List<DataType> dataTypes, int startTime, int endTime)

Reads the daily statistics of multiple data types.

You can set the data type list, start time, and end time to read the daily statistics in the specified period. If any data type does not support polymerization statistics, an exception will be thrown.

Parameters

Name

Description

dataTypes

Data types to be read.

Currently, only the following data types are supported:

Calories: DT_CONTINUOUS_CALORIES_BURNT

Distance: DT_CONTINUOUS_DISTANCE_DELTA

Heart rate: DT_INSTANTANEOUS_HEART_RATE

Incremental step count: DT_CONTINUOUS_STEPS_DELTA

Weight: DT_INSTANTANEOUS_BODY_WEIGHT

Stress: DT_INSTANTANEOUS_STRESS

Moderate to high intensity: DT_CONTINUOUS_EXERCISE_INTENSITY_V2

Resting heart rate: DT_INSTANTANEOUS_RESTING_HEART_RATE

Body temperature: DT_INSTANTANEOUS_BODY_TEMPERATURE

Skin temperature: DT_INSTANTANEOUS_SKIN_TEMPERATURE

Jump: DT_CONTINUOUS_JUMP

Exercise heart rate: DT_INSTANTANEOUS_EXERCISE_HEART_RATE

Altitude: DT_INSTANTANEOUS_ALTITUDE

Rope jumping speed: DT_INSTANTANEOUS_SKIP_SPEED

Blood pressure: DT_INSTANTANEOUS_BLOOD_PRESSURE

Water consumed: DT_INSTANTANEOUS_HYDRATE

Stroke rate in rowing: DT_INSTANTANEOUS_STROKE_RATE

Pedaling rate: DT_INSTANTANEOUS_PEDALING_RATE

SpO2: DT_INSTANTANEOUS_SPO2

Stroke rate in swimming: DT_INSTANTANEOUS_SWIMMING_STROKE_RATE

SWOLF: DT_INSTANTANEOUS_SWIMMING_SWOLF

Maximum oxygen uptake: DT_VO2MAX

Continuous blood glucose: DT_CGM_BLOOD_GLUCOSE

startTime

Start time.

An 8-digit integer in the format of YYYYMMDD, for example, 20200803.

The value must be later than January 1, 2014 (represented by 20140101).

endTime

End time.

An 8-digit integer in the format of YYYYMMDD, for example, 20200903.

The value must be later than January 1, 2014 (represented by 20140101).

Returns

Type

Description

Task<List<SampleSet>>

List of SampleSet. Each SampleSet indicates the daily statistics of a data type. If no data is found, the SamplePoint list in SampleSet is empty.

[h2]readTodaySummation(DataType dataType)

Method

public Task<SampleSet> readTodaySummation(DataType dataType)

Reads the statistics of a specified data type of the current day. If the specified data type does not support polymerization statistics, an exception will be thrown.

Parameters

Name

Description

dataType

Data types to be read.

Currently, only the following data types are supported:

Calories: DT_CONTINUOUS_CALORIES_BURNT

Distance: DT_CONTINUOUS_DISTANCE_DELTA

Heart rate: DT_INSTANTANEOUS_HEART_RATE

Incremental step count: DT_CONTINUOUS_STEPS_DELTA

Weight: DT_INSTANTANEOUS_BODY_WEIGHT

Stress: DT_INSTANTANEOUS_STRESS

Moderate to high intensity: DT_CONTINUOUS_EXERCISE_INTENSITY_V2

Resting heart rate: DT_INSTANTANEOUS_RESTING_HEART_RATE

Body temperature: DT_INSTANTANEOUS_BODY_TEMPERATURE

Skin temperature: DT_INSTANTANEOUS_SKIN_TEMPERATURE

Jump: DT_CONTINUOUS_JUMP

Exercise heart rate: DT_INSTANTANEOUS_EXERCISE_HEART_RATE

Altitude: DT_INSTANTANEOUS_ALTITUDE

Rope jumping speed: DT_INSTANTANEOUS_SKIP_SPEED

Blood pressure: DT_INSTANTANEOUS_BLOOD_PRESSURE

Water consumed: DT_INSTANTANEOUS_HYDRATE

Stroke rate in rowing: DT_INSTANTANEOUS_STROKE_RATE

Pedaling rate: DT_INSTANTANEOUS_PEDALING_RATE

SpO2: DT_INSTANTANEOUS_SPO2

Stroke rate in swimming: DT_INSTANTANEOUS_SWIMMING_STROKE_RATE

SWOLF: DT_INSTANTANEOUS_SWIMMING_SWOLF

Maximum oxygen uptake: DT_VO2MAX

Continuous blood glucose: DT_CGM_BLOOD_GLUCOSE

Returns

Type

Description

Task<SampleSet>

Statistical value of the data type. Calculation will be performed on the all queried data points with the start time or end time being in the specified time range, and the statistical results will be returned. Only the preceding data types are supported.

Sample code for reading the statistics of a specified data type of the current day:
Java
// 1. Use the specified data type (DT_CONTINUOUS_STEPS_DELTA) to call readTodaySummation to query the statistical data of this data type of the current day.
Task<SampleSet> todaySummationTask = dataController.readTodaySummation(DataType.DT_CONTINUOUS_STEPS_DELTA); 
 
// 2. Call readTodaySummation to query the statistics of the current day. As such query is asynchronous, you need to set a listener to return the data query result (success or failure).
// Note: The following takes reading the step count statistics of the current day as an example. The query time range starts from the timestamp of 00:00:00 of the day and ends at the system timestamp when the API is called.
// Calling this API will query all data points with the start time or end time being in the specified time range. The sum value of the queried data points will be returned.
// You can add other types of statistics you want to read. (For details, please refer to read the statistics of the day.)
todaySummationTask.addOnSuccessListener(new OnSuccessListener<SampleSet>() { 
    @Override 
    public void onSuccess(SampleSet sampleSet) { 
        logger("Success read today summation from HMS core"); 
        if (sampleSet != null) { 
            showSampleSet(sampleSet); 
        } 
    } 
}); 
todaySummationTask.addOnFailureListener(new OnFailureListener() { 
    @Override 
    public void onFailure(Exception e) { 
        String errorCode = e.getMessage(); 
        String errorMsg = HiHealthStatusCodes.getStatusCodeMessage(Integer.parseInt(errorCode)); 
        logger(errorCode + ": " + errorMsg); 
    } 
});
Kotlin
// 1. Use the specified data type (DT_CONTINUOUS_STEPS_DELTA) to call readTodaySummation to query the statistical data of this data type of the current day.
val todaySummationTask =
    dataController!!.readTodaySummation(DataType.DT_CONTINUOUS_STEPS_DELTA)

// 2. Call readTodaySummation to query the statistics of the current day. As such query is asynchronous, you need to set a listener to return the data query result (success or failure).
// Note: The following takes reading the step count statistics of the current day as an example. The query time range starts from the timestamp of 00:00:00 of the day and ends at the system timestamp when the API is called.
// Calling this API will query all data points with the start time or end time being in the specified time range. The sum value of the queried data points will be returned.
// You can add other types of statistics you want to read. (For details, please refer to read the statistics of the day.)
todaySummationTask!!.addOnSuccessListener { sampleSet ->
    logger("Successfully read today summation from HMS core")
    if (sampleSet != null) {
        showSampleSet(sampleSet)
    }
}
todaySummationTask.addOnFailureListener { e ->
    logger(e.toString())
}

If you directly copy and use the preceding sample code, the following three errors will be reported. The related solutions are provided as follows:

  1. dataController is not defined.

    Solution: See dataController is not defined.

  2. The helper function showSampleSet is not defined.

    Solution: See The helper function showSampleSet is not defined.

  3. The helper function logger is not defined.

    Solution: See The helper function logger is not defined.

[h2]readTodaySummation(List<DataType> dataTypes)

Method

public Task<List<SampleSet>> readTodaySummation(List<DataType> dataTypes)

Reads the statistics of multiple data types of the current day. If any data type does not support polymerization statistics, an exception will be thrown.

Parameters

Name

Description

dataTypes

Data types to be read.

Currently, only the following data types are supported:

Calories: DT_CONTINUOUS_CALORIES_BURNT

Distance: DT_CONTINUOUS_DISTANCE_DELTA

Heart rate: DT_INSTANTANEOUS_HEART_RATE

Incremental step count: DT_CONTINUOUS_STEPS_DELTA

Weight: DT_INSTANTANEOUS_BODY_WEIGHT

Stress: DT_INSTANTANEOUS_STRESS

Moderate to high intensity: DT_CONTINUOUS_EXERCISE_INTENSITY_V2

Resting heart rate: DT_INSTANTANEOUS_RESTING_HEART_RATE

Body temperature: DT_INSTANTANEOUS_BODY_TEMPERATURE

Skin temperature: DT_INSTANTANEOUS_SKIN_TEMPERATURE

Jump: DT_CONTINUOUS_JUMP

Exercise heart rate: DT_INSTANTANEOUS_EXERCISE_HEART_RATE

Altitude: DT_INSTANTANEOUS_ALTITUDE

Rope jumping speed: DT_INSTANTANEOUS_SKIP_SPEED

Blood pressure: DT_INSTANTANEOUS_BLOOD_PRESSURE

Water consumed: DT_INSTANTANEOUS_HYDRATE

Stroke rate in rowing: DT_INSTANTANEOUS_STROKE_RATE

Pedaling rate: DT_INSTANTANEOUS_PEDALING_RATE

SpO2: DT_INSTANTANEOUS_SPO2

Stroke rate in swimming: DT_INSTANTANEOUS_SWIMMING_STROKE_RATE

SWOLF: DT_INSTANTANEOUS_SWIMMING_SWOLF

Maximum oxygen uptake: DT_VO2MAX

Continuous blood glucose: DT_CGM_BLOOD_GLUCOSE

Returns

Type

Description

Task<List<SampleSet>>

List of SampleSet. Each SampleSet indicates the daily statistics of a data type. If no data is found, the SamplePoint list in SampleSet is empty.

[h2]update(UpdateOptions options)

Method

public Task<Void> update(UpdateOptions options)

Updates existing data. If the target to be updated does not exist, a new entry of data is inserted.

Parameters

Name

Description

options

UpdateOptions type, used to specify the time range of the data to be updated and the updated values.

Returns

Type

Description

Task<Void>

Task object, which is used to check whether a request is successful, fails, or is complete.

Sample code for updating data:
Java
// 1. Build the condition for data update: 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(); 
 
 // 2. Build the sampling dataset for the update: create a sampling dataset for the update based on the data collector.
SampleSet sampleSet = SampleSet.create(dataCollector); 
 
// 3. Build the start time and end time for a DT_CONTINUOUS_STEPS_DELTA sampling point for the update.
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. Data points within the specified timestamp range will be updated.
    startDate = dateFormat.parse("2020-03-17 09:00:00");
    endDate = dateFormat.parse("2020-03-17 09:05:00");
} catch (ParseException exception) {
    logger("Time parsing error");
}
int stepsDelta = 2000; 
 
// 4. Build a DT_CONTINUOUS_STEPS_DELTA sampling point for the update.
SamplePoint samplePoint = sampleSet.createSamplePoint() 
    .setTimeInterval(startDate.getTime(), endDate.getTime(), TimeUnit.MILLISECONDS); 
samplePoint.getFieldValue(Field.FIELD_STEPS_DELTA).setIntValue(stepsDelta); 
// If the updated step count data needs to be displayed on the home screen of the Huawei Health app, use addMetadata to add the following metadata to the sampling points:
samplePoint.addMetadata("motion_type", "RUN");
 
// 5. Add a DT_CONTINUOUS_STEPS_DELTA sampling point to the sampling dataset to be updated. Repeat steps 3 through 5 to add more update sampling points to the sampling dataset to be updated, if needed.
sampleSet.addSample(samplePoint); 
 
 // 6. Build a parameter object for the update.
// (1) The start time for the update object updateOption must not be later than the earliest time of any sampling point of the sampling dataset to be updated.
// (2) The end time for the update object updateOption must not be earlier than the latest time of any sampling point of the sampling dataset to be updated.
UpdateOptions updateOption = 
    new UpdateOptions.Builder().setTimeInterval(startDate.getTime(), endDate.getTime(), TimeUnit.MILLISECONDS) 
        .setSampleSet(sampleSet) 
        .build(); 
 
// 7. Use the specified parameter object for the update to call the update API to modify the sampling dataset.
Task<Void> updateTask = dataController.update(updateOption); 
 
// 8. Calling the update API to modify the sampling dataset is an asynchronous operation. Therefore, a listener needs to be registered to monitor whether the data update is successful or not.
updateTask.addOnSuccessListener(new OnSuccessListener<Void>() { 
    @Override 
    public void onSuccess(Void result) { 
        logger("Successfully update sample data from HMS core"); 
    } 
}).addOnFailureListener(new OnFailureListener() { 
    @Override 
    public void onFailure(Exception e) { 
        Log.i("DataController","Failed to update sample data from HMS core" + e.getMessage());
    } 
});
Kotlin
// 1. Build the condition for data update: a DataCollector object.
val dataCollector = DataCollector.Builder().setPackageName(applicationContext)
    .setDataType(DataType.DT_CONTINUOUS_STEPS_DELTA)
    .setDataStreamName("STEPS_DELTA")
    .setDataGenerateType(DataCollector.DATA_TYPE_RAW)
    .build()

// 2. Build the sampling dataset for the update: create a sampling dataset for the update based on the data collector.
val sampleSet = SampleSet.create(dataCollector)

// 3. Build the start time and end time for a DT_CONTINUOUS_STEPS_DELTA sampling point for the update.
val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE)
var startDate: Date? = null
var endDate: Date? = null
try {
    // Enter the start time and end time. The standard UNIX timestamp is used for storage, without considering the time zone differences. Data points within the specified timestamp range will be updated.
    startDate = dateFormat.parse("2020-08-27 09:00:00")
    endDate = dateFormat.parse("2020-08-27 09:05:00")
} catch (e: ParseException) {
    logger("Time parsing error")
}
val stepsDelta = 2000

// 4. Build a DT_CONTINUOUS_STEPS_DELTA sampling point for the update.
val samplePoint = sampleSet.createSamplePoint()
    .setTimeInterval(startDate!!.time, endDate!!.time, TimeUnit.MILLISECONDS)
samplePoint.getFieldValue(Field.FIELD_STEPS_DELTA).setIntValue(stepsDelta)

// 5. Add a DT_CONTINUOUS_STEPS_DELTA sampling point to the sampling dataset to be updated. Repeat steps 3 through 5 to add more update sampling points to the sampling dataset to be updated, if needed.
sampleSet.addSample(samplePoint)

// 6. Build a parameter object for the update.
// (1) The start time for the update object updateOption must not be later than the earliest time of any sampling point of the sampling dataset to be updated.
// (2) The end time for the update object updateOption must not be earlier than the latest time of any sampling point of the sampling dataset to be updated.
val updateOptions = UpdateOptions.Builder()
    .setTimeInterval(startDate.time, endDate.time, TimeUnit.MILLISECONDS)
    .setSampleSet(sampleSet)
    .build()

// 7. Use the specified parameter object for the update to call the update API to modify the sampling dataset.
val updateTask = dataController!!.update(updateOptions)

// 8. Calling the update API to modify the sampling dataset is an asynchronous operation. Therefore, a listener needs to be registered to monitor whether the data update is successful or not.
updateTask!!.addOnSuccessListener {
    logger("Successfully update sample data from HMS core")
}!!.addOnFailureListener { e -> logger(e.toString()) }

If you directly copy and use the preceding sample code, the following two errors will be reported. The related solutions are provided as follows:

  1. dataController is not defined.

    Solution: See dataController is not defined.

  2. The helper function logger is not defined.

    Solution: See The helper function logger is not defined.

[h2]readLatestData(List<DataType> dataTypes)

Method

public Task<Map<DataType, SamplePoint>> readLatestData(List<DataType> dataTypes)

Reads the latest data points of a specified data type.

Parameters

Name

Description

dataTypes

Data types to be read.

Currently, only the following data types are supported:

Height: DataType.DT_INSTANTANEOUS_HEIGHT

Weight: DataType.DT_INSTANTANEOUS_BODY_WEIGHT

Heart rate: DataType.DT_INSTANTANEOUS_HEART_RATE

Stress: DataType.DT_INSTANTANEOUS_STRESS

Blood pressure: HealthDataTypes.DT_INSTANTANEOUS_BLOOD_PRESSURE

Blood glucose: HealthDataTypes.DT_INSTANTANEOUS_BLOOD_GLUCOSE

SpO2: HealthDataTypes.DT_INSTANTANEOUS_SPO2

Maximum oxygen uptake: DataType.DT_VO2MAX

Returns

Type

Description

Task<Map<DataType, SamplePoint>>

Latest data points of a specified data type.

The latest data points are returned in the Map form. If they are not available for the corresponding data type, the key value of the data type won't be contained in the Map.

Sample code for reading the latest data points of a specified data type:
Java
// 1. Use the specified data type (DT_INSTANTANEOUS_HEIGHT) to query the latest data points.
List<DataType> dataTypes = new ArrayList<>();
// The following takes reading the latest height data (DT_INSTANTANEOUS_HEIGHT) as an example.
// You can add other types of latest data you want to read. (For details about the supported data types, please refer to read the latest data.)
dataTypes.add(DataType.DT_INSTANTANEOUS_HEIGHT);
Task<Map<DataType, SamplePoint>> readLatestDatas = dataController.readLatestData(dataTypes);

// 2. Calling the API to query the latest data points is an asynchronous operation. Therefore, a listener needs to be registered to monitor whether the query is successful or not.
readLatestDatas.addOnSuccessListener(new OnSuccessListener<Map<DataType, SamplePoint>>() {
    @Override
    public void onSuccess(Map<DataType, SamplePoint> samplePointMap) {
        logger("Success read latest data from HMS core");
        if (samplePointMap != null) {
            for (DataType dataType : dataTypes) {
                if (samplePointMap.containsKey(dataType)) {
                    showSamplePoint(samplePointMap.get(dataType));
                } else {
                    logger("The DataType " + dataType.getName() + " has no latest data");
                }
            }
        }
    }
});
readLatestDatas.addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(Exception e) {
        Log.i("DataController","Failed to read latest data from HMS core" + e.getMessage());
    }
});
Kotlin
// 1. Use the specified data type (DT_INSTANTANEOUS_HEIGHT) to query the latest data points.
// The following takes reading the latest height data (DT_INSTANTANEOUS_HEIGHT) as an example.
// You can add other types of latest data you want to read. (For details about the supported data types, please refer to read the latest data.)
val dataTypes = ArrayList<DataType>()
dataTypes.add(DataType.DT_INSTANTANEOUS_HEIGHT)
val readLatestDatas = dataController!!.readLatestData(dataTypes)

// 2. Calling the API to query the latest data points is an asynchronous operation. Therefore, a listener needs to be registered to monitor whether the query is successful or not.
readLatestDatas.addOnSuccessListener(OnSuccessListener<Map<DataType, SamplePoint>> { samplePointMap ->
    logger("Success read latest data from HMS core")
    if (samplePointMap != null) {
        for (dataType in dataTypes) {
            if (samplePointMap.containsKey(dataType)) {
                showSamplePoint(samplePointMap[dataType]!!)
            } else {
                logger("The DataType " + dataType.name + " has no latest data")
            }
        }
    }
})
readLatestDatas.addOnFailureListener(OnFailureListener { e ->
    val errorCode = e.message
    val errorMsg = HiHealthStatusCodes.getStatusCodeMessage(Integer.parseInt(errorCode!!))
    logger("$errorCode: $errorMsg")
})

If you directly copy and use the preceding sample code, the following three errors will be reported. The related solutions are provided as follows:

  1. dataController is not defined.

    Solution: See dataController is not defined.

  2. The helper function logger is not defined.

    Solution: See The helper function logger is not defined.

  3. The helper function showSamplePoint is not defined.

    Solution: Define an empty showSamplePoint function. Refer to the following sample code:

    Java
    /**
     * Print the SamplePoint as an output.
     *
     * @param samplePoint Data point
     */
    private void showSamplePoint(SamplePoint samplePoint) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
        logger("Sample point type: " + samplePoint.getDataType().getName());
        logger("Start: " + dateFormat.format(new Date(samplePoint.getStartTime(TimeUnit.MILLISECONDS))));
        logger("End: " + dateFormat.format(new Date(samplePoint.getEndTime(TimeUnit.MILLISECONDS))));
        for (Field field : samplePoint.getDataType().getFields()) {
            logger("Field: " + field.getName() + " Value: " + samplePoint.getFieldValue(field));
        }
        logger(System.lineSeparator());
    }
    Kotlin
    /**
     * Print the SamplePoint as an output.
     *
     * @param samplePoint Data point
     */
    private fun showSamplePoint(samplePoint: SamplePoint) {
        val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE)
    
        logger("Sample point type: " + samplePoint.dataType.name)
        logger("Start: " + dateFormat.format(Date(samplePoint.getStartTime(TimeUnit.MILLISECONDS))))
        logger("End: " + dateFormat.format(Date(samplePoint.getEndTime(TimeUnit.MILLISECONDS))))
        for (field in samplePoint.dataType.fields) {
            logger("Field: " + field.name + " Value: " + samplePoint.getFieldValue(field))
        }
        logger(System.lineSeparator())
    }