我实现了一个代码片段来计算磁场的特斯拉值。我最近对其进行了测试,我注意到当我在公共汽车上时,该值是24,但是当我不在公共汽车上时,该值约为45。我的意思是,总线是金属的,因此其内部的值应该更高。对于这种奇怪的行为有什么解释吗?

感谢您的帮助。

码:

public class MainActivity extends ActionBarActivity implements
        SensorEventListener {
    String telsaString;

    private TextView magneticX;
    private TextView magneticY;
    private TextView magneticZ;
    private TextView magneticT;
    private SensorManager sensorManager = null;

    private long lastUpdate = 0;

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

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        // Capture magnetic sensor related view elements
        magneticX = (TextView) findViewById(R.id.valMag_X);
        magneticY = (TextView) findViewById(R.id.valMag_Y);
        magneticZ = (TextView) findViewById(R.id.valMag_Z);
        magneticT = (TextView) findViewById(R.id.valMag_T);

        // Register magnetic sensor
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_NORMAL);


    }

    @Override
    protected void onPause() {
        // Unregister the listener
        sensorManager.unregisterListener(this);
        super.onPause();
    }

    @Override
    protected void onStop() {
        // Unregister the listener
        sensorManager.unregisterListener(this);
        super.onStop();
    }

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

        // Register magnetic sensor
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_NORMAL);
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    public void onSensorChanged(SensorEvent sensorEvent) {
        synchronized (this) {

            float magX = sensorEvent.values[0];

            float magY = sensorEvent.values[1];

            float magZ = sensorEvent.values[2];

            magneticX.setText(Float.toString(sensorEvent.values[0]));
            magneticY.setText(Float.toString(sensorEvent.values[1]));
            magneticZ.setText(Float.toString(sensorEvent.values[2]));

            int teslaXYZ = (int)(Math.sqrt((magX * magX) + (magY * magY)
                    + (magZ * magZ)));
            String str = String.valueOf(teslaXYZ);
            magneticT.setText(str);

            long curTime = System.currentTimeMillis();
            if ((curTime - lastUpdate) > 15000) {

                lastUpdate = curTime;

                try {
                    JSONObject tesla = new JSONObject();
                    tesla.put("tesla", teslaXYZ);
                    //tesla.put("tesla", teslaXYZ);
                    telsaString = tesla.toString();
                    new MyAsyncTask().execute(telsaString);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }
    }

最佳答案

像公共汽车这样的车辆将包含许多金属零件,其中一些会被磁化并具有自己的磁场。与您离开车辆时相比,设备的磁力计将获取地磁场和这些其他磁场的叠加,并显示错误的方向和绝对值。

汽车的引擎和电气基础设施也会造成干扰(在测试时,我注意到启动汽车的引擎时磁力计读数存在明显差异)。

07-27 16:17