Development

At this part, you’ll create an app that counts your steps in a day. The step data from the Samsung Health can be read from both a Galaxy watch and a phone. Using the Daily Step Count Trend data type, all steps will be retrieved without duplication.

Upon successful implementation of the succeeding steps, you can see the results as follows:

  • All Steps
  • Steps when using Watch
  • Steps when using Phone

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 Today’s Steps app project. Handling the user’s health data is only available upon user consent.

public static final Set<PermissionKey> PERMISSION_KEY_SET;
static {
    Set<PermissionKey> keyset = new HashSet<>();
    keyset.add(new PermissionKey("com.samsung.shealth.step_daily_trend", PermissionType.READ));
    keyset.add(new PermissionKey(HealthConstants.StepCount.HEALTH_DATA_TYPE, 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.

Step 3: Daily step data type

There are times that you are carrying a phone while wearing a watch at the same time. And, there are times when you use these separately. Here’s an example scenario:

Timeline:

  1. 9:00 ~ 10:00 – carries both Watch and Phone.
  2. 10:00 ~ 10:40 – carries only Phone.
  3. 10:40 ~ 12:00 – carries only Watch.
  4. 12:00 ~ 13:00 – carries both Watch and Phone.

In 1 and 4, it is possible that the steps counted are duplicated since the user carried both devices at the same time. The Daily Step Count Trend is useful on these scenarios where users want to read daily steps on multiple devices. The date of each data is recorded as a day_time value, which indicates 00:00:00 TUC of the date.

"com.samsung.shealth.step_daily_trend"

Field

Description

"day_time"

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

"count"

[Mandatory]
Total number of daily steps.

"source_type"

Kind of devices.
All device is “-2”

Step 4: Practice for reading all device’s steps

Conditions to get today’s steps for all devices are part of the filter as seen below:

  1. Today’s day_time
  2. "-2" source type for all devices
Filter filter = Filter.and(
        // filter for today
        Filter.eq("day_time", Util.getTodayStartTime()),
        // filter for all devices
        Filter.eq("source_type", -2)
);

Set the data type to read all steps.

/*********************************************************************************
 * [Practice] Build read request for today's all step 
 *    - Set data type of calculated step count 
 ----------------------------------------------------------------------------------
 *    - (Hint) Fill below TODO 1 with "com.samsung.shealth.step_daily_trend""
 *********************************************************************************/
ReadRequest request = new ReadRequest.Builder()
        .setDataType("TODO 1")
        .setFilter(filter)
        .build();

Step 5: Check results

Run the Today’s Steps app and check its results if it shows the following health data:

  1. All steps
  2. Watch steps
  3. Phone steps


You're done!

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