-
Notifications
You must be signed in to change notification settings - Fork 696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attempt to fix Health Connect on Android 14 #834
Attempt to fix Health Connect on Android 14 #834
Conversation
I was able to pull down this PR branch and verify the functionality work. The code looks reasonable to me. This PR resolves Issue #800 Thank you @eliasteeny |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solution works and code LGTM. I think we should add some documentation to the readme though. (I submitted a PR to your branch with proposed ReadMe updates)
NOTE: I'm not a maintainer so my approval means nothing. :)
0.0, HealthDataType.SLEEP_AWAKE, earlier, now); | ||
success &= await health.writeHealthData( | ||
0.0, HealthDataType.SLEEP_DEEP, earlier, now); | ||
// success &= await health.writeHealthData( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these changes relevant to the PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah for some reason adding the sleep records is failing stating that it is an unsupported type. Although it is working on Android 13 and below.
HealthDataType.SLEEP_DEEP, | ||
HealthDataType.SLEEP_REM, | ||
HealthDataType.SLEEP_SESSION, | ||
// HealthDataType.SLEEP_AWAKE, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these changes relevant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fetching the SLEEP data is crashing the app with the same error as writing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked out this PR, uncommented the SLEEP lines, and i can read sleep data without a crash. I am running Android 14.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eliasteeny could you please double check if this error still occurs on your device and if so, tell us your OS version and setup?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got a closer look. I did not have a wearable so was reading only sleep session data (time asleep, one data point) and that worked fine. I get the crash now that i have a wearable and i try to read the stages. The issue seems to be that SleepStageRecord object no longer exists in Android 14. Instead there is a list in the SleepSessionRecord. You can see this sample code in https://developer.android.com/health-and-fitness/guides/health-connect/develop/sessions
suspend fun readSleepSessions(
healthConnectClient: HealthConnectClient,
startTime: Instant,
endTime: Instant
) {
val response =
healthConnectClient.readRecords(
ReadRecordsRequest(
SleepSessionRecord::class,
timeRangeFilter = TimeRangeFilter.between(startTime, endTime)
)
)
for (sleepRecord in response.records) {
// Retrieve relevant sleep stages from each sleep record
val sleepStages = sleepRecord.stages
}
}
The convertRecord function returns a single record and now it should return many, not sure how to handle this with the way it works now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created a pull request on your fork @eliasteeny. I tested it on Android 13 and Android 14, both were able to pull both SLEEP_SESSION (total sleep time only) and individual sleep stages.
I did not test writing though, i don't have an app that would do that.
packages/health/example/android/app/src/main/kotlin/cachet/plugins/example_app/MainActivity.kt
Show resolved
Hide resolved
Update README.md for Android 14
any updates on this PR? This is a critical bug for all android 34 devices |
Hi, due to other projects and Christmas I was not able to properly evaluate it, I will do it this week though since it is an urgent issue. |
@hoffmatteo I hope you had a great start in the new year. Just for an update, where you able to review @eliasteeny PR and have any comments on it? |
Hi, is there any way to keep private fun manageHealthAppPermission() { |
if ( healthConnectAvailable) { | ||
val requestPermissionActivityContract = PermissionController.createRequestPermissionResultContract() | ||
|
||
healthConnectRequestPermissionsLauncher =(activity as ComponentActivity).registerForActivityResult(requestPermissionActivityContract) { granted -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we use FlutterFragmentActivity
instead of ComponentActivity
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need the ComponentActivity
in order to use the new api function registerForActivityResult
, FlutterFragmentActivity
is built upon the new Android apis which can be casted to a ComponentActivity
. If you try to cast FlutterActivity
to a ComponentActivity
it will throw an error.
I have downloaded this fork and used the dependency as a reference from the download. And I can confirm this fix works. It will send me to the Health Connect Dialog to confirm the permissions and I can go from there. I would advise anyone to do this until the package is properly updated. Thank you @eliasteeny you saved my app 💪🏻 |
Getting an error while testing on a Samsung Galaxy S23 Ultra, Android 14: I/FLUTTER_HEALTH(27526): Permission launcher not found |
I created a fork of this pull request (and created a pull request with it to update this one) which fixes sleep read issues. https://github.com/eric-nextsense/flutter-plugins/tree/health-connect-android-14-fix |
Works perfectly, thanks @eric-nextsense! |
I'm getting the same error as @tomassasovsky - Permission launcher not found. I've tried referencing the https://github.com/eric-nextsense/flutter-plugins/tree/health-connect-android-14-fix repository in the pubspec.yaml file like this:
Any clues what might be wrong? |
Did you use |
Hi, I was finally able to test the PR and will include it in the upcoming release that will be coming late this week/early next week! |
Did you include the additional changes to fix sleep that are in my branch? They were not merged in this fork by the author. https://github.com/eric-nextsense/flutter-plugins/tree/health-connect-android-14-fix |
@eric-nextsense I didn't merge it because I was waiting to have some free time to also fix the writing of sleep data. |
Hi, I was just looking into the sleep issue. Your PR looks great, I added it to target health-dev and am looking into adding write functionality right now! |
This pull request addresses the "Activity not found" error encountered when attempting to open the Health Connect permissions screen on Android 14. The issue was resolved by migrating from the old
startActivityForResult
method to the new API,registerForActivityResult
.Introduced a limitation for the Main app's activity: it should extend
FlutterFragmentActivity
instead ofFlutterActivity
. This adjustment allows casting fromActivity
toComponentActivity
for accessingregisterForActivityResult
.Note: As a non-experienced Android/Kotlin developer, any suggestions for improvements or alternative approaches are welcome especially regarding
FlutterFragmentActivity
.