Read health data using "Health Connect" API (android java app)

427 views Asked by At

I am currently working on developing a health app and I would like to be able to read and display health data information (already stored through the phone/smart watch sensors) using the Health Connect SDK. Some tutorials are available (https://developer.android.com/health-and-fitness/guides/health-connect/develop/get-started https://developer.android.com/codelabs/health-connect#0) but unfortunately they are in Kotlin and I am coding my app in Java language. Are there any other resources that could help me with Java instead ?

I am struggling with the ReadRecordsRequest ( https://developer.android.com/reference/androidx/health/connect/client/request/ReadRecordsRequest ) and ReadRecords ( https://developer.android.com/reference/androidx/health/connect/client/HealthConnectClient#readRecords(androidx.health.connect.client.request.ReadRecordsRequest) ) methods as I tried to unsuccesfully initialise their input paramerter (T in my case) but it's not the correct type...

Here is a portion of my code :

androidx.health.connect.client.request.ReadRecordsRequest<T> request = new androidx.health.connect.client.request.ReadRecordsRequest<T>(
                        TimeRangeFilter.between(start,end),
                        emptySet(),
                        false,
                        1000,
                        null
                );

                // Call the readRecords method on the HealthConnectClient
                Continuation<? super ReadRecordsResponse<T>> cont = new Continuation<T>() {
                    @NonNull
                    @Override
                    public CoroutineContext getContext() {
                        return null;
                    }

                    @Override
                    public void resumeWith(@NonNull Object o) {

                    }
                };
                List<Record> response = healthConnectClient.readRecords(request,cont);
                return filterOxygenRecords(response);

These are the errors I get : 'readRecords(androidx.health.connect.client.request.ReadRecordsRequest, kotlin.coroutines.Continuation<? super androidx.health.connect.client.response.ReadRecordsResponse>)' in 'androidx.health.connect.client.HealthConnectClient' cannot be applied to '(androidx.health.connect.client.request.ReadRecordsRequest, kotlin.coroutines.Continuation<capture<? super ReadRecordsResponse>>)'

Unknown class: 'T'

Picture of the Expected required type vs required one

Thank you in advance.

1

There are 1 answers

0
BearDroid On

What I would recommend you to do is to configure your project such that you only have Health Connect specific code in Kotlin. That way you, don't need to refactor your project in its entirety.

  <T extends Record>

"T" here implies the use of Generics.