I have a problem with getting google fit data on Android Wear 2.0. My requests are getting TIMEOUT response. If the await() method has no parameters, there is no response (the await() method did not returned). Any clues whats wrong?
App uses Google Sign-In, and everything works on regular Android device.
Creating of GoogleApiClient and SignInAccount
GoogleSignInOptions signInConfig = new GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestScopes(new Scope(Scopes.FITNESS_LOCATION_READ),new Scope(Scopes.FITNESS_ACTIVITY_READ))
.build();
client = new GoogleApiClient.Builder(this)
.enableAutoManage(this,this)
.addApi(Auth.GOOGLE_SIGN_IN_API, signInConfig)
.addApi(Fitness.HISTORY_API)
.addApi(Fitness.GOALS_API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
After login procedure is done I run:
new Thread(new Runnable() {
@Override
public void run() {
PendingResult<DailyTotalResult> result =
Fitness.HistoryApi.readDailyTotal(client, TYPE_STEP_COUNT_DELTA);
DailyTotalResult totalResult = result.await(60,TimeUnit.SECONDS);
if (totalResult.getStatus().isSuccess()) {
DataSet totalSet = totalResult.getTotal();
long total = totalSet.isEmpty()? 0 : totalSet.getDataPoints().get(0).getValue(FIELD_STEPS).asInt();
p("daily steps " + total);
}}).start();
}
Similar problem was asked here at G+ GoogleFitDevelopersGroup. Thanks to PujieWear we managed to solve the problem. You have to use two different GoogleApiClient's, one to get authenticated, second to get the data. I'm no sure wheather this is proper way of using Google Sign-In, but it works. //Yet, it seems that the Scopes are not yet resolved properly on Wear 2.0.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult signInResult = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (signInResult.isSuccess()) {
acct = signInResult.getSignInAccount();
editor.putString(ACCOUNT_NAME_PREFS,acct.getEmail());
editor.commit();
dataGoogleApiClientBuilder.setAccountName(acct.getEmail());
dataGoogleApiClient = dataGoogleApiClientBuilder.build();
dataGoogleApiClient.connect();
[...]