Android如何设置传感器延迟?

时间:2022-03-10 20:48:50

For my project i've created a class that uses the Sensors of the users device to detect north. I only need to get an update every second, and hopefully by making the delay longer save some battery, but I cant get the delay working. What i have:

对于我的项目,我创建了一个使用用户设备的传感器来检测北方的类。我只需要每秒钟进行一次更新,并希望通过延迟更长时间来节省一些电池,但我不能让延迟工作。是)我有的:

mAcceleroSensor = mSensorManagerAccelero.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
mMagneticSensor = mSensorManagerMagnetic.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
// Tried to set normal values like SensorManager.SENSOR_DELAY_NORMAL
// and numbers like 100 - 100.000. 1.0000.0000 
// (without the dots only here to make it readable here).
mSensorManagerAccelero.registerListener(this, mAcceleroSensor, 50000000);
mSensorManagerMagnetic.registerListener(this, mMagneticSensor, 50000000);

Everything works except the delay it almost looks like its updated live. I have used code similair to https://*.com/a/23060896/1667868 with some minour changes.

一切正常,除了延迟几乎看起来像它的更新现场。我使用代码similair来https://*.com/a/23060896/1667868进行一些改动。

Note: the minimum api version I use is 14

注意:我使用的最小api版本是14

1 个解决方案

#1


The documentation for SensorManager.registerListener(SensorEventListener listener, Sensor sensor, int samplingPeriodUs):

SensorManager.registerListener(SensorEventListener侦听器,传感器传感器,int samplingPeriodUs)的文档:

The events will be delivered to the provided SensorEventListener as soon as they are available. To reduce the power consumption, applications can use registerListener(SensorEventListener, Sensor, int, int) instead and specify a positive non-zero maximum reporting latency.

事件将在可用时立即传递给提供的SensorEventListener。为了降低功耗,应用程序可以使用registerListener(SensorEventListener,Sensor,int,int),并指定正的非零最大报告延迟。

Whilst the sensor's readings are sampled at the given interval, the reporting interval in that method is not specified causing the callback to be called rapidly as soon as a reading is available. Use the alternative method mentioned in the documentation as such:

虽然传感器的读数以给定的间隔进行采样,但未指定该方法中的报告间隔,从而在读数可用时立即快速调用回调。使用文档中提到的替代方法:

 mSensorManagerAccelero.registerListener (this, mAcceleroSensor, 1000000, 1000000)

to get it to report at about once at second (you can manually adjust the sampling rate as needed). This would also help with power usage.

让它在大约一秒钟报告(您可以根据需要手动调整采样率)。这也有助于用电。

Note the documentation claims the final argument maxReportLatencyUs is the:

请注意文档声称最终参数maxReportLatencyUs是:

Maximum time in microseconds that events can be delayed before being reported to the application.

事件在报告给应用程序之前可以延迟的最长时间(以微秒为单位)。

meaning that it could potentially still report faster than 1 second. If you want user's to see consistent smooth 1 second updates, consider saving results to a averaging buffer and updating the UI once per second.

意味着它可能仍然报告超过1秒。如果您希望用户看到一致的平滑1秒更新,请考虑将结果保存到平均缓冲区并每秒更新一次UI。

#1


The documentation for SensorManager.registerListener(SensorEventListener listener, Sensor sensor, int samplingPeriodUs):

SensorManager.registerListener(SensorEventListener侦听器,传感器传感器,int samplingPeriodUs)的文档:

The events will be delivered to the provided SensorEventListener as soon as they are available. To reduce the power consumption, applications can use registerListener(SensorEventListener, Sensor, int, int) instead and specify a positive non-zero maximum reporting latency.

事件将在可用时立即传递给提供的SensorEventListener。为了降低功耗,应用程序可以使用registerListener(SensorEventListener,Sensor,int,int),并指定正的非零最大报告延迟。

Whilst the sensor's readings are sampled at the given interval, the reporting interval in that method is not specified causing the callback to be called rapidly as soon as a reading is available. Use the alternative method mentioned in the documentation as such:

虽然传感器的读数以给定的间隔进行采样,但未指定该方法中的报告间隔,从而在读数可用时立即快速调用回调。使用文档中提到的替代方法:

 mSensorManagerAccelero.registerListener (this, mAcceleroSensor, 1000000, 1000000)

to get it to report at about once at second (you can manually adjust the sampling rate as needed). This would also help with power usage.

让它在大约一秒钟报告(您可以根据需要手动调整采样率)。这也有助于用电。

Note the documentation claims the final argument maxReportLatencyUs is the:

请注意文档声称最终参数maxReportLatencyUs是:

Maximum time in microseconds that events can be delayed before being reported to the application.

事件在报告给应用程序之前可以延迟的最长时间(以微秒为单位)。

meaning that it could potentially still report faster than 1 second. If you want user's to see consistent smooth 1 second updates, consider saving results to a averaging buffer and updating the UI once per second.

意味着它可能仍然报告超过1秒。如果您希望用户看到一致的平滑1秒更新,请考虑将结果保存到平均缓冲区并每秒更新一次UI。