FitKit icon indicating copy to clipboard operation
FitKit copied to clipboard

[iOS] steps count is not correct and return the sum of steps on multiple devices regardless of using them meantime.

Open melsheikh92 opened this issue 5 years ago • 3 comments

while using Apple watch and mobile together, the library gives me the sum of the number of steps on mobile and on apple watch as an aggregation which is not correct and makes double the value.

WhatsApp Image 2020-11-11 at 9 47 31 AM WhatsApp Image 2020-11-11 at 9 47 32 AM WhatsApp Image 2020-11-11 at 9 47 32 AM (1)

while the summary is correct on mobile but it doesn't return like it:

WhatsApp Image 2020-11-11 at 9 47 31 AM (1) but the library returns 15219 steps as a count of steps.

melsheikh92 avatar Nov 11 '20 09:11 melsheikh92

@melsheikh92 mind to share the code? The FitData contains the source field which you can use to properly calculate sum of steps for users with more than one device.

vintage avatar Dec 07 '20 13:12 vintage

  • This issue is due to the usage of HKSampleQuery in this plugin. https://developer.apple.com/documentation/healthkit/hksamplequery
  • If we use HKStatisticsQuery, this problem will get eliminated and duplicate removal will be done by Apple itself. 😍 https://developer.apple.com/documentation/healthkit/hkstatisticsquery
  • It currently supports some of the health kit parameters such as step_count, distance_walk, etc., and only supported by addable data types.

Fortunately, I have modified the plugin to do that 😁

Use this in the pubspec.yaml as fitkit plugin.

fit_kit:
  git:
    url: git://github.com/dinithherath/FitKit.git

And in your code to get the exercise_minutes use this snippet.

if (!permissions) {
  log.d('RequestPermissions: Denied');
} else {
  try {
    final now = DateTime.now();
    FitDataStatistics resultAllSteps;
    FitDataStatistics resultAllExercise;
    try {
      resultAllSteps = await FitKit.readStatistics(
        DataType.STEP_COUNT,
        dateFrom: DateTime(now.year, now.month, now.day),
        dateTo: now,
      );
    } catch (e) {
      resultAllSteps = null;
    }
    try {
      resultAllExercise = await FitKit.readStatistics(
        DataType.EXERCISE_TIME,
        dateFrom: DateTime(now.year, now.month, now.day),
        dateTo: now,
      );
    } catch (e) {
      resultAllExercise = null;
    }
  } on UnsupportedException catch (e) {
    print(e.toString());
  }
}

This reads data from the health kit using the iOS cumulative sum method which will eliminate duplicates from your phone and apple watch.

If you get the work done please star my repo of Fitkit and follow me on GitHub ❤ Thanks.

DinithHerath avatar Mar 01 '21 06:03 DinithHerath

@melsheikh92 mind to share the code? The FitData contains the source field which you can use to properly calculate sum of steps for users with more than one device.

  • Even though you do like that duplicates removal will be really hard since apple uses various algorithms to optimally calculate the cumulative value.
  • Even if you some way remove duplicates based upon the source, you will never get the value that is showing for you in the health kit app.

DinithHerath avatar Mar 01 '21 06:03 DinithHerath