“自定义"传感器事件速率似乎不适用于SensorManager.registerListener(SensorEventListener侦听器,传感器传感器,整数速率) [英] "Custom" sensor event rates don't seem to work with SensorManager.registerListener(SensorEventListener listener, Sensor sensor, int rate)

查看:391
本文介绍了“自定义"传感器事件速率似乎不适用于SensorManager.registerListener(SensorEventListener侦听器,传感器传感器,整数速率)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过引入类范围的静态计数器,而忽略了x个事件,我能够解决自己遇到的特定问题.但是我仍然想知道我在做错什么:重新注册带有微秒提示的侦听器,而不是使用四个给定常量之一.

I was able to solve the specific problem I was having by introducing a class-scope static counter and just ignoring ever x number of events. But I'd still like to know what I'm doing wrong re: registering the listener with a hint in microseconds instead of using one of the four given constants.

我的应用中的活动"正在使传感器参与操作,以获取设备的方向,确定侧倾并加以利用.

An Activity in my app is engaging the sensors to obtain the orientation of the device, determine the roll, and utilize it.

我正在使用

SensorManager.registerListener(SensorEventListener listener, Sensor sensor, int rate)

注册我的传感器.从此方法的Android文档:

参数

[...]

费率

传递速率传感器事件.这只是对系统的提示.事件接收可能比指定速率更快或更慢.通常事件的接收速度更快.该值必须是SENSOR_DELAY_NORMAL,SENSOR_DELAY_UI,SENSOR_DELAY_GAME或SENSOR_DELAY_FASTEST之一,或者是事件之间的期望延迟(以微秒为单位).

The rate sensor events are delivered at. This is only a hint to the system. Events may be received faster or slower than the specified rate. Usually events are received faster. The value must be one of SENSOR_DELAY_NORMAL, SENSOR_DELAY_UI, SENSOR_DELAY_GAME, or SENSOR_DELAY_FASTEST or, the desired delay between events in microsecond.

如果我使用4个预定义常量之一,则该应用程序可以正常工作;但是这些常量都提供了速率提示,这些提示对于我的需求而言太快了.每当事件发生变化时,我都必须发送一个包含一些信息的UDP数据包,并且接收端似乎已使用任何预定义的速率完全淹没了消息.使用30000这样的整数(因为API以毫秒为单位指定了数量)会导致该应用停止一起报告传感器事件.

If I use one of the 4 predefined constants then the app works fine; however these constants all provide rate hints that are too fast for my needs. I have to send out a UDP packet containing some information with every event change, and the receiving end seems to be getting completely inundated with messages using any of the predefined rates. Using an integer number like 30000 (since the API specifies quantities in microseconds) causes the app to stop reporting sensor events all together.

我缺少什么让我无法使用自己的事件发生率提示?

What am I missing that is preventing me from using my own event rate hints?

推荐答案

这个问题是在2011年提出的,但由于此后发生了许多变化,因此回答了这个问题.从API 19(2013+)开始,注册API有一个新变体,您可以在其中提及您希望以什么间隔接收传感器读数.从文档中:

This question was asked in 2011, nevertheless answering it as a lot has changed since then; from API 19 (2013+) onwards, there is a new variant of the registration API, in which you could mention at what interval you would like to receive the sensor readings. From the docs:

boolean registerListener(SensorEventListener侦听器, 感应器 整数采样周期 int maxReportLatencyUs)在给定采样下为给定传感器注册一个SensorEventListener 频率和给定的最大报告延迟时间.

boolean registerListener (SensorEventListener listener, Sensor sensor, int samplingPeriodUs, int maxReportLatencyUs) Registers a SensorEventListener for the given sensor at the given sampling frequency and the given maximum reporting latency.

此函数类似于registerListener(SensorEventListener, Sensor(int),但它允许事件暂时保留在硬件中 FIFO(队列)在交付之前.这些事件可以存储在 硬件FIFO最高为maxReportLatencyUs微秒.曾经的 FIFO中的事件需要报告,FIFO中的所有事件 依次报告.这意味着一些事件将会 在最大报告延迟时间之前报告.

This function is similar to registerListener(SensorEventListener, Sensor, int) but it allows events to stay temporarily in the hardware FIFO (queue) before being delivered. The events can be stored in the hardware FIFO up to maxReportLatencyUs microseconds. Once one of the events in the FIFO needs to be reported, all of the events in the FIFO are reported sequentially. This means that some events will be reported before the maximum reporting latency has elapsed.

这篇关于“自定义"传感器事件速率似乎不适用于SensorManager.registerListener(SensorEventListener侦听器,传感器传感器,整数速率)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆