Google Fit API是否仅在本地读取数据?

时间:2018-03-06 04:43:42

标签: android google-fit

我有两部使用Gmail的手机: 电话1:安装Google健身应用并将数据同步到网络[https://fit.google.com][1]

手机2:没有安装google fit app,我使用Google Fit Api读取步骤数据。 但它无法返回结果。

我的代码:

Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
                .readDailyTotal(DataType.TYPE_STEP_COUNT_DELTA)
                .addOnSuccessListener { dataSet ->
                    val total = (if (dataSet.isEmpty)
                        0
                    else
                        dataSet.dataPoints[0].getValue(Field.FIELD_STEPS).asInt()).toLong()
                    Log.i(TAG, "Total steps: " + total)
                    txtStep.setText(total.toString())
                }
                .addOnFailureListener(
                        object : OnFailureListener {
                            override fun onFailure(e: Exception) {
                                Log.w(TAG, "There was a problem getting the step count.", e)
                            }
                        })

我必须在手机2上安装google fit app吗? Google Fit API是否仅在本地读取数据?

我尝试过使用。enableServerQueries()但它仍然无法返回数据。

val endTime = cal.timeInMillis
        cal.add(Calendar.DAY_OF_YEAR, -1)
        cal.set(Calendar.HOUR_OF_DAY, 23)
        val startTime = cal.timeInMillis
        var readRequest = DataReadRequest.Builder()
                .read(DataType.TYPE_STEP_COUNT_DELTA)
                .enableServerQueries()
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .build()
        Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
                .readData(readRequest)
                .addOnSuccessListener { dataReadResult ->
                    var total =0
                    if (dataReadResult.buckets.size > 0) {
                            for (bucket in dataReadResult.buckets) {
                            val dataSets = bucket.dataSets
                            for (dataSet in dataSets) {
                                for (dp in dataSet.dataPoints) {
                                    for (field in dp.dataType.fields) {
                                        total=total + dp.getValue(field).asInt()
                                    }
                                }
                            }
                        }
                    } else if (dataReadResult.dataSets.size > 0) {

                        for (dataSet in dataReadResult.dataSets) {
                            for (dp in dataSet.dataPoints) {
                                for (field in dp.dataType.fields) {
                                    total=total + dp.getValue(field).asInt()
                                }
                            }
                        }
                    }
                    txtStep.setText(total.toString())
                }
                .addOnFailureListener(
                        object : OnFailureListener {
                            override fun onFailure(e: Exception) {
                                Log.w(TAG, "There was a problem getting the step count.", e)
                            }
                        })

我只能在sync Google Fit Data

点击Setting时才能读取数据

1 个答案:

答案 0 :(得分:0)

如果您不想安装Google Fit应用,可以使用 getRecordingClient 订阅健身数据。

您可以在应用程序中为不同的数据类型或数据源创建多个订阅。即使您的应用未运行,Google Fit也会从活动的订阅中存储健身数据,并在系统重启时恢复这些订阅。

记录的数据可在用户的健康历史记录中找到。如果您还想在应用程序中实时显示数据,则需要同时使用Sensors API和Recording API。要将健身数据与会话元数据一起记录,可以使用Sessions API。

订阅健身数据

ifeq ($(OS),Windows_NT)
  include Makefilewin
else
  include MakefileLinux
endif

DataReadRequest for Heart Rate

Fitness.getRecordingClient(this, GoogleSignIn.getLastSignedInAccount(this))
            .subscribe(DataType.TYPE_ACTIVITY_SEGMENT)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Log.i(TAG, "Successfully subscribed!");
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.i(TAG, "There was a problem subscribing.");
                }
            });

从历史记录中检索数据

readRequest = new DataReadRequest.Builder()
                .read(DataType.TYPE_HEART_RATE_BPM)
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .bucketByTime(365, TimeUnit.DAYS)
                .build();

显示数据

Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
            .readData(readRequest)
            .addOnSuccessListener(new OnSuccessListener<DataReadResponse>() {
                @Override
                public void onSuccess(DataReadResponse dataReadResponse) {
                    Log.d(TAG, "onSuccess()");

                    for (Bucket bucket : dataReadResponse.getBuckets()) {
                        Log.e("History", "Data returned for Data type: " + bucket.getDataSets());

                        List<DataSet> dataSets = bucket.getDataSets();
                        for (DataSet dataSet : dataSets) {
                            displayDataSet(dataSet);
                        }
                    }
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.e(TAG, "onFailure()", e);
                }
            })
            .addOnCompleteListener(new OnCompleteListener<DataReadResponse>() {
                @Override
                public void onComplete(@NonNull Task<DataReadResponse> task) {
                    Log.d(TAG, "onComplete()");
                }
            });