Shruti
Shruti

Reputation: 815

Integrating S Health SDK

I have developed my app using Samsung S Health SDK. I want to add walking, running and cycling tracking of S Health inside my app.

How to add these features?

Upvotes: 2

Views: 2132

Answers (1)

Shruti
Shruti

Reputation: 815

I added following properties inside my readTodayWalkingDistance():

private void readTodayWalkingDistance() {
    HealthDataResolver resolver = new HealthDataResolver(mStore, null);

    // Set time range from start time of today to the current time
    long startTime = getStartTimeOfToday();
    long endTime = System.currentTimeMillis();
    Filter filter = Filter.and(Filter.greaterThanEquals(HealthConstants.Exercise.START_TIME, startTime),
            Filter.lessThanEquals(HealthConstants.Exercise.START_TIME, endTime));

    HealthDataResolver.ReadRequest request = new ReadRequest.Builder()
            .setDataType(HealthConstants.Exercise.HEALTH_DATA_TYPE)
            .setProperties(new String[]{
                    HealthConstants.Exercise.EXERCISE_TYPE,
                    HealthConstants.Exercise.DURATION,
                    HealthConstants.Exercise.MAX_SPEED,
                    HealthConstants.Exercise.MEAN_SPEED,
            })
            .setFilter(filter)
            .build();

    try {
        resolver.read(request).setResultListener(mListener);
    } catch (Exception e) {
        Log.e(MainActivity.APP_TAG, e.getClass().getName() + " - " + e.getMessage());
    }
}

Then updated ResultListener:

private final HealthResultHolder.ResultListener<ReadResult> mListener = new HealthResultHolder.ResultListener<ReadResult>() {
    @Override
    public void onResult(ReadResult result) {
        int exercise_type = 0;
        long duration = 0;
        float max_speed = 0.0f, mean_speed = 0.0f;
        Cursor c = null;

        try {
            c = result.getResultCursor();
            if (c != null) {
                while (c.moveToNext()) {

                    exercise_type += c.getInt(c.getColumnIndex(HealthConstants.Exercise.EXERCISE_TYPE));
                    duration += c.getLong(c.getColumnIndex(HealthConstants.Exercise.DURATION));
                    max_speed += c.getColumnIndex(HealthConstants.Exercise.MAX_SPEED);
                    mean_speed += c.getColumnIndex(HealthConstants.Exercise.MEAN_SPEED);
                }
            }
        } finally {
            if (c != null) {
                c.close();
            }
        }
        MainActivity.getInstance().getData(exercise_type, duration, max_speed, mean_speed);
    }
};

Added following permissions in to HashSet MainActivity:

mKeySet.add(new PermissionKey(HealthConstants.Exercise.HEALTH_DATA_TYPE, PermissionType.READ));

And in AndroidManifest file:

<meta-data
    android:name="com.samsung.android.health.permission.read"
    android:value="com.samsung.health.exercise" />

Upvotes: 6

Related Questions