【问题标题】:How to utilize the type_magnetic_field_uncalibrated-sensor on Android?如何在 Android 上使用 type_magnetic_field_uncalibrated-sensor?
【发布时间】:2019-07-31 13:59:32
【问题描述】:

我正在为具有 Android 7.1.2 API 25 的手机创建一个用于读取磁场的应用程序。到目前为止,我已经成功地使用 TYPE_MAGNETIC_FIELD 传感器制作了一个应用程序,但我没有设法使用TYPE_MAGNETIC_FIELD_UNCALIBRATED-传感器。

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) {

    }

}

似乎从未使用未校准的磁场传感器记录任何事件。是我遗漏了什么,还是未校准的传感器不可用?

【问题讨论】:

    标签: android magnetometer


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 2011-12-08
      • 2022-08-03
      • 2021-08-31
      • 2018-05-06
      • 1970-01-01
      相关资源
      最近更新 更多