'GoogelFit sample app BasicHistoryApiKoltin, work on 2 device properly and on Samsung it gives APIException 5000

I am currently developing Step counter app, in that I am following Google Sample app. Problem is it works properly on my all device except one, On Samsung it gives me API Exception 5000. I have created app on Google Console as well and working fine on other devices. Below is the exception. I don't understand why it not working on specific device while on rest of them it works fine. I am following below sample : https://github.com/android/fit-samples/tree/main/BasicHistoryApiKotlin

enter image description here



Solution 1:[1]

I had similar error on both of my test devices (Xiaomi Android 9 & Samsung Android 12) even after I had setup my OAuth token and scope permissions properly. Then, I noticed that there are 2 issues with the latest code on that fit-sample master/main branch:

1. Application package name mismatch between the one in AndroidManifest.xml & app gradle.
In AndroidManifest.xml, it was declared as "com.google.android.gms.fit.samples.basichistoryapikotlin" & in app gradle as "com.google.android.gms.fit.samples.basichistoryapi".
Solution: commonize this package name & ensure it is the same with the one in your OAuth token settings.

2. Missing read access request in fitnessOptions variable.
In MainActivity.kt, the sample code only declares request to write TYPE_STEP_COUNT_DELTA and AGGREGATE_STEP_COUNT_DELTA data types but not reading them.
Solution:
Update

private val fitnessOptions: FitnessOptions by lazy {
    FitnessOptions.builder()
            .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_WRITE)
            .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_WRITE)
            .build()
}

To

private val fitnessOptions: FitnessOptions by lazy {
    FitnessOptions.builder()
            .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_WRITE)
            .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
            .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_WRITE)
            .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
            .build()
}

After fixing these 2 things, the sample apps works well on both of my test devices. Hope it will work in your case too.

Cheers.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 programmer dreamer