Kotlin使用运动传感器

时间:2018-08-06 14:39:40

标签: android kotlin kotlin-android-extensions

我试图弄清楚使用Kotlin的运动传感器是如何工作的。 我想做些什么,如果电话打开了,纠正了,如果电话关闭了,错了。 我已经看过教程,但仍然无法弄清楚。 以下是我尝试过的代码。

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

class MainActivity : AppCompatActivity(), SensorEventListener {

    private lateinit var sensorManager: SensorManager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        this.sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager

        sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)?.let {
            this.accelerometer = it
        }

        sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY)?.let {
            this.gravity = it
        }

        sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE)?.let {
            this.gyroscope = it
        }

        sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION)?.let {
            this.linearAcceleration = it
        }

        sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR)?.let {
            this.rotationVector = it
        }

    }

    override fun onSensorChanged(event: SensorEvent?) {
        when (event?.sensor?.type) {
            Sensor.TYPE_LINEAR_ACCELERATION -> {
                valuesLinAcceleration[0] = event.values[0]
                valuesLinAcceleration[1] = event.values[1]
                valuesLinAcceleration[2] = event.values[2]

                this.binding.tvLinearAccelX.text = event.values[0].formatValue()
                this.binding.tvLinearAccelY.text = event.values[1].formatValue()
                this.binding.tvLinearAccelZ.text = event.values[2].formatValue()
            }

            Sensor.TYPE_GRAVITY -> {
                valuesGravity[0] = event.values[0]
                valuesGravity[1] = event.values[1]
                valuesGravity[2] = event.values[2]

                this.binding.tvGravity.text = event.values[0].formatValue()
                this.binding.tvGravityY.text = event.values[1].formatValue()
                this.binding.tvGravityZ.text = event.values[2].formatValue()
            }

            Sensor.TYPE_ACCELEROMETER -> {
                val alpha = 0.8f

                accelGravity[0] = alpha * accelGravity[0] + (1 - alpha) * event.values[0]
                accelGravity[1] = alpha * accelGravity[1] + (1 - alpha) * event.values[1]
                accelGravity[2] = alpha * accelGravity[2] + (1 - alpha) * event.values[2]

                accelLin[0] = event.values[0] - accelGravity[0]
                accelLin[1] = event.values[1] - accelGravity[1]
                accelLin[2] = event.values[2] - accelGravity[2]

                this.binding.tvAccelerometerX.text = accelLin[0].formatValue()
                this.binding.tvAccelerometerY.text = accelLin[1].formatValue()
                this.binding.tvAccelerometerZ.text = accelLin[2].formatValue()

            }

            Sensor.TYPE_GYROSCOPE -> {
                valuesGyroscope[0] = event.values[0]
                valuesGyroscope[1] = event.values[1]
                valuesGyroscope[2] = event.values[2]

                this.binding.tvGyroscope.text = event.values[0].formatValue()
                this.binding.tvGyroscopeY.text = event.values[1].formatValue()
                this.binding.tvGyroscopeZ.text = event.values[2].formatValue()
            }

            Sensor.TYPE_ROTATION_VECTOR -> {
                valuesRotation[0] = event.values[0]
                valuesRotation[1] = event.values[1]
                valuesRotation[2] = event.values[2]

                this.binding.tvRotationVector.text = event.values[0].formatValue()
                this.binding.tvRotationVectorY.text = event.values[1].formatValue()
                this.binding.tvRotationVectorZ.text = event.values[2].formatValue()
            }
        }


    }

    override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {

    }
}

似乎缺少了一些东西,如果你们能指出正确的方向或正确的方法,那将非常有帮助。谢谢

1 个答案:

答案 0 :(得分:0)

您没有为传感器注册任何侦听器,因此您将永远不会获得任何更新。

您可以通过以下方式注册侦听器:

protected void onResume() {
    super.onResume();
    sensorManager.registerListener(this, this.accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    // repeat that line for each sensor you want to monitor
}

...

protected void onPause() {
    super.onPause();
    sensorManager.unregisterListener(this);
}

您可以根据使用情况使用不同的采样周期。检查SensorManager中定义的常量,以发现更适合您的内容。