I have a few doubts on Google Fit STEP count API in particular.
The questions are :
1) Can google fit API be use in myApp if the phone did not have Google Fit app installed.
2) When using the Recording API of Google Fit, should SENSOR DATA or only the STEP_DELTA data should be recorded? As i noticed that, Google Fit has different step count to other fitness app. Example in google fit app it has only 20 steps, however on other fitness app i have 2561 steps.
3) For days when step count is zero, googleFit does not retrieve data of those day, how can i solve this?
My apologies this is not code based.
Code to subscribe to google fit RecordingAPI
public void subscribe() {
mClient = new GoogleApiClient.Builder(mContext)
.addApi(Fitness.RECORDING_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
.useDefaultAccount()
.build();
//to request background collection of data
Fitness.RecordingApi.subscribe(mClient, DataType.TYPE_STEP_COUNT_DELTA);
Fitness.getRecordingClient(mContext, GoogleSignIn.getLastSignedInAccount(mContext))
.subscribe(DataType.TYPE_ACTIVITY_SAMPLES)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.i("TAG1", "Successfully subscribed");
accessGoogleFit();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i("TAG1", "There was a problem subscripting");
}
});
}
The data is being retrieve through the HistoricalAPI
private void accessGoogleFit() {
mClient = new GoogleApiClient.Builder(mContext)
.addApi(Fitness.HISTORY_API)
.addApi(Fitness.CONFIG_API)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(@Nullable Bundle bundle) {
//fetch data
new FetchStepsAsync().execute();
}
@Override
public void onConnectionSuspended(int i) {
if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
Log.i("TAG1", "Connection lost,Cause: network lost");
} else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
Log.i("TAG1", "Connection lost, service disconnected");
}
}
}).addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult result) {
Log.i("TAG1", "Connection failed. Cause:" + result.toString());
if (!result.hasResolution()) {
//show the localized error dialog
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), getActivity(), 0).show();
return;
}
//the failure has a resolution
if (!authInProcess) {
try {
Log.i("TAG1", "Attempting to resolve failed connection");
authInProcess = true;
result.startResolutionForResult(getActivity(), GOOGLE_FIT_PERMISSIONS_REQUEST_CODE);
} catch (IntentSender.SendIntentException e) {
Log.e("TAG1", "Exception while starting resolution activity", e);
}
}
}
})
.build();
mClient.connect();
}
private class FetchStepsAsync extends AsyncTask<Object, Object, Long> {
protected Long doInBackground(Object... params) {
long total = 0;
PendingResult<DailyTotalResult> result = Fitness.HistoryApi.readDailyTotal(mClient, DataType.TYPE_STEP_COUNT_DELTA);
DailyTotalResult totalResult = result.await(30, TimeUnit.SECONDS);
if (totalResult.getStatus().isSuccess()) {
DataSet totalSet = totalResult.getTotal();
if (totalSet != null) {
total = totalSet.isEmpty() ? 0 : totalSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt();
}
} else {
Log.w("TAG1", "There was a problem getting the step count.");
}
return total;
}
@Override
protected void onPostExecute(Long aLong) {
super.onPostExecute(aLong);
//Total steps
Log.i("TAG1", "Total steps:" + aLong);
edSteps.setText(aLong.toString());
}
}