private ObservableTransformer < RxBleInternalScanResult , RxBleInternalScanResult > repeatedWindowTransformer ( @ IntRange ( from = 0 , to = 4999 ) final int windowInMillis ) { final long repeatCycleTimeInMillis = TimeUnit . SECONDS . toMillis ( 5 ) ; final long delayToNextWindow = Math . max ( repeatCycleTimeInMillis - windowInMillis , 0 ) ; // to be sure that it won't be negative return new ObservableTransformer < RxBleInternalScanResult , RxBleInternalScanResult > ( ) { @ Override public Observable < RxBleInternalScanResult > apply ( final Observable < RxBleInternalScanResult > rxBleInternalScanResultObservable ) { return rxBleInternalScanResultObservable . take ( windowInMillis , TimeUnit . MILLISECONDS , scheduler ) . repeatWhen ( new Function < Observable < Object > , ObservableSource < ? > > ( ) { @ Override public ObservableSource < ? > apply ( Observable < Object > observable ) throws Exception { return observable . delay ( delayToNextWindow , TimeUnit . MILLISECONDS , scheduler ) ; } } ) ; } } ; }
A convenience method for running a scan for a period of time and repeat in five seconds intervals .