Google Fit API:ApiException 17(Fitness.CLIENT 在此设备上不可用)



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

尝试在 Android Wear 手表上使用 Fit API。我有一个处理身份验证的主活动,以及一个尝试访问 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'
//...
}

安卓清单:

//...
<application ...>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
//...

主活动片段:

//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;
}
}
}

服务片段(从异步任务中运行):

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<Void>() {
@Override
public void onComplete(@NonNull Task<Void> 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();
}
}

相关内容

  • 没有找到相关文章

最新更新