简体   繁体   English

如何在Android上利用type_magnetic_field_uncalibrated-sensor?

[英]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. 我正在创建一个应用程序,用于使用Android 7.1.2 API 25读取手机的磁场。到目前为止,我已经成功使用TYPE_MAGNETIC_FIELD-sensor制作了一个应用程序,但是我无法使用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在我的设备上不可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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