android - 使用 Google Fit 读取体重

标签 android google-fit

我可以连接到 Google Fit 并读取体重数据,但不能读取我在 Google Fit Web 中看到的所有数据。我相信数据源有问题,但我不确定。读取代码为:

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

最佳答案

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

DataType.TYPE_WEIGHT 必须由经过身份验证的 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();

关于android - 使用 Google Fit 读取体重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34545779/

相关文章:

node.js - Node : Integrations of Google Fit into my application

android - LibGDX 如何让标签变大?

Android 应用内购买问题

使用适用于 Unity3D 的 Google Fitness API 的 Android 插件

java - Caused by : java. lang.IllegalStateException: 请求聚合时必须指定有效的分桶策略

google-fit - 访问受限数据类型并在开发环境中实现

android - Edittext 的常量后缀,即使在添加字符后也会存在

android - Firestore - 获取子集合的父文档

java - 按下按钮时应用程序崩溃

google-fit - Fit Api 中的距离