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

断了今生、忘了曾经 提交于 2019-12-11 09:59:28

问题


My app is reading weight data from Google Fit. The data were inserted by Withings and by my own app. But this doesn't make any difference when I call dataSet.getDataSource().getAppPackageName(), because it always returns com.google.android.gms. So I have no chance of knowing where the data came from. Google describes how to get information of the data source in this article: https://developers.google.com/fit/android/data-attribution Unfortunately this is completely useless to me.

I'm using Google Play Services 8.3.0, tested using Android 4.3, 4.4.2 and 6.0.1.

Can anyone confirm the same behavior? Or am I doing something wrong? Any feedback is appreciated.

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

回答1:


DataPoint.getOriginalDataSource().getAppPackageName() will do the trick. It returns com.withings.wiscale2 for my Withings scale.

Note: There was an answer that suggested this solution yesterday evening, but this morning it's gone. I don't know what happened here, but unfortunately I have no idea of the author of the disappeared answer. Anyway, I want to say thank you for pointing me in the right direction!




回答2:


Thats nopt the right way to do it . I am sure your values are not inserted in google fit . You need to do all this inserting inside onClientConnected callback . Here is api Client build code

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


来源:https://stackoverflow.com/questions/34330471/datasource-getapppackagename-always-returns-com-google-android-gms-in-google

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!