没有获得Google健身的实时计步器更新

时间:2016-04-23 03:58:30

标签: android google-fit google-fit-sdk

我想检查某人是否正在使用Google Fit API行走,检查他们的步数delta是否为> 0.使用历史API来获取每日步数并确定它是否比上一次查询增加更为简单。

我遵循了代码here,但我没有进行位置跟踪,而是执行delta步计数。但是,听众似乎无法检索最新的增量步数。

这是我的代码:

public void buildFitnessClient()
{
    if ( mClient == null && checkPermissions() )
    {
        mClient = new GoogleApiClient.Builder(this)
                .addApi(Fitness.SENSORS_API)
                .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
                .addConnectionCallbacks(
                        new GoogleApiClient.ConnectionCallbacks()
                        {
                            @Override
                            public void onConnected(Bundle bundle)
                            {
                                Log.d(TAG, "Connected!!!");
                                // Now you can make calls to the Fitness APIs.
                                findFitnessDataSources();
                            }

                            @Override
                            public void onConnectionSuspended(int i)
                            {
                                // If your connection to the sensor gets lost at some point,
                                // you'll be able to determine the reason and react to it here.
                                if ( i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST )
                                    Log.d(TAG, "Connection lost.  Cause: Network Lost.");
                                else if ( i
                                        == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED )
                                    Log.d(TAG, "Connection lost.  Reason: Service Disconnected");
                            }
                        }
                )
                .enableAutoManage(this, 0, new GoogleApiClient.OnConnectionFailedListener()
                {
                    @Override
                    public void onConnectionFailed(ConnectionResult result)
                    {
                        Log.d(TAG, "Google Play services connection failed. Cause: " +
                                result.toString());
                    }
                })
                .build();
    }
}

private void findFitnessDataSources()
{
    // Note: Fitness.SensorsApi.findDataSources() requires the ACCESS_FINE_LOCATION permission.
    Fitness.SensorsApi.findDataSources(mClient, new DataSourcesRequest.Builder()
            // At least one datatype must be specified.
            .setDataTypes(DataType.TYPE_STEP_COUNT_DELTA)
            // Can specify whether data type is raw or derived.
            .setDataSourceTypes(DataSource.TYPE_DERIVED)
            .build())
            .setResultCallback(new ResultCallback<DataSourcesResult>()
            {
                @Override
                public void onResult(DataSourcesResult dataSourcesResult)
                {
                    Log.d(TAG, "Result: " + dataSourcesResult.getStatus().toString());
                    for ( DataSource dataSource : dataSourcesResult.getDataSources() )
                    {
                        Log.d(TAG, "Data source found: " + dataSource.toString());
                        Log.d(TAG, "Data Source type: " + dataSource.getDataType().getName());

                        //Let's register a listener to receive Activity data!
                        if ( dataSource.getDataType().equals(DataType.TYPE_STEP_COUNT_DELTA)
                                && mListener == null )
                        {
                            Log.d(TAG, "Data source for STEP_COUNT_DELTA found! Registering.");
                            registerFitnessDataListener(dataSource,
                                    DataType.TYPE_STEP_COUNT_DELTA);
                        }
                    }
                }
            });
}

private void registerFitnessDataListener(DataSource dataSource, DataType typeStep)
{
    mListener = new OnDataPointListener()
    {
        @Override
        public void onDataPoint(DataPoint dataPoint)
        {
            Log.d(TAG, "Detected onDataPoint");
            for ( Field field : dataPoint.getDataType().getFields() )
            {
                Value val = dataPoint.getValue(field);
                Log.d(TAG, "Detected DataPoint field: " + field.getName());
                Log.d(TAG, "Detected DataPoint value stepCount: " + val);
                TransientValues.setStepCountDelta(val.asInt());
            }
        }
    };


    Fitness.SensorsApi.add(
            mClient,
            new SensorRequest.Builder()
                    .setDataSource(dataSource) // Optional but recommended for custom data sets.
                    .setDataType(typeStep) // Can't be omitted.
                    .setSamplingRate(1L, TimeUnit.SECONDS)
                    .build(),
            mListener)
            .setResultCallback(new ResultCallback<Status>()
            {
                @Override
                public void onResult(Status status)
                {
                    if ( status.isSuccess() ) Log.d(TAG, "Listener registered!");
                    else Log.d(TAG, "Listener not registered.");
                }
            });
}

mListener = onDataPointListener(){...} -> registerFitnessDataListener(...)未调用onDataPoint方法,我不确定原因。

1 个答案:

答案 0 :(得分:0)

尝试将此功能实施到您的班级

implements OnDataPointListener

我遇到了同样的问题并修复了它。如果有帮助,请告诉我。