Magnetometer sensor works only on a few devices

Asked

Viewed 92 times

1

I created an app just to test the tilt of the device’s X, Y and Z axes. In tests, some devices the sensors read the values, while others do not. I noticed that the TYPE_ACCELEROMETER sensor works on almost all the most current devices, but the TYPE_MAGNETIC_FIELD sensor does not work at all. Actually the TYPE_MAGNETIC_FIELD sensor does not exist on some phones or I should use another more current sensor that does the same function?

Below the Mainactivity code:

package br.com.exesensor_2019101;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements SensorEventListener {

    private SensorManager sensorManager;
    private Sensor accelerometer;
    private Sensor magnetometer;

    private float[] accelerometerReading = new float[3];
    private float[] magnetometerReading = new float[3];

    private float[] rotationMatrix = new float[9];
    private float[] remapCoordinate = new float[9];
    private float[] orientationAngles = new float[3];

    private TextView textViewX;
    private TextView textViewY;
    private TextView textViewZ;

    private static final int TAXA_LEITURA = 250000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

        textViewX = (TextView) findViewById(R.id.textViewX);
        textViewY = (TextView) findViewById(R.id.textViewY);
        textViewZ = (TextView) findViewById(R.id.textViewZ);

    }

    @Override
    protected void onResume() {
        super.onResume();
        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        magnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

        if (accelerometer == null || magnetometer == null){
            Toast.makeText(this, "Não foi possível acessar algum sensor do dispositivo!", Toast.LENGTH_SHORT).show();
            finish();
        }else {
            if (accelerometer != null) {
                sensorManager.registerListener(this, accelerometer, TAXA_LEITURA);
            }

            if (magnetometer != null) {
                sensorManager.registerListener(this, magnetometer, TAXA_LEITURA);
            }
        }
    }

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

    @Override
    public void onSensorChanged(SensorEvent event) {
        switch (event.sensor.getType()){
            case Sensor.TYPE_ACCELEROMETER:
                accelerometerReading = event.values.clone();
                break;
            case Sensor.TYPE_MAGNETIC_FIELD:
                magnetometerReading = event.values.clone();
                break;
        }
        updateOrientationAngles();
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }

    public void updateOrientationAngles() {
        if (accelerometerReading != null && magnetometerReading != null){
            SensorManager.getRotationMatrix(rotationMatrix, null, accelerometerReading, magnetometerReading);
            SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, remapCoordinate);
            SensorManager.getOrientation(remapCoordinate, orientationAngles);

            textViewX.setText((String.format("Eixo X: %.2f°", orientationAngles[0] * 57.2957795f * -1)));
            textViewY.setText((String.format("Eixo Y: %.2f°", orientationAngles[1] * 57.2957795f * -1)));
            textViewZ.setText((String.format("Eixo Z: %.2f°", orientationAngles[2] * 57.2957795f * -1)));

            accelerometerReading = null;
            magnetometerReading = null;
        }
    }
}

Below the code of the XML Manifesto

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="br.com.exesensor_2019101">

    <uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" />
    <uses-feature android:name="android.hardware.sensor.compass" android:required="true"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

1 answer

1


  • got, researching here, I found even some posts saying that some devices do not have this sensor.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.