PhilipsHue / flutter_reactive_ble

Flutter library that handles BLE operations for multiple devices.
https://developers.meethue.com/
Other
669 stars 336 forks source link

BLE undeliverable exception - need more documentation on how to implement it #345

Closed chris-otani closed 3 years ago

chris-otani commented 3 years ago

I'm struggling to add the workaround for the BLE undeliverable exception to my flutter project. I have no Java/Kotlin experience and searched but can't figure out how to properly add it.

I've added the code to my MainActivity.kt but am getting errors. Could you provide more detail on where to add this code?

error: expecting member decleration

2021-08-19_16-54-21

When I try to run it I get:

Unresolved reference: throwable FAILURE: Build failed with an exception.

CODE:

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
    RxJavaPlugins.setErrorHandler { throwable ->
        if (throwable is UndeliverableException && throwable.cause is BleException) {
            return@setErrorHandler // ignore BleExceptions since we do not have subscriber
        }
        else {
            throw throwable
        }
    }
}

Any help would be greatly appreciated.

remonh87 commented 3 years ago

Do not forget to add

implementation "com.polidea.rxandroidble2:rxandroidble:1.11.1" to your gradle dependencies and fix the imports

chris-otani commented 3 years ago

@remonh87 , thanks for the quick response.

I added that to my gradle dependicies. I also added rxkotlin since it couldn't find that package.

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "com.polidea.rxandroidble2:rxandroidble:1.11.1"
    implementation 'io.reactivex.rxjava2:rxkotlin:2.4.0'
}

When fixing the imports it looks like It's not properly importing the "RxJavaPlugins". the import is greyed out like its not being used

MainActivity.kt

import io.flutter.embedding.android.FlutterActivity

import com.polidea.rxandroidble2.exceptions.BleException
import io.reactivex.exceptions.UndeliverableException
import io.reactivex.plugins.RxJavaPlugins

class MainActivity: FlutterActivity() {
    RxJavaPlugins.setErrorHandler { throwable ->
        if (throwable is UndeliverableException && throwable.cause is BleException) {
            return@setErrorHandler // ignore BleExceptions since we do not have subscriber
        }
        else {
            throw throwable
        }
    }
}

2021-08-20_09-37-23

Here are the static errors:

2021-08-20_09-36-23

keval-devani commented 3 years ago

Just change your code like this in your MainActivity

import android.os.Bundle
import com.polidea.rxandroidble2.exceptions.BleException
import io.flutter.embedding.android.FlutterActivity
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) {
                return@setErrorHandler // ignore BleExceptions since we do not have subscriber
            } else {
                throw throwable
            }
        }
    }
}
chris-otani commented 3 years ago

@keval-devani That did the trick. Thank you!