永远不会感觉到融合传感器。为什么?

时间:2018-02-27 03:33:26

标签: android android-sensors

我在Android Studio中创建了一个简单的传感器应用程序,如下所示。

package com.sensors.sensed;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements SensorEventListener {

    private SensorManager mSensorManager;
    private Sensor mRotationSensor;
    private float[] mRotation = new float[3];

    private TextView tv;

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

        tv = findViewById(R.id.tv);
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mRotationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR);
    }

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

    @Override
    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mRotationSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

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

    @Override
    public void onSensorChanged(SensorEvent event) {
        mRotation[0] = event.values[0];
        mRotation[1] = event.values[1];
        mRotation[2] = event.values[2];
        updateUI();
    }

    private void updateUI() {
        tv.setText("Rotation: (" + mRotation[0] + ", " + mRotation[1] + ", " + mRotation[2] + ")");
    }
}

但是,函数void onSensorChanged(SensorEvent event)永远不会在 AVD模拟器中调用,也不会使用 Bluestacks

我的示例应用程序有什么问题?我的 AVD仿真器扩展控件对话框中显示所有设置了某些值的传感器。

同样在带有 AVD模拟器的Logcat上,我看到以下两个错误:

a)E / art:向调试器发送回复失败:管道损坏
b)E / SensorManager:传感器或监听器为空

02-27 08:55:51.013 4834-4834/? I/art: Not late-enabling -Xcheck:jni (already on)
>>>>02-27 08:55:51.034 4834-4839/? E/art: Failed sending reply to debugger: Broken pipe
02-27 08:55:51.034 4834-4839/? I/art: Debugger is no longer active
02-27 08:55:51.056 4834-4834/? W/System: ClassLoader referenced unknown path: /data/app/com.sensors.sensed-1/lib/x86
02-27 08:55:51.064 4834-4834/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
>>>02-27 08:55:51.095 4834-4834/? E/SensorManager: sensor or listener is null
02-27 08:55:51.101 4834-4848/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                 [ 02-27 08:55:51.103  4834: 4834 D/         ]
                                                 HostConnection::get() New Host Connection established 0xab64cc80, tid 4834


                                                 [ 02-27 08:55:51.129  4834: 4848 D/         ]
                                                 HostConnection::get() New Host Connection established 0xacf54dc0, tid 4848
02-27 08:55:51.130 4834-4848/? I/OpenGLRenderer: Initialized EGL, version 1.4
02-27 08:55:51.130 4834-4848/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
02-27 08:55:51.133 4834-4848/? D/EGL_emulation: eglCreateContext: 0xab5632a0: maj 2 min 0 rcv 2
02-27 08:55:51.148 4834-4848/? D/EGL_emulation: eglMakeCurrent: 0xab5632a0: ver 2 0 (tinfo 0xab551d20)
02-27 08:55:51.170 4834-4848/? D/EGL_emulation: eglMakeCurrent: 0xab5632a0: ver 2 0 (tinfo 0xab551d20)

有趣的是,当我使用 Bluestacks 时,我在稍微修改过的代码时遇到了不同的错误,但功能相同:

E/Sensor-AccelerometerUI(xxxxx): java.io.IOException: Connection refused

更新

我发现只有融合的传感器无法正常工作,TYPE_ACCELEROMETERTYPE_MAGNETIC_FIELD等基本传感器虽然有效。 可能是什么原因?

感谢。

1 个答案:

答案 0 :(得分:0)

我有这个示例代码对Accelerometer非常好,但唯一的问题是它在Kotlin中 -

class AccelerometerActivity : AppCompatActivity(), SensorEventListener {

var context: Context = this
lateinit var sensorManager: SensorManager
lateinit var sensorAccelerometer: Sensor

lateinit var textView_X: EditText
lateinit var textView_Y: EditText
lateinit var textView_Z: EditText
lateinit var textView_Sensor: EditText

companion object {
    var x: Float = 0.0f
    var y: Float = 0.0f
    var z: Float = 0.0f
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_accelerometer)
    initViews()
}

private fun initViews() {
    sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
    sensorAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
    sensorManager.registerListener(this, sensorAccelerometer, SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM)

    textView_X = findViewById(R.id.textview_X) as EditText
    textView_Y = findViewById(R.id.textview_Y) as EditText
    textView_Z = findViewById(R.id.textview_Z) as EditText
    textView_Sensor = findViewById(R.id.textview_status) as EditText
}

override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
    Toast.makeText(context, "Accuracy Changed", Toast.LENGTH_LONG).show()
}

override fun onSensorChanged(event: SensorEvent?) {

    x = event!!.values[0]
    y = event.values[1]
    z = event.values[2]

    textView_X.setText(x.toString());
    textView_Y.setText(y.toString());
    textView_Z.setText(z.toString());

    if (x < -6 && x > -11) {
        textView_Sensor.setText("OVER RIGHT SIDE EDGE")
    } else if (x > 6 && x < 11) {
        textView_Sensor.setText("OVER LEFT SIDE EDGE")
    } else if (y > 6 && y < 11) {
        textView_Sensor.setText("VERTICAL STRAIGHT")
    } else if (y < -6 && y > -11) {
        textView_Sensor.setText("UPSIDE DOWN")
    } else if (z > 8) {
        textView_Sensor.setText("LYING STEADY - SCREEN UP")
    } else if (z < -8) {
        textView_Sensor.setText("LYING STEADY - SCREEN DOWN")
    }
  }
}

希望它能帮助