简体   繁体   中英

How to utilize the type_magnetic_field_uncalibrated-sensor on Android?

I'm creating an app for reading of magnetic field for a phone with Android 7.1.2 API 25. I have so fare successfully made one using the TYPE_MAGNETIC_FIELD-sensor, but I didn't manage to retrieve any data using the TYPE_MAGNETIC_FIELD_UNCALIBRATED-sensor.

public class MainActivity extends AppCompatActivity implements SensorEventListener {


    TextView textViewX;
    TextView textViewY;
    TextView textViewZ;


    private static SensorManager sensorManager;
    private Sensor magnetometer;


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



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


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

        magnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED);

    }

    @Override
    protected void onResume() {
        super.onResume();

        sensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_NORMAL);

    }

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

    }

    @Override
    public void onSensorChanged(SensorEvent Event) {

        if (Event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED) {
            float xAxis = (Event.values[0]);
            float yAxis = (Event.values[1]);
            float zAxis = (Event.values[2]);



            String textX = String.format("%.0f", xAxis);
            textViewX.setText(textX + "µTr");

            String textY = String.format("%.0f", yAxis);
            textViewY.setText(textY + "µTr");

            String textZ = String.format("%.0f", zAxis);
            textViewZ.setText(textZ + "µTr");



        }



    }

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

    }

}

Seems like there is never registered any events using the uncalibrated magnetic field sensor. Is there something I am missing, or is the uncalibrated sensor just not available?

我发现TYPE_MAGNETIC_FIELD_UNCALIBRATED在我的设备上不可用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM