How do I use coroutines in my Kotlin code to read in phone sensor data one by one?

77 Views Asked by At

I'm working on a project where I'm trying to read sensor data from multiple sensors into a .txt file. I would like for the data to read in nicely so that it would be easy to read. Right now I've gotten the data to write to a .txt file but the data reads in very sloppy and at different rates. It appears that the sensors are listening and writing at different rates. I would like for each sensor to get their data and then wait for the next sensor to get the data and then the next until the final one and then write to the .txt file. From what I understand coroutines can help me do that but I'm trying to figure out how to implement it.

This is where I call on the sensors.

           val accelSensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
            val gyroSensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE)
            val lightSensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT)

            if(accelSensor != null && gyroSensor != null && lightSensor != null) {


                sensorManager.registerListener(this, accelSensor, readSpeed)
                sensorManager.registerListener(this, gyroSensor, readSpeed)
                sensorManager.registerListener(this, lightSensor, readSpeed)


            }

This is my onSensorChanged listener.

 @SuppressLint("SetTextI18n")
    override fun onSensorChanged(p0: SensorEvent?) {

        val path = this.getExternalFilesDir(null)
        val folder = File(path, "AppData")
        val file = File(folder, "AppData.txt")

        data = data + "${time.format(formatter)} , "

        if (safetyType == 0) {

            data = data + "Safe, "

        }
        else if (safetyType == 1) {

            data = data + "Unsafe, "

        }

        if (p0 != null) {



            if ( p0.sensor.type == Sensor.TYPE_ACCELEROMETER) {


                accelerometerTextView.text =
                    "Accelerometer X: " + "%.4f".format(p0.values[0]) +
                        " Y: " + "%.4f".format(p0.values[1]) + " Z: " + "%.4f".format(p0.values[2])

                data = data +  "Accel X: "+ "%.4f".format(p0.values[0]) +
                        " Y: " + "%.4f".format(p0.values[1]) + " Z: " + "%.4f".format(p0.values[2]) + " , "

            }

            if (p0.sensor.type == Sensor.TYPE_GYROSCOPE) {

                gyroscopeTextView.text =
                    "Gyroscope X: " + "%.4f".format(p0.values[0]) +
                            " Y: " + "%.4f".format(p0.values[1]) + " Z: " + "%.4f".format(p0.values[2])

                data = data +  "Gyro X: "+ "%.4f".format(p0.values[0]) +
                        " Y: " + "%.4f".format(p0.values[1]) + " Z: " + "%.4f".format(p0.values[2]) + " , "

            }

            if (p0.sensor.type == Sensor.TYPE_LIGHT) {

                lightTextView.text =
                    "Light Level: " + "%.4f".format(p0.values[0])

                data = data + "%.4f".format(p0.values[0]) + " , \n\n"


                file.appendText(data)
                data = ""

            }



        }



    }

Here is what my current output looks like:

Here is a made-up example of how I would like for the output to look like:

I tried to use If statements to check which sensor is being activated to read in the data one by one but it still ended up just reading in the data very sloppy and all over the place.

0

There are 0 best solutions below