Magnetic field Sensor

In this tutorial i will show you how to implement a magnetic field sensor.

Note:THIS TUTORIAL WORKS ONLY WITH REAL DEVICE

following Framework is needed:

  • SensorManager (provide instance of class and constants)
  • Sensor(provide sensor types)
  • SensorEvent(represent data like accurancy, time-stamp)
  • SensorEventListener(refresh real time information by changing data with methods onAccuratyChanged(…),OnSensorChanged(..) )

MainActivity

public class MainActivity extends Activity implements SensorEventListener {
    private SensorManager sensorManager;
    private Sensor Field;
    EditText txbox;
    @Override
    public final void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         txbox = findViewById(R.id.txbox);
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        Field = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    }

    @Override
    public final void onAccuracyChanged(Sensor sensor, int accuracy) {
        // Do something here if sensor accuracy changes.
    }

    @Override
    public final void onSensorChanged(SensorEvent event) {

        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];
        txbox.setText("x:"+x+"y:"+y+"z:"+z);
    }

    @Override
    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(this,Field, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/txbox"
        style="@android:style/Widget.EditText"
        android:layout_width="224dp"
        android:layout_height="43dp"
        android:layout_marginStart="99dp"
        android:layout_marginTop="256dp"
        android:layout_marginEnd="49dp"
        android:layout_marginBottom="430dp"
        android:background="#EBE7E8"
        android:editable="false"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="sensor"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textColorLink="#FFFFFF"
        android:textCursorDrawable="@android:color/transparent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.115"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.615" />
</android.support.constraint.ConstraintLayout>

Download Project: