The Android platform provides two sensors to determine the location of the device, and these two sensors are magnetic field sensors and directional sensors. The Android platform also provides sensors (proximity sensors) that measure the distance from the front of a device to a nearby object. magnetic field sensors and proximity sensors are hardware-based sensors. Most Android phones and tablets have magnetic field sensors. Proximity sensors are often common in mobile phones. Because you can use this sensor to test how far away your phone screen is from your face when you answer a call. Some work can be done when the phone is close to the ear. The direction sensor is software-based and the sensor's return data comes from an accelerometer and a magnetic field sensor.
Position sensors are useful for determining the physical location of a device in the real world. For example, you can combine a magnetic field sensor and an accelerometer to measure the position of a device relative to the geomagnetic North Pole, and you can use a direction sensor to determine the position of the current device relative to its own reference frame.
Both the magnetic field sensor and the direction sensor return a value of 3 values (sensorevent.values), while the proximity sensor returns only 1 values.
Direction sensor:
Sensorevent.values[0]:
The angle of rotation around the z axis. If the y-axis (the normal direction of the phone) is facing north, the value is 0, if the y-axis is pointing south, the value is the 180,y axis pointing east, the value is 90, if the y-axis points to the West, the value is 270.
SENSOREVENT.VALUES[1]:
The degree of rotation around the x-axis. A positive value is changed when the positive direction of the y-axis is from the z-axis. Conversely, it is negative. The value varies between 180 and 180.
SENSOREVENT.VALUES[2]:
The degree of rotation around the y-axis. When the positive direction of the x-axis is positive from the z-axis, a positive value is changed. Conversely, it is negative. The value varies between 180 and 180.
magnetic field Sensors:
Sensorevent.values[0]:
Magnetic force along the x-axis (Μt,millitesla)
SENSOREVENT.VALUES[1]:
Magnetic force along the y-axis (Μt,millitesla)
SENSOREVENT.VALUES[2]:
Magnetic force along the z axis (ΜT,MILLITESLA)
Proximity sensors:
Sensorevent.values[0]:
Distance from the front of the phone (CM) to the nearest physical distance
The exact code is the same as the previous one, and I won't write it.
Android Position sensor