1

私のアプリは Google Fit から体重データを読み取っています。データは Withings と私自身のアプリによって挿入されました。dataSet.getDataSource().getAppPackageName()しかし、これは常に を返すため、を呼び出しても違いはありませんcom.google.android.gms。したがって、データがどこから来たのかを知る機会はありません。Google は、この記事でデータ ソースの情報を取得する方法について説明しています: https://developers.google.com/fit/android/data-attribution残念ながら、これは私にはまったく役に立ちません。

Android 4.3、4.4.2、および 6.0.1 を使用してテストされた Google Play Services 8.3.0 を使用しています。

誰でも同じ動作を確認できますか? それとも私は何か間違ったことをしていますか?フィードバックをお待ちしております。

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

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 に答える