The magnetometer in your smartphone measures the strength of earth’s magnetic field.
The magnetometer is enclosed in a small electronic chip that often incorporates another sensor (typically a built in accelerometer) that helps to correct the raw magnetic measurements using tilt information from the auxiliary sensor.

In addition to general rotational information, the magnetometer is crucial for detecting the relative orientation of your device relative to the Earth’s magnetic north.

Photo “Three-axis magnetometer sensor unit” by Steve Marple is licensed under CC BY 2.0