0

I am creating an application for fall detection and I am trying to implement RxKotlin to do it. To provide good data processing I'm taking sesnorEvent every 250ms and put it to the buffer (with last 10 seconds). Unfortunately, entire data in the buffer is the same event when I move my phone. Is the problem in my code or it just work like this?

Already I tried to use window and buffer, however, results are the same. Target API of the app is 28, min API 21. SensorListener is placed in the foreground Service

//initialization of the properties
private val proxy: BehaviorSubject<SensorEvent> = BehaviorSubject.create()
private var mSensorManager: SensorManager? = null
private var mAccelerometer: Sensor? = null
private lateinit var subscribe: Disposable

override fun onCreate() {
        super.onCreate()
        mSensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
        mAccelerometer = mSensorManager!!.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
        mSensorManager!!.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL)

        subscribe = Observable.interval(250, TimeUnit.MILLISECONDS)
            .map { proxy.value }
            .buffer(1, TimeUnit.SECONDS)
            .map {
                it.map { sensorEvent -> calculateAcceleration(sensorEvent!!) }
            }
            .subscribe {

                    Log.d("MySensor", it.toString())

            }
    }

//Here I call onNext also log data for every called event
override fun onSensorChanged(event: SensorEvent?) {
        proxy.onNext(event!!)
        Log.d("MySensor", calculateAcceleration(event).toString())
    }



private fun calculateAcceleration(event: SensorEvent): Float {
        val axisX = event.values[0]
        val axisY = event.values[1]
        val axisZ = event.values[2]
        val acceleration = sqrt(axisX.pow(2) + axisY.pow(2) + axisZ.pow(2))
        //Log.d("MyDataSave", acceleration.toString())
        return acceleration
    }



I expect the output of the observable was like [11.234473, 9.768473, 13.23543], however, I get [9.768473, 9.768473, 9.768473]. Main point is that the values in the buffer should be different after I move the phone during the buffer lifetime, but they are not.

Molly
  • 1,887
  • 3
  • 17
  • 34
MarBas
  • 11
  • 2

1 Answers1

1

Try adding return@map in your map lambdas (i.e., .map { return@map proxy.value }) . Also, you can directly subscribe to your proxy BehaviorSubject to keep track of the changes in your sensor. You can use debounce to 'delay' emissions. So your code will be:

override fun onCreate() {
        super.onCreate()
        mSensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
        mAccelerometer = mSensorManager!!.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
        mSensorManager!!.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL)

        proxy.debounce(250, TimeUnit.MILLISECONDS)
            .subscribe {
                Log.d("MySensor", it.toString())
            }
}
Christilyn Arjona
  • 2,173
  • 3
  • 13
  • 20