AndroidX icon indicating copy to clipboard operation
AndroidX copied to clipboard

HealthConnect Records do not implement IRecord

Open adam4fx opened this issue 7 months ago • 8 comments

Android framework version

net9.0-android

Affected platform version

Visual Studio 17.13.6, .NET 9

Description

I am getting an error when try to insert ExerciseSessionRecord using HealthConnectClient.InsertRecords() method, which requires an IList<IRecord>. I can create an ExerciseSessionRecord fine, but when adding it as a parameter to InsertRecords, it becomes red underlined with the following error:

CS0029: Cannot implicitly convert type 'AndroidX.Health.Connect.Client.Records.ExerciseSessionRecord' to 'AndroidX.Health.Connect.Client.Records.IRecord'

Image

Steps to Reproduce

Add the Xamarin.AndroidX.Health.Connect.ConnectClient library to a Maui project.

Insert the code anywhere, as you can't run the app due to the error. var client =HealthConnectClient.GetOrCreate(Platform.AppContext); var start = Java.Time.Instant.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); var end = Java.Time.Instant.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); var offset = Java.Time.ZoneOffset.From(start); var exerciseSession = new AndroidX.Health.Connect.Client.Records.ExerciseSessionRecord(start, offset, end, offset, 1); client.InsertRecords([exerciseSession], null);

Did you find any workaround?

No

Relevant log output

No relevant logs

adam4fx avatar Jul 15 '25 15:07 adam4fx

Can you either share an example project, or the version of the Xamarin.AndroidX.Health package you are using?

jonathanpeppers avatar Jul 15 '25 16:07 jonathanpeppers

The version of Xamarin.AndroidX.Health is 1.1.0-alpha07.

I created a new repo here, its just a new MAUI project, with the code in the MainActivity. I suspect ExerciseSessionRecord, and the other Records (e.g. HeartRateRecord, SpeedRecord) are possibly supposed to implement IRecord. The original android docs show ExerciseSessionRecord implements Record.

https://github.com/adam4fx/MauiHealthConnectError/tree/master

adam4fx avatar Jul 16 '25 08:07 adam4fx

We normally don't even bind prerelease packages, and this one is alpha. Maybe @moljac knows some history about it.

Latest release as of now (if this is the right package):

  • https://mvnrepository.com/artifact/androidx.health.connect/connect-client/1.1.0-rc02

Can I ask what feature you're using in your app? What does the package provide that you need?

I think this is something we should investigate if the package becomes stable. For now, I would be happy to help if you want to contribute with a PR.

We could also assign copilot and just see if it comes up with anything?

jonathanpeppers avatar Jul 16 '25 15:07 jonathanpeppers

You might also be able to use JavaCast<T>() to AndroidX.Health.Connect.Client.Records.IRecord for now, such as mentioned here:

  • https://github.com/dotnet/android-libraries/issues/1188#issuecomment-3077764563

jonathanpeppers avatar Jul 16 '25 15:07 jonathanpeppers

I am developing an app that connects to fitness equipment (treadmill, bike etc.) and we're looking to send the workout data to Google Fit, but those APIs say not to use them as its being replaced with Health Connect and to implement that instead.

I have never contributed with a PR before, so I wouldn't even know where to start.

I will give the JavaCast<T>() a try.

adam4fx avatar Jul 16 '25 15:07 adam4fx

I have a problem with some methods requiring a IContinuous parameter.

HealthConnectClient.PermissionController.GetGrantedPermissions(IContinuous p0)

and the

HealthConnectClient.InsertReceord(IList<IRecord>, IContinuous p1)

Do you know off the top of your head how I may be able to deal with these, or will I need to wait to a later date to implement this?

Thanks for your help so far.

adam4fx avatar Jul 17 '25 13:07 adam4fx

I would try to find a Java/Kotlin example of what you're trying to do and then adopt it to C#. That is the approach I've taken since Xamarin days.

Alternatively, you can use the "slim binding" idea and write a small Java/Kotlin project with a simple interface for C# to call into:

  • https://devblogs.microsoft.com/dotnet/native-library-interop-dotnet-maui/

Some of that article is out of date, but there is an AndroidGradleProject item group that is directly in the product now.

If there are missing C# APIs in the bindings you need, let us know here, and we'll working on addressing issues with upvotes and activity. Thanks!

jonathanpeppers avatar Jul 17 '25 16:07 jonathanpeppers

@adam4fx This project will help you~ https://github.com/EagleDelux/androidx.health-connect-demo-.net-maui

maonaoda avatar Jul 18 '25 08:07 maonaoda