I'm new to Android Wearable and Google Fit. I'm investigating how Google Fit Recording API works by creating a simple Android app with the following code in the Main Activity (these were taken from Google's Basic Recording API example):
@Override
protected void onCreate(Bundle savedInstanceState) {
...
buildFitnessClient();
}
private void buildFitnessClient() {
// Create the Google API Client
mClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.RECORDING_API)
.addScope(Fitness.SCOPE_LOCATION_READ_WRITE)
.addScope(Fitness.SCOPE_ACTIVITY_READ_WRITE)
.addScope(Fitness.SCOPE_BODY_READ_WRITE)
.addConnectionCallbacks(
new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected!!!");
// Now you can make calls to the Fitness APIs. What to do?
// Subscribe to some data sources!
subscribe();
}
@Override
public void onConnectionSuspended(int i) {
// If your connection to the sensor gets lost at some point,
// you'll be able to determine the reason and react to it here.
if (i == ConnectionCallbacks.CAUSE_NETWORK_LOST) {
Log.i(TAG, "Connection lost. Cause: Network Lost.");
} else if (i == ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
Log.i(TAG, "Connection lost. Reason: Service Disconnected");
}
}
}
)
.enableAutoManage(this, 0, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "Google Play services connection failed. Cause: " +
result.toString());
Snackbar.make(
MainActivity.this.findViewById(R.id.main_activity_view),
"Exception while connecting to Google Play services: " +
result.getErrorMessage(),
Snackbar.LENGTH_INDEFINITE).show();
}
})
.build();
}
/**
* Subscribe to an available {@link DataType}. Subscriptions can exist across application
* instances (so data is recorded even after the application closes down). When creating
* a new subscription, it may already exist from a previous invocation of this app. If
* the subscription already exists, the method is a no-op. However, you can check this with
* a special success code.
*/
public void subscribe() {
// To create a subscription, invoke the Recording API. As soon as the subscription is
// active, fitness data will start recording.
// [START subscribe_to_datatype]
Fitness.RecordingApi.subscribe(mClient, DataType.TYPE_STEP_COUNT_DELTA)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.isSuccess()) {
if (status.getStatusCode()
== FitnessStatusCodes.SUCCESS_ALREADY_SUBSCRIBED) {
Log.i(TAG, "Existing subscription for activity detected.");
} else {
Log.i(TAG, "Successfully subscribed!");
}
} else {
Log.i(TAG, "There was a problem subscribing: " + status.getStatusMessage());
}
}
});
// [END subscribe_to_datatype]
}
I wasn't sure how the recording API works. According to Google Fit documentation, the API would automatically take care of the tracking of steps (in this case) and storing the number of steps into the Fitness Store. So what I expected the app to work was starting the app, walking for a while, and when checking my Google Fit account, the number of steps should be increased.
However, this never occurred.
Could you please correct me if my understanding wasn't right, or if it is, please point me to the right direction to get this code working.
Cheers
Update: After more than an hour, my Google Fit data shown that the number of steps increased from 3033 (previously generated by the Fit app, which was then removed to securely testing this app) to 5011. The increasing amount is very confusing because I simulated steps by shaking my phone and certainly I didn't sake it 2000 times! I used Sensor API to display the number of steps in real time and the total of these numbers were only below 200.
Moreover, I manually checked the data every 10-20 mins and I'm sure that it took more than 1 hour for the data to get updated. According to Google Fit's documentation, the storing of data (into Fitness Store) is done automatically in a "battery- efficient manner". It however doesn't mention clearly how that is done, i.e., how frequently.
It would be great if someone can help me with these questions. Any help appreciated!