Open In App

Understanding Sensor Rate Limitations in Android 13

Last Updated : 26 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sensors are an aromatic part of your Android application, they can be useful for all purposes, whether your app is a gaming app, a compass, or even a simple directional utility app. However, Android 13 introduced some new Sensor Rate Limitations, so it’s really vital to understand how these affect your application so that you are ready for the newly implemented changes, and your app behaves the intended way.

In this article, we will be discussing the Sensor Rate Limitations which are introduced in Android 13. If your app targets Android 13 (API level 32) or higher, the system limits the refresh rate of data from some motion sensors and location sensors in order to safeguard potentially sensitive information about users. 

These measurements come from the accelerometer, gyroscope, and geomagnetic field sensor of the gadget. The following code snippet demonstrates how to declare the HIGH SAMPLING RATE SENSORS permission if your app wants to collect motion sensor data at a greater rate. Otherwise, a security exception happens if your program tries to collect motion sensor data at a faster rate without declaring this permission. You will need to add this to your app’s manifest, which is the place where you add all the app’s permission:

<manifest>
    <!--This is your app's manifest file -->
    <uses-permission android:name="android.permission.HIGH_SAMPLING_RATE_SENSORS"/>
</manifest>

The refresh rate cap is determined by the method used to obtain sensor data:

  1. The sensor sampling rate is capped at 200 Hz:
    If the registerListener() function is used to keep track of sensor events. This holds true for all registerListener() method overloads.
  2. The sensor sampling rate is restricted to RATE NORMAL, or 50 Hz if the SensorDirectChannel class is used.

GeekTip: Ensure that the delivery rate you select when registering a sensor using the registerListener() function is appropriate for your application or use case. Data from sensors can be produced at very high rates. Allowing the system to deliver more data than you require drains battery life and system resources.

Listeners with No Registered Sensors

When you are through utilizing a sensor or when sensor activity pauses, be sure you deregister the sensor’s listener. The sensor will keep collecting data and using battery resources if a sensor listener is registered and its activity is interrupted until you unregister the sensor. To unregister a listener, use the onPause() method as demonstrated in the code below:

Kotlin




private SensorManager gfgSensorManager;
  
@Override
protected void onPause() {
    super.onPause();
    gfgSensorManager.unregisterListener(this);
}


Guidelines For Using and Accessing Sensors

Make careful to adhere to the recommendations in this section when you develop your sensor implementation. These recommendations represent best practices for anyone utilizing the sensor framework to access sensors and gather sensor data.

Collect Sensor Information Just in the Foreground

The following limitations apply to apps operating in the background on smartphones running Android 9 (API level 28) or higher:

  1. Events are not received by sensors that operate in continuous reporting modes, such as gyroscopes and accelerometers.
  2. Given these limitations, it’s ideal to monitor sensor events while your app is active or as a component of a running foreground service.
  3. Events are not sent to sensors that use the on-change or one-shot reporting modes.

Conclusion

Before attempting to collect data from a device, always be sure that it has sensors. Never presume the existence of a sensor just because it is one that is often used. No specific sensors must be included by device manufacturers in their products.

In addition to varying from device to device, sensor availability can also change between Android versions. The reason for this is that there have been multiple platform versions since the introduction of the Android sensors.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads