简体   繁体   中英

Request timeout in Google Fit - Android wear 2.0

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();
    }

You may want to check the correct process in inserting data wherein it was discussed that to insert data into the fitness history, first create a DataSet instance before using the HistoryApi.insertData method and wait synchronously or provide a callback method to check the status of the insertion.

For a more detailed information and sample codes, you may want to check the full documentation .

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();
                    [...]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM