Samsung Health Calorie Checker

Overview

Samsung Health is an engaging app for fitness, wellness, and care. It helps you track your activity data and gain insights from the collected data. It is available for both Android and iOS users.

Moreover, the data from a wearable device like Galaxy Watch or from a mobile phone is synced to Samsung Health. Through Samsung Health Android SDK, you can share the user’s health data. With more than 70 Million Samsung Health users, join and be part of the Samsung Health ecosystem for more services.

Figure 1 Samsung Health Featured Apps

Figure 1 Samsung Health Featured Apps

Objective

Create a Calorie Checker app with Samsung Health Android SDK to monitor calories burned and intake calories with user goals.

Setup

Requirements:
  1. a) At least Java SE Development Kit 8

  2. b) Android Studio

  3. c) Mobile Phone

Initial Setup:
  1. a) To activate developer mode of Samsung Health, tap the version line 10 times as seen below. Navigate it through Samsung Health > Settings > About Samsung Health.

    Figure 2 Activating Developer Mode

    Figure 2 Activating Developer Mode

  2. b) Open the given Android Application Project.

Application 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 on the right:

  1. a) Calories burned with a goal

  2. b) Intake calories with a goal

Figure 3 Today’s Step App

Figure 3 Today’s Step App

Locate Files

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

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.

Figure 4 Permission Request

Figure 4 Permission Request

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 regarding to 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

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)");
    } 

Result Check

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

  1. a) Burned calories

  2. b) Intake calories

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

Figure 5 Calorie Checker App

Figure 5 Calorie Checker App