Google Fit : Cannot update both step and distance in real-time

一个人想着一个人 提交于 2019-12-11 04:58:18

问题


I can use sensor api to get step data in real time. I also can get distance data in real time too. However, I can't get step and distance together.

This is my code.

private void registerFitnessDataListener(final DataSource dataSource, final DataType dataType) {
    if (mListener == null)
        mListener = new OnDataPointListener() {
            @Override
            public void onDataPoint(DataPoint dataPoint) {
                isUpdatingStep = false;
                if (!isUpdatingStep) {
                    for (Field field : dataPoint.getDataType().getFields()) {
                        if (String.valueOf(field).equals("steps(i)")) {
                            thisActivity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (!isUpdatingStep) {
                                    }
                                }
                            });
                        } else if (String.valueOf(field).equals("distance(f)")) {
                            thisActivity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (!isUpdatingStep) {
                                    }
                                }
                            });
                        }
                    }
                }
            }
        };
    SensorRequest req = new SensorRequest.Builder()
            .setDataSource(dataSource)
            .setDataType(dataType)
            .setSamplingRate(2000, TimeUnit.MILLISECONDS)
            .build();

    Fitness.SensorsApi.add(
            mClient,
            req,
            mListener)
            .setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    if (status.isSuccess()) {
                    } else {
                    }
                }
            });
}

   private void findFitnessDataSources(final DataType dataType) {
    Fitness.SensorsApi.findDataSources(mClient, new DataSourcesRequest.Builder()
            .setDataTypes(dataType)
            .setDataSourceTypes(DataSource.TYPE_DERIVED)
            .build())
            .setResultCallback(new ResultCallback<DataSourcesResult>() {
                @Override
                public void onResult(DataSourcesResult dataSourcesResult) {
                    for (DataSource dataSource : dataSourcesResult.getDataSources()) {
                        if (dataSource.getDataType().equals(DataType.TYPE_STEP_COUNT_DELTA)) {
                            registerFitnessDataListener(dataSource, DataType.TYPE_STEP_COUNT_DELTA);
                            setDailySteps();
                        } else if (dataSource.getDataType().equals(DataType.TYPE_DISTANCE_DELTA)) {
                            registerFitnessDataListener(dataSource, DataType.TYPE_DISTANCE_DELTA);
                            setDailyDistance();
                        }
                    }
                }
            });
}

    private void buildFitnessClient() {
    if (mClient == null && checkPermissions()) {
        mClient = new GoogleApiClient.Builder(getActivity())
                .addApi(Fitness.SENSORS_API)
                .addApi(Fitness.HISTORY_API)
                .addScope(Fitness.SCOPE_ACTIVITY_READ)
                .addConnectionCallbacks(
                        new GoogleApiClient.ConnectionCallbacks() {
                            @Override
                            public void onConnected(Bundle bundle) {
                                findFitnessDataSources(DataType.TYPE_STEP_COUNT_DELTA);
                                findFitnessDataSources(DataType.TYPE_DISTANCE_DELTA);
                            }

                            @Override
                            public void onConnectionSuspended(int i) {
                                if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
                                } else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
                                }
                            }
                        }
                )
                .enableAutoManage(getActivity(), 0, new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(ConnectionResult result) {
                    }
                })
                .build();
    }
}

Ps. when I comment one of these functions. findFitnessDataSources(DataType.TYPE_STEP_COUNT_DELTA); or findFitnessDataSources(DataType.TYPE_DISTANCE_DELTA);

in method buildFitnessClient().

The app can work properly.(Update only a single value)


回答1:


Changing the sampling rate to be different for both the findFitnessDataSources methods in the calling function might resolve this.



来源:https://stackoverflow.com/questions/38120657/google-fit-cannot-update-both-step-and-distance-in-real-time

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