Reputation: 73
I am making a fitness app that uses step count to measure distance. I am trying to use the sensorManager.requestTriggerSensor()
method to get the initial step count when the app is started. However, my code returns false, meaning sensor was not triggered. I have successfully implemented the SensorEventListener
, which I use for continuing the step count. My code snippet is as follows, similar to the documentation
private SensorManager mSensorManager;
private Sensor sensorStepCounter;
private TriggerEventListener triggerEventListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
triggerEventListener = new TriggerEventListener() {
@Override
public void onTrigger(TriggerEvent event) {
float initialSteps = event.values[0];
Log.d(TAG, "initial Steps: " + Arrays.toString(event.values));
}
};
sensorStepCounter = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
....
}
@Override
protected void onResume() {
...
boolean triggered = mSensorManager.requestTriggerSensor(triggerEventListener, sensorStepCounter);
}
Or is there a different way to get the current sensor data, such as step count, when app is started?
Upvotes: 1
Views: 718
Reputation: 429
I have only assumption that triggering the step sensor is false beacause the sensor was already triggered. Yet, I propose a different solution.
Upvotes: 0
Reputation: 6635
I believe you want to use the Google Fit API for this, rather than accessing the raw sensor data. From the documentation:
long total = 0;
PendingResult<DailyTotalResult> result = Fitness.HistoryApi.readDailyTotal(mClient, DataType.TYPE_STEP_COUNT_DELTA);
DailyTotalResult totalResult = result.await(30, TimeUnit.SECONDS);
if (totalResult.getStatus().isSuccess()) {
DataSet totalSet = totalResult.getTotal();
total = totalSet.isEmpty()
? 0
: totalSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt();
} else {
Log.w(TAG, "There was a problem getting the step count.");
}
Note that this code doesn't want to be run on the UI thread. You can either move it into another thread (as is shown on the page I linked) or use the PendingResult.setResultCallback
method.
Upvotes: 1