2
我想通過檢查他們的步數增量是否大於0來檢查是否有人正在使用Google Fit API。這似乎比使用歷史API獲取每日步數更簡單弄清楚它是否從前面的查詢中增加。未收到來自Google Fit的實時計步器更新
我跟着代碼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.");
}
});
}
的onDataPoint方法不mListener = onDataPointListener(){...} -> registerFitnessDataListener(...)
叫,我不知道爲什麼。通過實施這類
implements OnDataPointListener
你有沒有找到解決辦法?我與我的應用程序有同樣的問題 – appLogic
不,我沒有。我將嘗試克隆我鏈接的示例以查看他們是否獲得實時更新。 –