Development

Step 1: Open the app project

Open the given Android app project and find ExerciseReporter.java.

The SDK library is samsung-health-data.jar in lib.


Step 2: Make a query to read exercise data

Create a ReadRequest instance:

  • Data type name: HealthConstants.Exercise.HEALTH_DATA_TYPE
/*********************************************************************************
 * [Practice 1] Make a query to read exercise data.
 *    - Fill "TODO 1" with the exercise's data type
 ---------------------------------------------------------------------------------
 *    - (Hint) See the video tutorial and find exercise's data type
 *********************************************************************************/
// Build a query for getting exercise data
HealthDataResolver.ReadRequest request = new HealthDataResolver.ReadRequest.Builder()
        .setDataType(/*TODO 1*/)
        .setLocalTimeRange(HealthConstants.Exercise.START_TIME,
                HealthConstants.Exercise.TIME_OFFSET, dayTime, dayTime + ONE_DAY)
        .setSort(HealthConstants.Exercise.START_TIME, HealthDataResolver.SortOrder.DESC)
        .setResultCount(0,1)
        .build();

Step 3: Get today’s exercise data in a result

HealthConstants.Exercise has:

HealthConstants.Exercise
Field

Description

EXERCISE_TYPE

Predefined exercise type, e.g. 1002 is running.
* Type: int

CALORIE

Burned calorie during the activity in kilocalories.
* Type: float

DURATION

Duration of this exercise in milliseconds.
* Type: long

A listener receives a query result. Check the following info of the received health data:

  • HealthConstants.Exercise.EXERCISE_TYPE
  • HealthConstants.Exercise.CALORIE
  • HealthConstants.Exercise.DURATION
try {
    /*********************************************************************************
     * [Practice 2] Check the received exercise data.
     *    - Fill "TODO 2" for the exercise type
     *    - Fill "TODO 3" for the exercise's calories
     *    - Fill "TODO 4" for the exercise's duration
     ---------------------------------------------------------------------------------
     *    - (Hint) See the video  tutorial and find exercise's data type
     *********************************************************************************/
    resolver.read(request).setResultListener(result -> {
        try {
            Iterator<HealthData> iter = result.iterator();
            if (iter.hasNext()) {
                HealthData data = iter.next();
                long startTime = data.getLong(HealthConstants.Exercise.START_TIME);
                long endTime = data.getLong(HealthConstants.Exercise.END_TIME);
                long timeOffset = data.getLong(HealthConstants.Exercise.TIME_OFFSET);
                int type = data.getInt(/*TODO 2*/);
                float calorie = data.getFloat(/*TODO 3*/);
                long duration = data.getLong(/*TODO 4*/);

Step 4: Check LIVE_DATA

Exercise data’s live_data is in compressed JSON format.

It includes detailed info during the exercise.

HealthConstants.Exercise
Field

Description

LIVE_DATA

Detailed status during exercise with compressed JSON format.
* Type: byte[]

Key Type Unit
"start_time"

long UTC millisecond
"heart_rate"

float Beat per minute
"cadence"

float Rate per minute
"count"

int
"power"

float Watt
"speed"

float Meter per second
"distance"

float Meter

To get detailed info of the compressed JSON data easily, HealthDataUtil provides the following API.

HealthDataUtil
static<T>T

getStructuredDataList(byte[] jsonBlob, Class&lt;T&gt;clazz)
Converts a given raw data to the particular class type

Look into LIVE_DATA and check:

/*********************************************************************************
 * [Practice 3] Get live_data with structured data list.
 *    - Fill "TODO 5" with a proper param of HealthDataUtil.getStructuredDataList().
 ---------------------------------------------------------------------------------
 *    - (Hint) See the video tutorial and find HealthDataUtil.getStructuredDataList().
 *********************************************************************************/
byte[] blob = data.getBlob(HealthConstants.Exercise.LIVE_DATA);
List<ExerciseLiveData> liveData = new ArrayList<>();
if (blob != null) {
    liveData = HealthDataUtil.getStructuredDataList(/*TODO 5*/, ExerciseLiveData.class);
}

Step 5: Run the app

Run the app project and check the app dashboard on a phone.

  • Exercise type, duration, and calories
  • Detailed status in LIVE_DATA

You're done!

Congratulations! You have successfully achieved the goal of this Code Lab activity. Now, you can create a detailed health dashboard app by yourself! But, if you're having trouble, you may check out the link below.

Advanced Health Dashboard Complete Code377.27 KB