简体   繁体   English

SensorManager requestTriggerSensor返回false

[英]SensorManager requestTriggerSensor returning false

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. 我正在尝试使用sensorManager.requestTriggerSensor()方法来启动应用程序时获取初始步骤计数。 However, my code returns false, meaning sensor was not triggered. 但是,我的代码返回false,表示未触发传感器。 I have successfully implemented the SensorEventListener , which I use for continuing the step count. 我已经成功实现了SensorEventListener ,用于继续执行步骤计数。 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? 还是在启动应用程序时有其他方法来获取当前传感器数据(例如步数)?

I believe you want to use the Google Fit API for this, rather than accessing the raw sensor data. 我相信您想为此使用Google Fit API,而不是访问原始传感器数据。 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. 请注意,此代码不希望在UI线程上运行。 You can either move it into another thread (as is shown on the page I linked) or use the PendingResult.setResultCallback method. 您可以将其移到另一个线程中(如我链接的页面所示),也可以使用PendingResult.setResultCallback方法。

I have only assumption that triggering the step sensor is false beacause the sensor was already triggered. 我仅假设触发步进传感器是错误的,因为传感器已被触发。 Yet, I propose a different solution. 但是,我提出了另一种解决方案。

  1. You can listen to the Sensor.TYPE_STEP_COUNTER. 您可以收听Sensor.TYPE_STEP_COUNTER。 Then value of steps you receive with first sensor event is your 'bias' so to detect how many steps user made since app launch you would need to substract the 'bias' from the newest sensor value. 然后,您在第一个传感器事件中收到的步骤值就是您的“偏差”,因此要检测自应用启动以来用户执行的步骤数,您需要从最新的传感器值中减去“偏差”。
  2. On the other hand there is another sensor: Sensor.TYPE_STEP_COUNTER which is triggered with every step. 另一方面,还有另一个传感器:Sensor.TYPE_STEP_COUNTER,每个步骤都会触发该传感器。 You also might want to use this one. 您可能还想使用此功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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