【问题标题】:How can I receive response from the sensors如何接收传感器的响应
【发布时间】:2013-12-13 22:52:38
【问题描述】:

我正在从 tutorial 开发一个指南针应用程序,但与教程有些不同,例如我不想绘制指南针,只需将值放在 EditText 中,问题是它不起作用或者我真的不知道发生了什么...我尝试调试应用程序但传感器没有响应并且日志没有显示任何错误...这就是我尝试的:

public class MainActivity extends Activity implements SensorEventListener {

float azimut;
private SensorManager mSensorManager;
Sensor accelerometer;
Sensor magnetometer;
float[] mGravity;
float[] mGeomagnetic;
private EditText edTLatitud;
private EditText edTLongitud;
private EditText edTCompass;
private EditText edTDirecc;


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

    edTCompass = (EditText)this.findViewById(R.id.edTxtBrujula);
    edTDirecc = (EditText)this.findViewById(R.id.edTxtBrujdireccion);

    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
}

@Override
public void onSensorChanged(SensorEvent event) {
    if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
        mGravity = event.values;
    if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
        mGeomagnetic = event.values;
    if(mGravity != null && mGeomagnetic != null){
        float R[] = new float[9];
        float I[] = new float[9];
        boolean success = SensorManager.getRotationMatrix(R,I, mGravity, mGeomagnetic);
        if(success){
            float orientation[] = new float[3];
            SensorManager.getOrientation(R, orientation);
            azimut = orientation[0];
            UpdateCompassOnScreen(azimut);
            edTCompass.setText((int)azimut);
        }
    }
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}

// Show the value on the application of the compass with the direction
public void UpdateCompassOnScreen(float value)
{
    if (value >= 338 && value <= 22.9999)
    {
        edTDirecc.setText("° N");
    }
    else if(value >= 23 && value <= 67.9999)
    {
        edTDirecc.setText("° NE");
    }
    else if (value >= 68 && value <= 112.9999)
    {
        edTDirecc.setText("° E");
    }
    else if(value >= 113 && value <= 157.9999)
    {
        edTDirecc.setText("° SE");
    }
    else if(value >= 158 && value <= 202.9999)
    {
        edTDirecc.setText("° S");
    }
    else if(value >= 203 && value <= 247.9999)
    {
        edTDirecc.setText("° SW");
    }
    else if(value >= 248 && value <= 292.9999)
    {
        edTDirecc.setText("° W");
    }
    else if (value >= 293 && value <= 337.9999)
    {
        edTDirecc.setText("° NW");
    }
    edTCompass.setText(String.valueOf(value));
}

感谢您的建议。

【问题讨论】:

    标签: android android-studio compass-geolocation


    【解决方案1】:

    我认为 getRotationMatrix 返回一个矩阵。

    这里。 API演示中有一些东西。 Pro Android 2 也有一些东西。

    public void onSensorChanged(SensorEvent 事件) {

            float[] mIdentityMatrix = new float[16];
            mIdentityMatrix[0] = 1;
            mIdentityMatrix[4] = 1;
            mIdentityMatrix[8] = 1;
            mIdentityMatrix[12] = 1;
    
            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                if (counter++ % 10 == 0) {
                    gravityMatrix = event.values.clone();
                    sensorReady = true;
                }
            }
            if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
                geomagneticMatrix = event.values.clone();
    
            if (sensorReady)
                SensorManager.getRotationMatrix(mRotationMatrix,
                        mIdentityMatrix, gravityMatrix, geomagneticMatrix);
        }
    

    【讨论】:

    • 此外,您需要了解 onSensorChanged() 会触发很多。您可能希望限制从中更新的频率。
    • 感谢您的回复,但请查看我的代码并根据教程 azimuth =orientation[0] 因此,它分配了向量的位置 0...
    • 布尔成功 = SensorManager.getRotationMatrix(R,I, mGravity, mGeomagnetic);
    • 我真的不知道你想用你的代码做什么。在调用 getRotationMatrix() 之前,矩阵 R 和 I 都已初始化。
    • 嗯,这是我链接的博客中的教程。
    猜你喜欢
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多