Google Fit API:ApiException 17(此设备无法使用Fitness.CLIENT)

Google Play文档声称这是一个API_NOT_CONNECTED代码 ,但我已经完成了(我认为我)必须要做的所有事情才能访问Google Fit API。

尝试在Android Wear手表上使用Fit API。 我有一个主要活动,它处理Auth,以及一个试图访问API的服务。

我在Google API控制台中添加了相关的调试密钥并启用了Google Fit – 我调用了成功的权限回调 – 暗示这不是激活的权限问题

的build.gradle:

dependencies { //... compile 'com.google.android.gms:play-services-fitness:11.8.0' compile 'com.google.android.gms:play-services-auth:11.8.0' //... } 

Android清单:

 //...   //... 

MainActivity代码段:

 //This method is called if the user chooses to enable Google Fit private void GFitRegister(){ FitnessOptions options = FitnessOptions.builder() .addDataType(DataType.TYPE_HEART_RATE_BPM, FitnessOptions.ACCESS_WRITE) .addDataType(DataType.TYPE_ACTIVITY_SAMPLES, FitnessOptions.ACCESS_WRITE) .addDataType(DataType.TYPE_ACTIVITY_SEGMENT, FitnessOptions.ACCESS_WRITE) .build(); if (!GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(mService), options)) { GoogleSignIn.requestPermissions( this, // your activity GOOGLE_FIT_PERMISSIONS_REQUEST_CODE, GoogleSignIn.getLastSignedInAccount(mService), options); } else { accessGoogleFit(); //Sets some fields to true } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { if (requestCode == GOOGLE_FIT_PERMISSIONS_REQUEST_CODE) { accessGoogleFit(); //mUseFit = true; } } } 

服务片段(从asynctask中运行):

 if(mPreferences.getBoolean("googlefit_use", false)){ //Insert heart rate data if it exists if(data.hasHRData()) { DataSource source = new DataSource.Builder() .setAppPackageName(TrackerService.this) .setDataType(DataType.TYPE_HEART_RATE_BPM) .setStreamName(TAG + " - heart rate") .setType(DataSource.TYPE_RAW) .build(); DataSet FitData = DataSet.create(source); for(int i = 1; i < data.getDataLength(); i++){ long time = (long) data.getTimeAt(i-1); double current = data.getHRAt(i); com.google.android.gms.fitness.data.DataPoint d = FitData.createDataPoint() .setTimeInterval(time + 1, time + SleepData.STEPMILLIS, TimeUnit.MILLISECONDS); d.getValue(Field.FIELD_BPM).setFloat((float) current); FitData.add(d); } Task response = Fitness.getHistoryClient(TrackerService.this, GoogleSignIn.getLastSignedInAccount(TrackerService.this)) .insertData(FitData).addOnCompleteListener(new OnCompleteListener() { @Override public void onComplete(@NonNull Task task) { if(task.isSuccessful()) { Log.d(TAG, "HR data added to google fit"); }else{ /* This is logged, with the error in the title */ Log.d(TAG, "HR data unsucessful: ", task.getException()); } } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.d(TAG, "Error: "+e.getMessage()); } }); } } 

我为糟糕的格式道歉,是否有办法在SO中正确编辑代码?

由注释行打印的例外:

  com.google.android.gms.common.api.ApiException: 17: API: Fitness.CLIENT is not available on this device. at com.google.android.gms.common.internal.zzb.zzy(Unknown Source) at com.google.android.gms.common.internal.zzbk.zzz(Unknown Source) at com.google.android.gms.common.internal.zzbl.zzr(Unknown Source) at com.google.android.gms.common.api.internal.BasePendingResult.zzc(Unknown Source) at com.google.android.gms.common.api.internal.BasePendingResult.setResult(Unknown Source) at com.google.android.gms.common.api.internal.zzm.zzu(Unknown Source) at com.google.android.gms.common.api.internal.zzc.zzs(Unknown Source) at com.google.android.gms.common.api.internal.zzbo.zzw(Unknown Source) at com.google.android.gms.common.api.internal.zzbo.onConnectionFailed(Unknown Source) at com.google.android.gms.common.internal.zzad.onConnectionFailed(Unknown Source) at com.google.android.gms.common.internal.zzn.zzj(Unknown Source) at com.google.android.gms.common.internal.zze.zzw(Unknown Source) at com.google.android.gms.common.internal.zzi.zzaks(Unknown Source) at com.google.android.gms.common.internal.zzh.handleMessage(Unknown Source) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.os.HandlerThread.run(HandlerThread.java:61) 

您使用的是最新版本的Google Play Services吗?

您可以通过调用下一个代码来检查它:

 private void checkForUpdates() { GoogleApiAvailability availability = GoogleApiAvailability.getInstance(); int resultCode = availability.isGooglePlayServicesAvailable(this); if (resultCode == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED) { availability.getErrorDialog(this, resultCode, REQUEST_CODE).show(); } }