cph-cachet / flutter-plugins

A collection of Flutter plugins developed by CACHET
555 stars 682 forks source link

[HEALTH 8.1.0] SLEEP_IN_BED diffrentiate between apple watch and sleep scheduling data IOS #864

Open Kam5678 opened 11 months ago

Kam5678 commented 11 months ago

For the following data, I call the following function:

inBedData = await health.getHealthDataFromTypes(leftBound, rightBound, [HealthDataType.SLEEP_IN_BED]);
leftBound = 2023-12-17 00:00:00
rightBound = 2023-12-18 00:00:00

And get the following data: (I excluded deviceID, sourceID)

flutter: HealthDataPoint -
    value: 427.50831666666664,
    unit: MINUTE,
    dateFrom: 2023-12-17 22:53:07.000,
    dateTo: 2023-12-18 06:00:37.499,
    dataType: SLEEP_IN_BED,
    platform: PlatformType.IOS,
    sourceName: Kam’s iPhone
flutter: HealthDataPoint -
    value: 39.0,
    unit: MINUTE,
    dateFrom: 2023-12-17 23:48:25.616,
    dateTo: 2023-12-18 00:27:25.616,
    dataType: SLEEP_IN_BED,
    platform: PlatformType.IOS,
    sourceName: Kam’s Apple Watch
flutter: HealthDataPoint -
    value: 58.0,
    unit: MINUTE,
    dateFrom: 2023-12-17 22:49:55.616,
    dateTo: 2023-12-17 23:47:55.616,
    dataType: SLEEP_IN_BED,
    platform: PlatformType.IOS,
    sourceName: Kam’s Apple Watch
flutter: HealthDataPoint -
    value: 54.0,
    unit: MINUTE,
    dateFrom: 2023-12-17 06:34:29.840,
    dateTo: 2023-12-17 07:28:29.840,
    dataType: SLEEP_IN_BED,
    platform: PlatformType.IOS,
    sourceName: Kam’s Apple Watch
flutter: HealthDataPoint -
    value: 58.50475,
    unit: MINUTE,
    dateFrom: 2023-12-17 06:01:30.000,
    dateTo: 2023-12-17 07:00:00.285,
    dataType: SLEEP_IN_BED,
    platform: PlatformType.IOS,
    sourceName: Kam’s iPhone
flutter: HealthDataPoint -
    value: 3.0,
    unit: MINUTE,
    dateFrom: 2023-12-17 06:29:29.840,
    dateTo: 2023-12-17 06:32:29.840,
    dataType: SLEEP_IN_BED,
    platform: PlatformType.IOS,
    sourceName: Kam’s Apple Watch
flutter: HealthDataPoint -
    value: 14.5,
    unit: MINUTE,
    dateFrom: 2023-12-17 06:13:59.840,
    dateTo: 2023-12-17 06:28:29.840,
    dataType: SLEEP_IN_BED,
    platform: PlatformType.IOS,
    sourceName: Kam’s Apple Watch
flutter: HealthDataPoint -
    value: 81.5,
    unit: MINUTE,
    dateFrom: 2023-12-17 04:51:29.840,
    dateTo: 2023-12-17 06:12:59.840,
    dataType: SLEEP_IN_BED,
    platform: PlatformType.IOS,
    sourceName: Kam’s Apple Watch
flutter: HealthDataPoint -
    value: 308.78333333333336,
    unit: MINUTE,
    dateFrom: 2023-12-17 00:52:41.000,
    dateTo: 2023-12-17 06:01:28.000,
    dataType: SLEEP_IN_BED,
    platform: PlatformType.IOS,
    sourceName: Kam’s iPhone
flutter: HealthDataPoint -
    value: 82.5,
    unit: MINUTE,
    dateFrom: 2023-12-17 03:27:59.840,
    dateTo: 2023-12-17 04:50:29.840,
    dataType: SLEEP_IN_BED,
    platform: PlatformType.IOS,
    sourceName: Kam’s Apple Watch
flutter: HealthDataPoint -
    value: 86.5,
    unit: MINUTE,
    dateFrom: 2023-12-17 02:00:59.840,
    dateTo: 2023-12-17 03:27:29.840,
    dataType: SLEEP_IN_BED,
    platform: PlatformType.IOS,
    sourceName: Kam’s Apple Watch
flutter: HealthDataPoint -
    value: 17.0,
    unit: MINUTE,
    dateFrom: 2023-12-17 01:43:29.840,
    dateTo: 2023-12-17 02:00:29.840,
    dataType: SLEEP_IN_BED,
    platform: PlatformType.IOS,
    sourceName: Kam’s Apple Watch
flutter: HealthDataPoint -
    value: 46.5,
    unit: MINUTE,
    dateFrom: 2023-12-17 00:55:59.840,
    dateTo: 2023-12-17 01:42:29.840,
    dataType: SLEEP_IN_BED,
    platform: PlatformType.IOS,
    sourceName: Kam’s Apple Watch
flutter: HealthDataPoint -
    value: 0.6333333333333333,
    unit: MINUTE,
    dateFrom: 2023-12-17 00:51:56.000,
    dateTo: 2023-12-17 00:52:34.000,
    dataType: SLEEP_IN_BED,
    platform: PlatformType.IOS,
    sourceName: Kam’s Apple Watch
flutter: HealthDataPoint -
    value: 25.45,
    unit: MINUTE,
    dateFrom: 2023-12-17 00:26:27.000,
    dateTo: 2023-12-17 00:51:54.000,
    dataType: SLEEP_IN_BED,
    platform: PlatformType.IOS,
    sourceName: Kam’s iPhone

To get an Idea of how the data looks on my phone:

On Dec 16 - 17: 00:52 -> 7:28 On Dec 17 -18 : 23:53 -> 6:00

THE PROBLEM:

So after doing some debugging, the problem is that there seem's to be two instances of Sleep_In_Bed data being saved in the list returned for SLEEP_IN_BED data. One comes from the Sleep schedule feature, where your iphone schedules your sleep time, and the other are entries from your apple watch. This causes there to be overlap of sleep data, and thus summing the list causes an inaccurate answer to be produced, that is way over the actual sleep in bed time spent.

The only difference that can be noticed is that the source name will diffrentiate between iphone and apple watch, so I thought I could use this to my advantage, BUT:

In case the user changes their name for their iphone or apple watch, to something that doesn't have these keywords, then there is no way of knowing which data for SLEEP_IN_BED is for which device.

I haven't included deviceID and sourceID in this example for privacy reasons, but from my data I have seen the deviceID is always the same but source id is different:

for iPhone: com.apple.health.739... for Apple watch: com.apple.health.FD5....

Is there some sort of convention for this? I don't think sourceID depends at all on the device model though, so I don't think it will work?

Or some better way to to diffrentiate between both types of data?

[!] Flutter (Channel [user-branch], 3.13.9, on Microsoft Windows [Version 10.0.19045.3324], locale en-CA)
    ! Flutter version 3.13.9 on channel [user-branch] at C:\flutter
      Currently on an unknown channel. Run `flutter channel` to switch to an official channel.
      If that doesn't fix the issue, reinstall Flutter by following instructions at https://flutter.dev/docs/get-started/install.
    ! Upstream repository unknown source is not a standard remote.
      Set environment variable "FLUTTER_GIT_URL" to unknown source to dismiss this error.
    • Framework revision d211f42860 (8 weeks ago), 2023-10-25 13:42:25 -0700
    • Engine revision 0545f8705d
    • Dart version 3.1.5
    • DevTools version 2.25.0
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.

[√] Windows Version (Installed version of Windows is version 10 or higher)

Model: IPHONE 15 PRO

ThomLen commented 10 months ago

I have the same issue. Almost all data comes from the Apple Watch. But sometimes - I can't see a pattern - there are totaled data from the iPhone that includes the data from the watch. This of course leads to incorrect results in the total.