繁体   English   中英

无法从Google Fit API获取每日步数

[英]Can't get daily step count from Google Fit API

我是Google Fit API的新手。 我正在尝试从API获取每日步数,但没有运气。

我一直在官方文档和Stack Overflow上下翻阅,但似乎Google在2018年对API进行了重大更改,因此大多数事情都已过时,包括Google自己的教程(于2018年发布)。 一些更新了文档的地方提供了代码片段,因此,我不知道如何使用它们。

这是我连接到Google Fit的代码:

    FitnessOptions fitnessOptions = FitnessOptions.builder()
            .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
            .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
            .build();

    if (!GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(this), fitnessOptions)) {
        GoogleSignIn.requestPermissions(
                this, // your activity
                GOOGLE_FIT_PERMISSIONS_REQUEST_CODE,
                GoogleSignIn.getLastSignedInAccount(this),
                fitnessOptions);
    } else {
        accessGoogleFit();
    }

private void accessGoogleFit() {
    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date());
    long endTime = cal.getTimeInMillis();
    cal.add(Calendar.YEAR, -1);
    long startTime = cal.getTimeInMillis();


    DataReadRequest readRequest = new DataReadRequest.Builder()
            .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .bucketByTime(1, TimeUnit.DAYS)
            .build();



    Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
            .readData(readRequest)
            .addOnSuccessListener(new OnSuccessListener<DataReadResponse>() {
                @Override
                public void onSuccess(DataReadResponse dataReadResponse) {
                    Log.d(TAG, "onSuccess()");
                    Toast.makeText(getApplicationContext(),"onSuccess()",Toast.LENGTH_LONG).show();
                    //Toast.makeText(getApplicationContext(),dataReadResponse.getBuckets().get(0).getDataSets()+"",Toast.LENGTH_LONG).show();
                    tvLastRun.setText(dataReadResponse.getBuckets().get(0).getDataSets()+"");

                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.e(TAG, "onFailure()", e);
                    Toast.makeText(getApplicationContext(),"onFailure()",Toast.LENGTH_LONG).show();
                }
            })
            .addOnCompleteListener(new OnCompleteListener<DataReadResponse>() {
                @Override
                public void onComplete(@NonNull Task<DataReadResponse> task) {
                    Log.d(TAG, "onComplete()");
                    Toast.makeText(getApplicationContext(),"onComplete()",Toast.LENGTH_LONG).show();
                }
            });
}
08-27 13:10:39.488 1996-2723/com.google.android.gms.persistent W/InstanceID/Rpc: No response android.os.ConditionVariable@33657c3a
08-27 13:10:39.488 1996-2723/com.google.android.gms.persistent W/GLSUser: [AppCertManager] Failed to get security token.
    java.io.IOException: TIMEOUT
        at vpw.b(:com.google.android.gms@11947270:40)
        at vpw.a(:com.google.android.gms@11947270:47)
        at vpt.b(:com.google.android.gms@11947270:11)
        at vpt.a(:com.google.android.gms@11947270:29)
        at fvt.b(:com.google.android.gms@11947270:4)
        at fvt.a(:com.google.android.gms@11947270:55)
        at fvr.a(:com.google.android.gms@11947270:8)
        at fvn.a(:com.google.android.gms@11947270:1)
        at fvm.a(:com.google.android.gms@11947270:10)
        at com.google.android.gms.auth.account.be.legacy.AuthCronChimeraService.b(:com.google.android.gms@11947270:7)
        at faq.call(:com.google.android.gms@11947270:3)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at mwc.run(:com.google.android.gms@11947270:26)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at nbl.run(:com.google.android.gms@11947270)
        at java.lang.Thread.run(Thread.java:818)
08-27 13:10:39.505 1996-2723/com.google.android.gms.persistent W/ConfigurationChimeraPro: Got null configs for com.google.`enter code here`android.gms.common_auth
08-27 13:10:39.524 1996-2723/com.google.android.gms.persistent I/Auth: [ReflectiveChannelBinder] Successfully bound channel!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM