public Flowable < ReactiveSensorEvent > observeSensor ( int sensorType , final int samplingPeriodInUs , final Handler handler , final BackpressureStrategy strategy ) { if ( ! hasSensor ( sensorType ) ) { String format = "Sensor with id = %d is not available on this device" ; String message = String . format ( Locale . getDefault ( ) , format , sensorType ) ; return Flowable . error ( new SensorNotFoundException ( message ) ) ; } final Sensor sensor = sensorManager . getDefaultSensor ( sensorType ) ; final SensorEventListenerWrapper wrapper = new SensorEventListenerWrapper ( ) ; final SensorEventListener listener = wrapper . create ( ) ; return Flowable . create ( new FlowableOnSubscribe < ReactiveSensorEvent > ( ) { @ Override public void subscribe ( final FlowableEmitter < ReactiveSensorEvent > emitter ) throws Exception { wrapper . setEmitter ( emitter ) ; if ( handler == null ) { sensorManager . registerListener ( listener , sensor , samplingPeriodInUs ) ; } else { sensorManager . registerListener ( listener , sensor , samplingPeriodInUs , handler ) ; } } } , strategy ) . doOnCancel ( new Action ( ) { @ Override public void run ( ) throws Exception { sensorManager . unregisterListener ( listener ) ; } } ) ; }
Returns RxJava Observable which allows to monitor hardware sensors as a stream of ReactiveSensorEvent object with defined sampling period