I use the following code to listen for an unsolicited message from my Bluetooth LE device. I am expecting a byte array with text that says "datc00099", for example. Instead, the mapper is returning a null SingleSource. What could be going wrong here?
val charUUID = scanResult.scanRecord.serviceUuids?.get(0)?.uuid
/* Establish connection to device */
rxBleDevice!!.establishConnection(false) ?
.doOnNext {
_ ->
Log.d("Device: ", "Connection Established")
} ?
.flatMapSingle {
rxBleConnection ->
charUUID ? .let {
rxBleConnection.readCharacteristic(it)
}
} ? .subscribe({
count ->
// count should be in bytes
println("OUTPUT: $count")
}, {
throwable ->
Log.d("ERROR: ", "$throwable")
})
Error:
D/ERROR:: java.lang.NullPointerException: The mapper returned a null SingleSource
Debug:
Related
i use Rx in android below
repository.getUserList()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ response ->
val userList = response.body()!!
view.showUser(userList)
}, { throwable ->
handlerException(view, throwable)
}).addToDisposable()
It works.
But sometimes response.body() is null, maybe network error or server not response,
then app will crash.
so i have to add try catch like this
repository.getUserList()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ response ->
try {
val userList = response.body()!!
view.showUser(userList)
} catch (e: Exception) {
handleException(view, e)
}
}, { throwable ->
handleException(view, throwable)
}).addToDisposable()
But I think there should be a better way to handler error in rx.
can anyone help me, thanks.
you can use filter operator
.filter { it != null }
this was RX way but you can use kotlin null safety like this:
.subscribe({ response ->
response.body()?.let{
view.showUser(userList)
}
}
hope it helped 👍🏻
I am trying to transform an Observable in my login function but I keep getting this error. This my code and error I am getting on flatMap
fun login(phoneNumber: String, password: String, deviceId: String) {
// remove previous subscriptions
disposables.clear()
// function to save userInfo and access token
val saveResponse = { response: LoginResponse ->
val user = response?.user
val token = response?.token
// userManager.updateToken(champion, token, deviceId)
}
// on success callback
val onSuccess = { isSuccess: Boolean ->
progressBarVisibility.postValue(false)
loginSuccess.postValue(isSuccess)
if (!isSuccess) errorMessage.postValue("An error occurred please try again.")
}
// on failure callback
val onError = { throwable: Throwable ->
val message = when (throwable) {
is HttpException -> when (throwable.code()) {
400 -> "Enter valid Phone Number or Password"
422 -> "Incorrect Phone Number or Password"
else -> throwable.toErrorMessage()
}
else -> "An Error Occurred."
}
// show error message
errorMessage.postValue(message)
progressBarVisibility.postValue(false)
}
val disposable = accountUseCase.login(LoginRequest(phoneNumber, password)).observeOnUI()
.doOnSubscribe { progressBarVisibility.postValue(true) }
.flatMap {
val resp = it.data
when (resp) {
null -> Single.just(false)
else -> saveResponse(it)
}
}
.subscribe(onSuccess, onError)
// add subscription to disposables
disposables.add(disposable)
}
error
Type mismatch. Required: ((BaseResponse<LoginResponse>!) → SingleSource<out (???..???)>!)! Found: (BaseResponse<LoginResponse>!) → Any!
The problem is with the implicit return of your flatMap:
when (resp) {
null -> Single.just(false)
else -> saveResponse(it)
}
In the null branch, the return type is Single<Boolean>.
In the else branch, you return the result of saveResponse. But the return type of saveResponse is Unit, because it doesn't return anything.
Kotlin therefore infers the return type of your flatMap to be either Single<Boolean> or Unit, and the only common supertype of these is Any.
This is why you get the error message: Found: (BaseResponse<LoginResponse>!) → Any!
You need to have saveResponse return something, probably also a Single<Boolean>, depending on your usecase.
I am developing an Android app using RxJava.
I have some API call chains.
verify
consume
val verify = Completable.error(Exception("TEST"))
.doOnSubscribe { Log.d(TAG, "1. verify") }
.doOnComplete{ Log.d(TAG, "1. verify - success") }
.doOnError { Log.e(TAG, "1. verify - failed: ${it.message}") }
.retryWhen { attempts ->
attempts.zipWith(
Flowable.range(1, 3), BiFunction<Throwable, Int, Long> { t, i ->
if (i <= 3) {
1L
} else {
throw t
}
}
).flatMap {
Flowable.timer(it, TimeUnit.SECONDS)
}
}
// 2. consume
val consume = Single.just("SUCCESS")
.doOnSubscribe { Log.d(TAG, "2. consume") }
.doOnSuccess { Log.d(TAG, "2. consume - success") }
.doOnError { Log.e(TAG, "2. consume - failed: ${it.message}", it) }
disposable.add(
verify.andThen (consume)
.subscribeOn(ioScheduler)
.observeOn(uiScheduler)
.subscribe({
Log.d(TAG, "done")
}, { t ->
Log.e(TAG, "failed: ${t.message}", t)
})
);
What I excepted is...
"verify" should be called 3 times every 1 seconds.
After 3 retries failed, it should be done with Error.
But in my case, "consume" was run too.
Why?
I want to skip "consume" if "verify" is failed!
How can I do it?
It's because your code is not failing.
With Flowable.range(1, 3) you create a range from 1 to 3 so the else part of your code is never reached.
Try with Flowable.range(1, 4) and you will see the correct behaviour.
I'm making an Android application with some BLE interraction using the RxAndroidBLE API. I followd examples guidelines and samples from https://github.com/Polidea/RxAndroidBle
I establish a BLE connection with a specified device, later while connected I read and write characteristic with no problem, but when i try to setup notification for the battery level characteristic I get the following throwable error message : Already connected to device with MAC address XX:XX..."
I really don't understand the error in that context since I can read and write in characteristic with no problem.
I want to setup notification for this characteristic after an initial read of its value for specific purpose.
Here is a sample code that reproduce my problem :
private lateinit var device: RxBleDevice
private var connectionObservable: Observable<RxBleConnection>? = null
private var rxBleConnection: RxBleConnection? = null
private val connectionDisposable = CompositeDisposable()
private val connectionStateDisposable = CompositeDisposable()
private var notifyValueChangeSubscription = CompositeDisposable()
var enableBatteryNotificationRunnable: Runnable = Runnable {
enableBatteryNotification()
}
private var myHandler = Handler()
val DELAY_BEFORE_ENABLE_NOTIFICATION: Long = 100
private fun connect() {
connectionObservable = device.establishConnection(false)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
connectionObservable?.let {
connectionDisposable.add(it.subscribe(
{ rxBleConnection ->
this.rxBleConnection = rxBleConnection
},
{ _ ->
Log.e("connect", "connexion error")
})
)
}
val state = device.observeConnectionStateChanges().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
connectionStateDisposable.add(
state.subscribe(
{ connectionState ->
Log.i("connect", "connexion state :$connectionState")
if(connectionState == RxBleConnection.RxBleConnectionState.CONNECTED) {
myHandler.postDelayed(enableBatteryNotificationRunnable, DELAY_BEFORE_ENABLE_NOTIFICATION);
}
}
)
{ _ ->
Log.e("connection listener", "connexion state error")
}
)
}
private fun enableBatteryNotification () {
connectionObservable?.let {
var observableToReturn = it
.flatMap { it.setupNotification(UUID_BATTERY_LEVEL) }
.doOnNext {
Log.i("NOTIFICATION", "doOnNext")
}
.flatMap { it }
.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
notifyValueChangeSubscription.add(observableToReturn.subscribe({ bytes ->
var strBytes = String(bytes)
Log.i("NOTIFICATION", "value change: $strBytes")
},
{ throwable ->
Log.e("NOTIFICATION", "Error in notification process: " + throwable.message)
})
)
}
}
Thanks in advance for any help :)
setupNotification returns “Error already connected” whereas no connection request is send
Two connection requests are actually made — hence the error. From the RxBleDevice.establishConnection() Javadoc:
* Establishes connection with a given BLE device. {#link RxBleConnection} is a handle, used to process BLE operations with a connected
* device.
In your code there are two subscriptions to the establishConnection() Observable.
private lateinit var device: RxBleDevice
private var connectionObservable: Observable<RxBleConnection>? = null
private var rxBleConnection: RxBleConnection? = null
private val connectionDisposable = CompositeDisposable()
private val connectionStateDisposable = CompositeDisposable()
private var notifyValueChangeSubscription = CompositeDisposable()
var enableBatteryNotificationRunnable: Runnable = Runnable {
enableBatteryNotification()
}
private var myHandler = Handler()
val DELAY_BEFORE_ENABLE_NOTIFICATION: Long = 100
private fun connect() {
connectionObservable = device.establishConnection(false)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
connectionObservable?.let {
connectionDisposable.add(it.subscribe( // << Here is the first subscription
{ rxBleConnection ->
this.rxBleConnection = rxBleConnection
},
{ _ ->
Log.e("connect", "connexion error")
})
)
}
val state = device.observeConnectionStateChanges().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
connectionStateDisposable.add(
state.subscribe(
{ connectionState ->
Log.i("connect", "connexion state :$connectionState")
if(connectionState == RxBleConnection.RxBleConnectionState.CONNECTED) {
myHandler.postDelayed(enableBatteryNotificationRunnable, DELAY_BEFORE_ENABLE_NOTIFICATION);
}
}
)
{ _ ->
Log.e("connection listener", "connexion state error")
}
)
}
private fun enableBatteryNotification () {
connectionObservable?.let {
var observableToReturn = it
.flatMap { it.setupNotification(UUID_BATTERY_LEVEL) }
.doOnNext {
Log.i("NOTIFICATION", "doOnNext")
}
.flatMap { it }
.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
notifyValueChangeSubscription.add(observableToReturn.subscribe({ bytes -> // << Here is the second subscription
var strBytes = String(bytes)
Log.i("NOTIFICATION", "value change: $strBytes")
},
{ throwable ->
Log.e("NOTIFICATION", "Error in notification process: " + throwable.message)
})
)
}
}
This situation is a common source of confusion for people learning RxJava. There are three paths to fix your situation. From least to most amount of work:
Share the establishConnection Observable
It is possible to share a single RxBleConnection with RxReplayingShare. Change this:
connectionObservable = device.establishConnection(false)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
To this:
connectionObservable = device.establishConnection(false)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.compose(ReplayingShare.instance())
Use the rxBleConnection: RxBleConnection? property
Instead of:
connectionObservable?.let {
var observableToReturn = it
.flatMap { it.setupNotification(UUID_BATTERY_LEVEL) }
.doOnNext {
Log.i("NOTIFICATION", "doOnNext")
}
.flatMap { it }
.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
notifyValueChangeSubscription.add(observableToReturn.subscribe({ bytes -> // << Here is the second subscription
var strBytes = String(bytes)
Log.i("NOTIFICATION", "value change: $strBytes")
},
{ throwable ->
Log.e("NOTIFICATION", "Error in notification process: " + throwable.message)
})
)
}
Make it:
rxBleConnection?.let {
var observableToReturn = rxBleConnection.setupNotification(UUID_BATTERY_LEVEL)
.doOnNext {
Log.i("NOTIFICATION", "doOnNext")
}
.flatMap { it }
.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
notifyValueChangeSubscription.add(observableToReturn.subscribe({ bytes -> // << Here is the second subscription
var strBytes = String(bytes)
Log.i("NOTIFICATION", "value change: $strBytes")
},
{ throwable ->
Log.e("NOTIFICATION", "Error in notification process: " + throwable.message)
})
)
}
This is discouraged as you may end up with a RxBleConnection that is no longer valid as it may have been disconnected before calling enableBatteryNotification()
Change the flow of your code to use a single .subscribe()
This is a custom solution tailored to your exact use-case. Unfortunately with the information you have added is not enough to create a drop-in code replacement but it could look something like this:
device.establishConnection(false)
.flatMap { connection ->
Observable.merge(
connection.readCharacteristic(uuid0).map { ReadResult(uuid0, it) }.toObservable(),
connection.setupNotification(uuid1).flatMap { it }.map { NotifyResult(uuid1, it) }.delaySubscription(100, TimeUnit.MILLISECONDS)
)
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ /* handle ReadResult/NotifyResult */ },
{ /* handle potential errors */ }
)
Where ReadResult and NotifyResult would be data class that take UUID and ByteArray
I'm trying to retrieve a value from a Bluetooth Device.
if (rxBleDevice.connectionState != RxBleConnection.RxBleConnectionState.CONNECTED) {
rxBleDevice!!.establishConnection(false) ? .subscribe({
rxBleConnection ->
Log.d("Device: ", "Connection Established")
val stringDeviceUUID = rxBleDevice.bluetoothDevice.uuids[0].toString()
val charUUID = UUID.nameUUIDFromBytes(stringDeviceUUID.toByteArray())
val count = rxBleConnection.readCharacteristic(charUUID)
println("OUTPUT: ${count}")
}, {
throwable -> Log.d("Device: ", "$throwable")
})
}
I'm using the following dependencies, mainly RxJava and a reactive Bluetooth library called RxAndroidBLE:
implementation 'io.reactivex.rxjava2:rxkotlin:2.1.0'
implementation "com.polidea.rxandroidble2:rxandroidble:1.8.1"
implementation "io.reactivex.rxjava2:rxjava:2.2.7"
My output:
I/System.out: OUTPUT: io.reactivex.internal.operators.single.SingleFlatMap#bf9162d
I have no how to process this object. I believe I should be receiving a simple ByteArray from the Bluetooth device.
An example for the value I should see is datc00099, indicating a count of 99.
You are supposed to subscribe to Single. Following the examples provided by RxAndroidBle, something like this might work in your case:
if (rxBleDevice.connectionState != RxBleConnection.RxBleConnectionState.CONNECTED) {
// Have your charUUID ready. Might need extra null checks for rxBleDevice
val charUUID = rxBleDevice.bluetoothDevice.uuids[0].uuid
rxBleDevice!!.establishConnection(false) ?
.doOnNext {
_ -> Log.d("Device: ", "Connection Established")
} ?
.flatMapSingle {
rxBleConnection -> rxBleConnection.readCharacteristic(charUUID)
} ? .subscribe({
count ->
// count should be in bytes
println("OUTPUT: $count")
}, {
throwable ->
Log.d("ERROR: ", "$throwable")
})
}