简体   繁体   中英

Not getting live step counter updates from Google Fit

I want to check whether someone is walking using the Google Fit API by checking whether their step count delta is > 0. It seems simpler than using the History API to get daily step counts and figuring out whether it increased from the previous query.

I followed the code here but instead of location tracking, I do delta step counting. However, the listener doesn't seem to retrieve the latest delta step count.

Here is my code:

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

The onDataPoint method is not called in mListener = onDataPointListener(){...} -> registerFitnessDataListener(...) and I'm not sure why.

Try by implementing this to your class

implements OnDataPointListener

I had the same problem and this fixed it. Let me know if it helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM