Why does a cell phone need a magnetometer

Sensors in smartphones add extensive location and movement functions to your application, such as GPS, network location, accelerometer, gyroscope, temperature and barometer.

I would like to take a closer look at the following sensors:

  • Motion sensors measure acceleration forces and torsional forces on 3 axes. This chapter discusses accelerometers, gravity sensors, gyroscopes, and rotation vector sensors.
  • Position sensors measure the physical position of a device. Orientation sensors and magnetometers are described in this chapter.
  • Environmental sensors measure environmental parameters such as temperature, air pressure, lighting and humidity. This chapter describes the barometer, photometer and thermometer.

The sensors on the device can be accessed - if they are available - using the Android sensor framework. The framework offers classes and interfaces to solve a wide variety of tasks such as:

  • To find out which ones Sensors at all on the device Are available.
  • To the Skills of a sensor determine such as the maximum range, manufacturer and resolution.
  • Around capture raw sensor data and e.g. define minimum speeds that receive the sensor data.
  • Registration and deregistration of Sensor event listeners which record the changes.

introduction

Many types of sensors can be accessed with the Android sensor framework. Some of these sensors are hardware-based, others are software-based. Hardware-based sensors are physical components that are built into a handset or tablet device. They derive their data by directly measuring specific environmental properties such as acceleration, geomagnetic field strength or changes in angle. Software-based sensors are not physical devices, although they mimick hardware-based sensors. They derive their data from one or more of the hardware-based sensors and are sometimes referred to as virtual sensors or synthetic sensors. The linear accelerometer and the gravity sensor are examples of software-based sensors. Table 1 summarizes the sensors supported by the Android platform.

Few Android devices have all types of sensors. For example, most handheld devices and tablets have an accelerometer and magnetometer, but fewer devices have barometers or thermometers.

Table 1. Sensor types,which are supported on the Android platform.

Sensor framework

One can access these sensors and collect raw sensor data using the Android sensor framework. The sensor framework is part of the android.hardware package and comprises the following classes and interfaces:

SensorManager

You can use this class to create an instance of the sensor service. This class offers various methods such as for accessing and listing sensors.

sensor

One can use this class to create an instance of a specific sensor. This class offers various methods with which one can determine the capabilities of a sensor.

SensorEvent

The system uses this class to create a sensor event object that provides information on a sensor event. A sensor event object contains the following information:

  • the raw sensor data,
  • the type of sensor,
  • who generated the event,
  • the accuracy of the data and
  • the timestamp for the event.

SensorEventListener

You can use this interface to create two callback methods that receive notifications (sensor events) when sensor values ​​change or when the sensor accuracy changes.

In a typical application, these sensor-related APIs would be used to perform two basic tasks:

  • Identify sensors and sensor capabilities Run-time is useful if your application has functions that rely on certain sensor types or functions. For example, you may want to identify all of the sensors that are present on a device and disable any application functionality that is based on sensors that are not present. Likewise, you may want to identify all sensors of a particular type so that you can select the sensor implementation that will provide the best performance for your application.
  • Monitoring of sensor events is actually how to collect raw sensor data. A sensor event occurs each time a sensor detects a change in the parameters that the sensor is measuring. A sensor event provides four pieces of information:
    • the name of the sensor,
    • that triggered the event,
    • the timestamp for the event,
    • the accuracy of the event and
    • the raw sensor data that triggered the event.

Motion sensors

The Android platform offers several sensors that can be used to monitor the movement of a device.

The possible architectures of the sensors vary depending on the sensor type:

  • The sensors for Gravitation, linear acceleration, rotation vector, significant motion, pedometer and step detector are either hardware-based or software-based.
  • Accelerometers and gyroscope sensors are always hardware based.

Most Android devices have an accelerometer, and many already include a gyroscope. The availability of the software-based sensors is variable, as they often rely on one or more hardware sensors to derive their data. Depending on the device, these software-based sensors can derive their data from either the accelerometer and magnetometer or the gyroscope.

Motion sensors are useful for monitoring device movements such as tilting, shaking, rotating, or panning. The movement is usually one Reflection of direct user input (e.g. a user who has a car in a game), but it can also be a Reflection of the physical environment in which the device is located (e.g. when driving a car). In the first case, you monitor the Movement relative to the frame of reference of the device or the frame of reference of your application. In the second case, monitor the Movement relative to the frame of reference of the world. Motion sensors themselves are typically not used to monitor device position. However, you can use other sensors, such as B. the geomagnetic field sensor can be used to determine the position of a device relative to the reference system of the world.

Motion sensors return multidimensional arrays of sensor values ​​for each SensorEvent. For example, during a single sensor event, the accelerometer returns acceleration force data for the three coordinate axes and the gyroscope returns the rotational speed data for the three coordinate axes. These data values ​​are returned in a float array (values) along with other SensorEvent parameters. Table 2 summarizes the motion sensors available on the Android platform.

Table 2. Motion sensors supported on the Android platform.

Example of a gravity sensor

The gravity sensor provides a three-dimensional vector that indicates the direction and magnitude of the force of gravity. Typically this sensor is used to determine the relative orientation of the device in space. The following code shows how to get an instance of the standard gravity sensor:

The units correspond to those of the acceleration sensor (m / s2). The coordinate system corresponds to that of the acceleration sensor.

Position sensors

The Android platform offers two sensors that can be used to determine the position of a device:

  • the geomagnetic field sensor and
  • the accelerometer.

The Android platform also offers a sensor that can be used to determine how close a device is to another object (proximity sensor). The The geomagnetic field sensor and the proximity sensor are hardware-based. Most handset and tablet manufacturers include a geomagnetic field sensor. Similarly, handset manufacturers typically include a proximity sensor to determine when a handset is being held near a user's face (e.g., during a phone call). To determine the orientation of a device, one can use the readings from the device's accelerometer and the geomagnetic field sensor.

Note: The orientation sensor was no longer supported in Android 2.2 (API level 8), and the orientation sensor type was no longer used in Android 4.4W (API level 20).

Position sensors are useful for determining the physical position of a device in the world's frame of reference. E.g. the earth's magnetic field sensor can be used in combination with the acceleration sensor to determine the position of a device relative to the magnetic north pole. These sensors can also be used to determine the orientation of a device in the frame of reference of your application. Position sensors are typically not used to monitor movements or movements of the device, such as B. vibrations, inclinations or.

The geomagnetic field sensor and accelerometer return multidimensional arrays of sensor values ​​for each sensor event. For example, the geomagnetic field sensor provides geomagnetic field strength values ​​for each of the three coordinate axes during a single sensor event. Similarly, the accelerometer measures the acceleration applied to the device during a sensor event.

The proximity sensor provides a single value for each sensor event. Table 3 summarizes the position sensors that are supported on the Android platform.

Table 3. Position sensors supported on the Android platform.

Environmental sensors

The Android platform offers four sensors that can be used to monitor various environmental characteristics. With these sensors you can:

  • relative ambient humidity,
  • Illuminance,
  • Ambient pressure and
  • Ambient temperature monitor near an Android device.

All four environmental sensors are hardware based and only available if a device manufacturer has built them into a device. With the exception of the light sensor, which most device manufacturers use to control screen brightness, environmental sensors are not always available on devices. For this reason, it is especially important that you check that an environmental sensor is present at runtime before attempting to collect data.

In contrast to most motion sensors and position sensors, which return a multi-dimensional array of sensor values ​​for each SensorEvent, give Environmental sensors one for each data event individual sensor value (e.g. temperature in ° C, pressure in hPa). Environmental sensors typically do not require data filtering or processing. Table 4 summarizes the environmental sensors that are supported on the Android platform.

Table 4. Environmental sensors supported on the Android platform.

Example light, pressure and temperature sensors

The raw data you get from the light, pressure, and temperature sensors usually don't require calibration, filtering, or modification, which makes them the easiest sensors to use. To collect data from these sensors, you create an instance of the SensorManager class that you can use to determine an instance of a physical sensor. Then you register a sensor listener in the onResume () method and start with incoming sensor data in the onSensorChanged () callback method. The following code shows how it works:

You always have to include implementations of the onAccuracyChanged () and onSensorChanged () callback methods. Make sure that a sensor is unregistered whenever an activity is interrupted. This prevents a sensor from continuously collecting data and draining the battery.

 

Source & pictures: https://developer.android.com/guide/topics/sensors/