简体   繁体   English

在Android中快速读取传感器数据

[英]Read sensor data fast in Android

I am looking for a way to get data of sensors (especially Light Sensor) rapidly, say, the sampling rate at 500Hz or so. 我正在寻找一种快速获取传感器(尤其是光传感器)数据的方法,例如500Hz左右的采样率。 I am totally new in Android. 我是Android的新手。 I have Nexus 5 and Nexus 7 to use. 我有Nexus 5和Nexus 7可供使用。 Is that possible to do so without the access to the driver code? 如果没有访问驱动程序代码,是否可以这样做? Thank you in advance 先感谢您

As in the official documentation, you can specify the frequency in the registerListener. 与官方文档一样,您可以在registerListener中指定频率。

@Override
protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(this, mLight,SensorManager.SENSOR_DELAY_NORMAL);
}

In this example, the default data delay (SENSOR_DELAY_NORMAL) is specified when the registerListener() method is invoked. 在此示例中,当调用registerListener()方法时,将指定默认数据延迟(SENSOR_DELAY_NORMAL)。 The data delay (or sampling rate) controls the interval at which sensor events are sent to your application via the onSensorChanged() callback method. 数据延迟(或采样率)控制通过onSensorChanged()回调方法将传感器事件发送到您的应用程序的时间间隔。 The default data delay is suitable for monitoring typical screen orientation changes and uses a delay of 200,000 microseconds. 默认数据延迟适用于监视典型的屏幕方向变化,并使用200,000微秒的延迟。 You can specify other data delays, such as SENSOR_DELAY_GAME (20,000 microsecond delay), SENSOR_DELAY_UI (60,000 microsecond delay), or SENSOR_DELAY_FASTEST (0 microsecond delay). 您可以指定其他数据延迟,例如SENSOR_DELAY_GAME(延迟20,000微秒),SENSOR_DELAY_UI(延迟60,000微秒)或SENSOR_DELAY_FASTEST(延迟0微秒)。 As of Android 3.0 (API Level 11) you can also specify the delay as an absolute value (in microseconds). 从Android 3.0(API级别11)开始,您还可以将延迟指定为绝对值(以微秒为单位)。

The delay that you specify is only a suggested delay. 您指定的延迟仅是建议的延迟。 The Android system and other applications can alter this delay. Android系统和其他应用程序可以更改此延迟。 As a best practice, you should specify the largest delay that you can because the system typically uses a smaller delay than the one you specify (that is, you should choose the slowest sampling rate that still meets the needs of your application). 最佳做法是,应指定可能的最大延迟,因为系统通常使用的延迟小于指定的延迟(也就是说,应选择仍能满足应用程序需求的最低采样率)。 Using a larger delay imposes a lower load on the processor and therefore uses less power. 使用较大的延迟会给处理器带来较低的负载,因此会消耗较少的功率。

If you want 500Hz, you might want to use the SENSOR_DELAY_FASTEST to check what's the fastest rate you can get. 如果需要500Hz,则可能需要使用SENSOR_DELAY_FASTEST来检查最快的速率。 If it's possible with this, then you can specify the value to 2,000 microsecond delay. 如果可以的话,您可以将值指定为2,000微秒延迟。

There is no public method for determining the rate at which the sensor framework is sending sensor events to your application; 没有公开的方法可以确定传感器框架向您的应用程序发送传感器事件的速率。 however, you can use the timestamps that are associated with each sensor event to calculate the sampling rate over several events. 但是,您可以使用与每个传感器事件关联的时间戳来计算多个事件的采样率。 You should not have to change the sampling rate (delay) once you set it. 设置后,您不必更改采样率(延迟)。 If for some reason you do need to change the delay, you will have to unregister and reregister the sensor listener. 如果由于某些原因您确实需要更改延迟,则必须注销并重新注册传感器侦听器。

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

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