Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading weight with Google Fit

I can connect to Google Fit and read weight data but not all the data that I can see in Google Fit Web. I believe that something is wrong with datasources but I'm not sure. The code for reading is:

Instant endTime = Instant.now();
Instant startTime = DateTime.now().minusYears(10).toInstant();

DataReadRequest readRequest = new DataReadRequest.Builder ()
        .setTimeRange (startTime.getMillis(), endTime.getMillis(), TimeUnit.MILLISECONDS)
        .read (DataType.TYPE_WEIGHT)
        .build ();

DataReadResult dataReadResult = Fitness.HistoryApi.readData(MyApp.mClient, readRequest).await (1, TimeUnit.MINUTES);
like image 950
angelfmarcos Avatar asked Nov 08 '22 22:11

angelfmarcos


1 Answers

https://developers.google.com/fit/rest/v1/authorization#OAuth2Authorizing

DataType.TYPE_WEIGHT must be authorized by an authenticated Scopes.FITNESS_BODY_READ

    mClient = new GoogleApiClient.Builder(this)
            .addApi(Fitness.HISTORY_API)
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
            .addScope(new Scope(Scopes.FITNESS_BODY_READ))
            .addScope(new Scope(Scopes.FITNESS_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?
                            // Look at some data!!
                            new InsertAndVerifyDataTask().execute();
                        }

                        @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();
like image 197
miaowBoss Avatar answered Nov 14 '22 22:11

miaowBoss