1

我的应用正在从 Google Fit 读取体重数据。数据是由 Withings 和我自己的应用程序插入的。但这在我调用时没有任何区别dataSet.getDataSource().getAppPackageName(),因为它总是返回com.google.android.gms。所以我没有机会知道数据来自哪里。谷歌在这篇文章中描述了如何获取数据源的信息:https ://developers.google.com/fit/android/data-attribution不幸的是这对我来说完全没用。

我正在使用 Google Play Services 8.3.0,使用 Android 4.3、4.4.2 和 6.0.1 进行了测试。

任何人都可以确认相同的行为吗?还是我做错了什么?任何反馈表示赞赏。

public void connect(final Activity activity) {
    client = new GoogleApiClient.Builder(activity)
        .addApi(Fitness.HISTORY_API)
        .addApi(Fitness.CONFIG_API)
        .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
        .addScope(new Scope(Scopes.FITNESS_BODY_READ_WRITE))
        .build();
    client.connect();
}

public DataReadResult readWeightValues(final Date startDate, final Date endDate) {
    final DataReadRequest readRequest = new DataReadRequest.Builder()
        .enableServerQueries()
        .setTimeRange(startDate.getTime(), endDate.getTime(), TimeUnit.MILLISECONDS)
        .read(DataType.TYPE_WEIGHT)
        .build();

    return Fitness.HistoryApi.readData(client, readRequest).await(1, TimeUnit.MINUTES);
}

public void examineWeightValues(final DataReadResult dataReadResult) {
    if ((dataReadResult != null) && dataReadResult.getStatus().isSuccess()) {
        if (!dataReadResult.getBuckets().isEmpty()) {
            for (final Bucket bucket : dataReadResult.getBuckets()) {
                final List<DataSet> dataSets = bucket.getDataSets();
                for (final DataSet dataSet : dataSets) {
                    Log.i("=====>", dataSet.getDataSource().getAppPackageName());
                }
            }
        }

        if (!dataReadResult.getDataSets().isEmpty()) {
            for (final DataSet dataSet : dataReadResult.getDataSets()) {
                Log.i("=====>", dataSet.getDataSource().getAppPackageName());
            }
        }
    }
}

public Status insertWeightValue(final Context context, final Date date, final float weightKg) {
    final DataSource dataSource = new DataSource.Builder()
        .setAppPackageName(context.getApplicationContext().getPackageName())
        // already tried setAppPackageName(context) too
        .setName("com.mycompany.myapp")
        .setDataType(DataType.TYPE_WEIGHT)
        .setType(DataSource.TYPE_RAW)
        .build();

    final DataSet dataSet = DataSet.create(dataSource);

    final DataPoint dataPoint = dataSet.createDataPoint();
    dataPoint.setTimestamp(date.getTime(), TimeUnit.MILLISECONDS);
    dataPoint.getValue(Field.FIELD_WEIGHT).setFloat(weightKg);
    dataSet.add(dataPoint);

    return Fitness.HistoryApi.insertData(client, dataSet).await(1, TimeUnit.MINUTES);
}
4

2 回答 2

2

DataPoint.getOriginalDataSource().getAppPackageName()会成功的。它返回com.withings.wiscale2到我的 Withings 量表。

注意:昨天晚上有一个答案建议使用此解决方案,但今天早上它消失了。我不知道这里发生了什么,但不幸的是我不知道消失答案的作者。无论如何,我想说谢谢你为我指明了正确的方向!

于 2015-12-22T06:38:50.740 回答
0

那不是正确的方法。我确定您的值未插入 google fit 。您需要在 onClientConnected 回调中完成所有这些插入操作。这是api客户端构建代码

public void buildFitnessClient() {
    fitnessClient = new GoogleApiClient.Builder(context)
            .addApi(Fitness.HISTORY_API)
            .addApi(Fitness.SESSIONS_API)
            .addApi(Fitness.RECORDING_API)
            .addScope(new Scope(Scopes.FITNESS_BODY_READ_WRITE))
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
            .addScope(new Scope(Scopes.FITNESS_LOCATION_READ_WRITE))
            .addScope(new Scope(Scopes.FITNESS_NUTRITION_READ_WRITE))
            .addConnectionCallbacks(
                    new GoogleApiClient.ConnectionCallbacks() {
                        @Override
                        public void onConnected(Bundle bundle) {
                            //Do here your stuff Or call methods from here Otherwise 
                        // You will gety Client not Connected Exception
                        }
                        @Override
                        public void onConnectionSuspended(int i) {
                        }
                    })
            .addOnConnectionFailedListener(
                    new GoogleApiClient.OnConnectionFailedListener() {
                        @Override
                        public void onConnectionFailed(
                                ConnectionResult result) {
                            if (!result.hasResolution()) {
                                GooglePlayServicesUtil.getErrorDialog(
                                        result.getErrorCode(), context, 0)
                                        .show();
                                return;
                            }
                            if (!authInProgress) {
                                try {
                                    authInProgress = true;
                                    result.startResolutionForResult(
                                            context,
                                            KeyConstant.REQUEST_OAUTH);
                                } catch (IntentSender.SendIntentException e) {
                                }
                            }
                        }
                }).build();
}
于 2015-12-21T06:20:02.370 回答