DataSource.getAppPackageName() 总是 returns "com.google.android.gms" 在 Google 适合

DataSource.getAppPackageName() always returns "com.google.android.gms" in Google Fit

我的应用正在从 Google Fit 读取体重数据。数据是由 Withings 和我自己的应用程序插入的。但是当我调用 dataSet.getDataSource().getAppPackageName() 时这没有任何区别,因为它总是 returns com.google.android.gms。所以我没有机会知道数据来自哪里。 Google在这篇文章中描述了如何获取数据源的信息: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);
}

这才是正确的做法。我确定您的值未插入 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();
}

DataPoint.getOriginalDataSource().getAppPackageName() 就可以了。它 returns com.withings.wiscale2 适合我的 Withings 电子秤。

注意:昨天晚上有一个答案建议这个解决方案,但是今天早上就没有了。我不知道这里发生了什么,但不幸的是我不知道消失的答案的作者。不管怎样,我想说谢谢你给我指明了正确的方向!