0

I'm trying to develop a Watchface in Kotlin and I'm facing some difficulties in obtaining the number of steps and other data. I'm really a beginner in this subject and in Kotlin, so please be kind c:

I'm trying to retrieve this data from the Google Fit API, but I'm encountering an error when I call getHistoryClient, as shown in the code below:

class GoogleFitHelper(private val context: Context) {

fun getStepCount(callback: (Int) -> Unit) {

    val account = GoogleSignIn.getAccountForExtension(context, fitnessOptions)

    val endTime = System.currentTimeMillis()
    val startTime = endTime - TimeUnit.DAYS.toMillis(1) // Obtém dados dos últimos 24 horas

    val readRequest = DataReadRequest.Builder()
        .read(DataType.TYPE_STEP_COUNT_DELTA)
        .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
        .build()

    Fitness.getHistoryClient(context, account)
        .readData(readRequest)
        .addOnSuccessListener { response ->
            val stepCount = processStepCountData(response)
            callback.invoke(stepCount)

        }
        .addOnFailureListener { exception ->
            Log.d("step_fit","$exception")
        }

}

The raised error is: com.google.android.gms.common.api.ApiException: 4: The user must be signed in to make this API call.

I have registered the app in Firebase and Google API, and I believe I have done everything correctly. During my research, I found that the error may be due to not requesting permissions in my code. I discovered that I need to start an Activity to request the permissions, but as I understand, the CanvasWatchFaceService() is a Service, not an Activity.

So, I added the permissions in the AndroidManifest.xml and created the following class:

class PermissionRequestActivity : Activity() {
private var mPermissions: Array<String>? = null
private var mRequestCode: Int = 0

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    mPermissions = arrayOf(arrayOf(intent.getStringExtra("KEY_PERMISSIONS")).toString())
    mRequestCode = intent.getIntExtra("KEY_REQUEST_CODE", PERMISSIONS_CODE)

    ActivityCompat.requestPermissions(this, mPermissions!!, mRequestCode)
}

override fun onRequestPermissionsResult(
    requestCode: Int,
    permissions: Array<String>,
    grantResults: IntArray
) {
    if (requestCode == mRequestCode) {
        for (i in permissions.indices) {
            val permission = permissions[i]
            val grantResult = grantResults[i]
            val result =
                if (grantResult == PackageManager.PERMISSION_GRANTED) "granted" else "revoked"
            Log.d("PermissionRequestActivity", "$permission $result")
        }
    }
    finish()
}

companion object {
    private const val PERMISSIONS_CODE = 0
}

}

I'm trying to call it from the onCreate() method in the CanvasWatchFaceService() using the code below, but it throws an unknown error when I run it:

val myIntent = Intent(this@VpetWatchFace, PermissionRequestActivity::class.java) myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) myIntent.putExtra("KEY_PERMISSIONS", Manifest.permission.BODY_SENSORS) startActivity(myIntent)

I confess that I have been lost for a while in what I'm doing. I don't know if I actually have a permissions issue or if the API call error occurs for some other reason. Has anyone here ever done something like this and would know how to guide me on how to obtain the step count? Am I on the right track?

2
  • Have you considered using the Steps Complication Data Provider for the device instead of getting steps from Google Fit? Commented Jul 7, 2023 at 18:57
  • 1
    is this possible to obtain the data from the complication? i'm also looking for a solution... Commented Sep 25, 2023 at 20:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.