Open pmagnuson opened 11 months ago
On Android side we use the RxAndroidBle library of Polidea. After migration towards RxJava 2 some of the errors are not routed properly to their listeners and thus this will result in a BLE Undeliverable Exception. The root cause lies in the threading of the Android OS. As workaround RxJava has a hook where you can set the global errorhandler. For more info see RxJava docs .
A default workaround implementation in the Flutter app (needs to be in the Java / Kotlin part e.g. mainactivity) is shown below. For an example (in Java) see Polidea RxAndroidBle sample.
BleException is coming from Polidea RxAndroidBle, so make sure your application declares the following depedency: implementation "com.polidea.rxandroidble2:rxandroidble:1.11.1"
RxJavaPlugins.setErrorHandler { throwable ->
if (throwable is UndeliverableException && throwable.cause is BleException) {
return@setErrorHandler // ignore BleExceptions since we do not have subscriber
}
else {
throw throwable
}
}
Hi @pmagnuson
To resolve the issue, you primarily need to add the necessary dependency in your android/app/build.gradle
file, like so:
android {
// Other configurations...
}
flutter {
source '../..'
}
dependencies {
// TODO: Add this dependency
implementation "com.polidea.rxandroidble2:rxandroidble:1.11.1"
}
Next, you'll need to update your Android´s main file
. In my case, I'm using Kotlin
(for Java the solution is in the README) , so your file might look something like this:
//TODO: Update it with your app information
package com.your_company.your_app
import io.flutter.embedding.android.FlutterActivity
import android.os.Bundle
import com.polidea.rxandroidble2.exceptions.BleException
import io.reactivex.exceptions.UndeliverableException
import io.reactivex.plugins.RxJavaPlugins
class MainActivity: FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
RxJavaPlugins.setErrorHandler { throwable ->
if (throwable is UndeliverableException && throwable.cause is BleException) {
// ignore BleExceptions since we do not have subscriber
return@setErrorHandler
} else {
throw throwable
}
}
}
}
Finally, after making these changes, clean your project to get the dependencies and then run it again.
flutter clean && flutter pub get && flutter run
The issue should now be resolved :octocat: 🔨
Describe the bug When the Bluetooth device disconnects, the Bluetooth stack has an undeliverable exception and the app crashes.
To Reproduce So far, this has only happened on Android, not on iOS.
Steps to reproduce the behavior:
connectTo()
Expected behavior The Bluetooth library should not crash and cause the app to crash.
Smartphone / tablet
Also on
Peripheral device
Additional context In the following logs, lines with
[log]
are from the application.successful connection
successful disconnect & reconnect
crash on disconnect