Skip to content

Instantly share code, notes, and snippets.

@pentarex
Created May 4, 2017 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pentarex/263164f225f17db17aacdfc7cb52eccd to your computer and use it in GitHub Desktop.
Save pentarex/263164f225f17db17aacdfc7cb52eccd to your computer and use it in GitHub Desktop.
Kotlin Observable.from is not waiting for observable
// 1st Try
Observable.zip<Unit, Long, Unit>(Observable.from(units), Observable.interval(5, TimeUnit.SECONDS)) { unit, t -> unit }
.subscribe({ unit ->
// execute bluetooth and wait for response with .onBackpressureBuffer() and .timeout in seconds
rxBluetoothService.sendAndReceiveWithTimeout("at+...", 10)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ response ->
// do some stuff here
}, {
//Handle error
})
})
//2nd Try
Observable.just(units)
.concatMap({unitsList -> Observable.from(unitsList)})
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.computation())
.timeout(11, TimeUnit.SECONDS)
.flatMap({unit -> readBrakeValueFrom(unit, units, unitsMap).onErrorResumeNext(Observable.empty())})
.subscribe()
private fun readValuesFromBt(command: String) : Observable<String>{
return rxBluetoothService.sendAndReceiveWithTimeout("at+...", 10)
.observeOn(AndroidSchedulers.mainThread())
.doOnNext { btwiResponse ->
// same maging
}
.onErrorReturn({""})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment