"Custom" sensor event rates do not seem to work with SensorManager.registerListener (SensorEventListener, Sensor sensor, int rate)

UPDATED:

I managed to solve the specific problem that I encountered by introducing a static counter of class-scope and simply ignoring ever x number of events. But I still would like to know what I'm doing wrong: register the listener with a hint in microseconds instead of using one of the four given constants.


The activity in my application is related to sensors in order to get the orientation of the device, determine the roll and use it.

I use

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

to register my sensors. From the Android documentation for this method:

Parameters

[...]

rate

Speed ​​sensor events are transmitted to. This is just a hint of the system. Events can be received faster or slower than the specified rate. Events usually get 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 microseconds.

If I use one of 4 predefined constants, the application works fine; however, these constants all give speed hints that are too fast for my needs. I have to send a UDP packet containing some information every time the event changes, and the receiving party seems to be completely filled with messages using any of the predefined rates. Using an integer such as 30000 (because the API sets the values ​​in microseconds), the application stops reporting sensor events together.

What am I missing, what prevents me from using my own event hints?

+6
source share
2 answers

I am sure that the listening speed of the sensors did what she was supposed to do. In your question, you wrote 30,000, which is 30 milliseconds. The paper says that speed is usually higher than a hint. This way you are doing faster than 30 ms. Is it possible that your other network-related routines go too fast? This may have caused some blockage, which leads you to believe that the message transfer report has been stopped.

In my application, I also find this rate too high. So I set the speed to 250,000. I also used the moving average calculation to smooth the number by 5. I find the resulting behavior close to the iPhone compass.

However, I would not suggest that you do network reporting in the sensor receiver. It should not be done that way. However, you can do some simple calculations in the listener and write the value. Then use a timer like Handler.postDelayed with a large number to handle network transmission by the way.

0
source

This question was asked in 2011, however, answering it, since much has changed since then; from API 19 (2013+) onwards, there is a new version of the registration API in which you could indicate at what period of time you would like to receive sensor readings. From the docs:

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

This function is similar to the registerListener (SensorEventListener, Sensor, int) function, but it allows events to temporarily remain at the hardware FIFO level (queue) before delivery. Events can be stored in hardware FIFO to the maximum. The maximum latency of microseconds. As soon as one of the events in the FIFO is to be presented, all events in the FIFO are reported sequentially. This means that some events will occur before the maximum latency of reporting.

0
source

Source: https://habr.com/ru/post/887220/


All Articles