Android Google适合录制API无效

时间:2016-01-27 09:10:29

标签: android google-fit

我想从我的应用程序中集成google fit记录API。然后我想使用历史记录API读取数据。从我的手机我卸载了谷歌适合的应用程序,以便我可以确保录音api集成工作完美。

请在下面找到我所做的代码。当使用History API检索数据时,我正在为我完成的那些步行获取桶,但数据点将变空。任何人都可以帮助我。

client = new GoogleApiClient.Builder(context)
                .addApi(Fitness.HISTORY_API)
                .addApi(Fitness.RECORDING_API)
                .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
                .addConnectionCallbacks(
                        new GoogleApiClient.ConnectionCallbacks() {
                            @Override
                            public void onConnected(Bundle bundle) {
                                Log.i(GOOGLE_FIT_TAG, "Connected!");
                                subscribe();
                                bus.post(new GoogleFitConnectEvent());
                            }

                            @Override
                            public void onConnectionSuspended(int i) {
                                String errorMessage = "Connection lost. ";
                                if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
                                    errorMessage += "Cause: Network Lost.";
                                    Log.i(GOOGLE_FIT_TAG, errorMessage);
                                } else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
                                    errorMessage = "Reason: Service Disconnected";
                                    Log.i(GOOGLE_FIT_TAG, errorMessage);
                                }

                                bus.post(new GoogleFitConnectionErrorEvent(errorMessage));
                            }
                        }
                )
                .build();

public void subscribe() {
        Fitness.RecordingApi.subscribe(client, DataType.TYPE_ACTIVITY_SAMPLE)
                .setResultCallback(new ResultCallback<Status>() {
                    @Override
                    public void onResult(Status status) {
                        if (status.isSuccess()) {

                        } else {
                            Log.i(GOOGLE_FIT_TAG, "There was a problem subscribing.");
                        }
                    }
                });
    }
//Read data from History API:

Fitness.HistoryApi.readData(client, buildQuery(startTime, endTime))
                .setResultCallback(new ResultCallback<DataReadResult>() {
                    @Override
                    public void onResult(DataReadResult dataReadResult) {
                        Set<Workout> parsedWorkouts = parseWorkoutsFromFitnessData(dataReadResult);
                    }
                }, Constants.GOOGLE_FIT_TIMEOUT, Constants.GOOGLE_FIT_TIMEOUT_TIMEUNIT);


 private DataReadRequest buildQuery(long startTime, long endTime) {
        DataReadRequest readRequest = new DataReadRequest.Builder()

                .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
                             .bucketByActivitySegment(1, TimeUnit.MINUTES)
                .enableServerQueries()
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .build();

        return readRequest;
    }

private Set<Workout> parseWorkoutsFromFitnessData(DataReadResult dataReadResult) {
        List<Workout> workouts = new ArrayList<>();

        for (Bucket bucket : dataReadResult.getBuckets()) {

            for (DataSet dataSet : bucket.getDataSets()) {

                for (DataPoint dp : dataSet.getDataPoints()) {

                    for (Field field : dp.getDataType().getFields()) {
                        Log.i(GOOGLE_FIT_TAG, "\tField: " + field.getName() + " Value: " + dp.getValue(field) + " ");
                    }

                        if (bucket.getActivity().equals(FitnessActivities.RUNNING) || bucket.getActivity().equals(FitnessActivities.WALKING)) {
                            long duration = dp.getEndTime(TimeUnit.MINUTES) - dp.getStartTime(TimeUnit.MINUTES);

                        }

                }
            }
        }

0 个答案:

没有答案