Development

On this part, you’ll create a simple calorie checker with the Samsung Health SDK. The app can read and write both food intake and burned calories data to the Samsung Health.

When you follow this section successfully, you can see the following data:

  • Calories burned with a goal
  • Intake calories with a goal

Step 1: Locate files

The SDK library is located at lib/samsung-health-data.jar of the project file.

Step 2: Permission request

Open HealthDataProvider.java in the Calorie Checker app project. Handling the user’s health data is only available upon user consent.

static {
    Set<PermissionKey> keyset = new HashSet<>();
    keyset.add(new PermissionKey(Exercise.HEALTH_DATA_TYPE, PermissionType.READ));
    keyset.add(new PermissionKey(Exercise.HEALTH_DATA_TYPE, PermissionType.WRITE));
    keyset.add(new PermissionKey(Nutrition.HEALTH_DATA_TYPE, PermissionType.WRITE));
    keyset.add(new PermissionKey(Nutrition.HEALTH_DATA_TYPE, PermissionType.READ));
    keyset.add(new PermissionKey("com.samsung.shealth.calories_burned", PermissionType.READ));
    PERMISSION_KEY_SET = Collections.unmodifiableSet(keyset);
}

static void requestPermissions(Activity activity, HealthDataStore store,
                               ResultListener<PermissionResult> listener) {
    HealthPermissionManager pmsManager = new HealthPermissionManager(store);
    pmsManager.requestPermissions(PERMISSION_KEY_SET, activity).setResultListener(listener);

}

In your mobile phone, when you connect the created app to Samsung Health, you will need to enable the data permission and select Done.

Adding nutrition and exercise data

Writing health data to Samsung Health is available with its SDK. After running the Calorie Checker app, add the food intake and exercise data. These added data are shown on Samsung Health as well.

Calories burned data type

The calories burned data type has fields for active time, rest time, and thermic effect of food (TEF). Refer to the table below for the calories burned data types.

"com.samsung.shealth.calories_burned"
Field

Description

"day_time"

[Mandatory]
The date of summarized data in milliseconds.
It indicates 00:00:00 UTC of the date exactly.

"active_calorie"

[Mandatory]
Calories burned during active time including walking
and exercise.

"rest_calorie"

[Mandatory]
Calories burned during rest time

"tef_calorie"

[Mandatory]
Thermic Effect of Food. The energy expenditure
during digestion (10% of caloric intake)

To get the total calories burned:

active_calorie + rest_calorie + tef_calories

Step 3: Practice for total calories burned

Set the data to read calories burned.

/*********************************************************************************
 * [Practice 1] Build read request for today's calorie burned 
 *    - Set data type of calorie burned
 ---------------------------------------------------------------------------------
 *    - (Hint) Fill below TODO 1-1 (1) for data type with "com.samsung.shealth.calories_burned"
 *********************************************************************************/ 
ReadRequest request = new ReadRequest.Builder()
        .setDataType("TODO 1")
        .setFilter(Filter.eq("day_time", todayDayTime))
        .build();

As mentioned, to get the total calories, simply get the sum of the active, rest, and TEF calories. Add up all the property values in the result.

mResolver.read(request).setResultListener(result -> {
    float sumOfCalorie = 0f;
 /*********************************************************************************
  * [Practice 2] Get three types of calories from the result
  *    They are active calorie, rest calorie, and tef calorie
  ---------------------------------------------------------------------------------
  *    - (Hint) Fill below TODOs with 
  *	         (1) "active_calorie", 
  *          (2) "rest_calorie", and
  *          (3) "tef_calorie"                
  *********************************************************************************/ 
      
    for (HealthData data : result) {
        sumOfCalorie += data.getFloat("TODO 2 (1)");
        sumOfCalorie += data.getFloat("TODO 2 (2)");
        sumOfCalorie += data.getFloat("TODO 2 (3)");
    } 

Step 4: Result check

Run the Calorie Checker app and check its results on the phone which should have the following health data:

  • Burned calories

  • Intake calories

The goal defined for the burned and intake calories are 2300 kcal and 2000 kcal respectively.


You're done!

Congratulations! You have successfully achieved the goal of this Code Lab topic. Now, you can create a calorie checker app by yourself!