How to get swimming data using samsung health app

162 views Asked by At

I want to get swimming data from Samsung Health App but not get any proper solution. Below is my code.

    public class SwimmingReport {

    private final HealthDataStore mStore;
    private SwimObserver swimObserver;
    private static final long ONE_DAY_IN_MILLIS = 24 * 60 * 60 * 1000L;

    public SwimmingReport(HealthDataStore store) {
        mStore = store;
    }

    public void start(SwimObserver listener, String strDate, JSONObject jsonObject) {
        swimObserver = listener;
        HealthDataObserver.addObserver(mStore, HealthConstants.Exercise.HEALTH_DATA_TYPE, new HealthDataObserver(null) {
            @Override
            public void onChange(String s) {
                readTodaySwimData(strDate,jsonObject);
            }
        });

        readTodaySwimData(strDate,jsonObject);
    }

    private void readTodaySwimData(String strDate,JSONObject jsonObject) {
        HealthDataResolver resolver = new HealthDataResolver(mStore, null);
        long startTime = GlobalMethods.getEpochTime(strDate);
        long endTime = startTime + ONE_DAY_IN_MILLIS;

        HealthDataResolver.ReadRequest request = new HealthDataResolver.ReadRequest.Builder()
                .setDataType(HealthConstants.Exercise.HEALTH_DATA_TYPE)
                .setProperties(new String[]{HealthConstants.Exercise.EXERCISE_TYPE})
                .setLocalTimeRange(HealthConstants.Exercise.START_TIME, HealthConstants.Exercise.TIME_OFFSET,
                        startTime, endTime)
                .build();

        try {
            resolver.read(request).setResultListener(result ->{
                double distance = 0.0;
                try {
                    for (HealthData data : result) {
                        distance += data.getFloat(HealthConstants.Exercise.DISTANCE);
                    }
                } finally {
                    result.close();
                }
                if (swimObserver != null) {
                    swimObserver.onChanged(distance,strDate,jsonObject);
                }
            });
        } catch (Exception e) {
            Log.e("=> ", "Getting step count fails.", e);
        }
    }

    public interface SwimObserver {
        void onChanged(Double distance, String date, JSONObject jsonObject);
    }

}

Here is my whole source code this

1

There are 1 answers

2
MinseongPark On

I figured out on your code that you want to read distance value from the read result.

distance += data.getFloat(HealthConstants.Exercise.DISTANCE);

But you described absolutely not related property. Maybe that is the problem.

.setProperties(new String[]{HealthConstants.Exercise.EXERCISE_TYPE})

Try this.

.setProperties(new String[]{HealthConstants.Exercise.DISTANCE})