PostLollipopEstimoteScanner.kt line 29

I am facing scan error issue on
(Lenovo, Motorola and Nokia) Android Device. Every time when i setup the zone after few second app crashes.

Fatal Exception: io.reactivex.exceptions.UndeliverableException
java.lang.Exception: Bluetooth Low Energy scan failed with error code: 2
keyboard_arrow_up

io.reactivex.plugins.RxJavaPlugins.onError (RxJavaPlugins.java:367)
io.reactivex.internal.operators.observable.ObservableRefCount$RefCountObserver.onError (ObservableRefCount.java:198)
io.reactivex.internal.operators.observable.ObservablePublish$PublishObserver.onError (ObservablePublish.java:182)
io.reactivex.internal.observers.BasicFuseableObserver.onError (BasicFuseableObserver.java:100)
io.reactivex.internal.observers.BasicFuseableObserver.onError (BasicFuseableObserver.java:100)
io.reactivex.internal.operators.observable.ObservableRefCount$RefCountObserver.onError (ObservableRefCount.java:196)
io.reactivex.internal.operators.observable.ObservablePublish$PublishObserver.onError (ObservablePublish.java:182)
io.reactivex.internal.operators.observable.ObservableCreate$CreateEmitter.tryOnError (ObservableCreate.java:85)
io.reactivex.internal.operators.observable.ObservableCreate$CreateEmitter.onError (ObservableCreate.java:73)
com.estimote.proximity_sdk.internals.monitoring.estimote_monitoring.SimpleEstimoteMonitor$startEstimoteLocationScan$1$2.invoke (SimpleEstimoteMonitor.kt:45)
com.estimote.proximity_sdk.internals.monitoring.estimote_monitoring.SimpleEstimoteMonitor$startEstimoteLocationScan$1$2.invoke (SimpleEstimoteMonitor.kt:17)
com.estimote.scanning_plugin.api.use_cases.ScanUseCase$notifyUserCallbacks$2.accept (ScanUseCase.kt:28)
com.estimote.scanning_plugin.api.use_cases.ScanUseCase$notifyUserCallbacks$2.accept (ScanUseCase.kt:8)
io.reactivex.internal.observers.LambdaObserver.onError (LambdaObserver.java:77)
io.reactivex.internal.observers.BasicFuseableObserver.onError (BasicFuseableObserver.java:100)
io.reactivex.internal.observers.BasicFuseableObserver.onError (BasicFuseableObserver.java:100)
io.reactivex.internal.observers.BasicFuseableObserver.onError (BasicFuseableObserver.java:100)
io.reactivex.internal.observers.BasicFuseableObserver.onError (BasicFuseableObserver.java:100)

Ahh, the Android BLE error 2, let’s talk exactly about what it is:

Android Bluetooth stack supports limited amount of apps/scanners using Bluetooth. The exact number differs between smartphones/manufacturers, but usually, this limit is high enough to never cause problems.

Unless…:

  1. Your app doesn’t stop scanning properly (rule of thumb: you should have a “stop” for every “startObserving”). This means that over time, you start more and more scanners, and eventually hit the limit, and get this “error code 2”.

  2. Your app gets force-stopped a lot. When your app gets force-stopped, it doesn’t get a chance to properly stop/unregister its Bluetooth scanners, which means we run into the same problem as #1 — over time, we’ll hit the limit and get the “error code 2”.

Here’s the catch with #2 => force-stops usually happen all the time in development, because each time you hit “run” in Android Studio, the current instance of the app gets force-stopped and the new build gets deployed.

Restarting Bluetooth “cleans” the registrations, so it’s a common remedy for this error.

Also from my experience, some Android smartphones will actually “clean up” the Bluetooth scanners when the app gets force-stopped, which means they’re not affected by #2.

All in all, assuming you properly manage your start/stop cycles, the “error code 2” shouldn’t affect you in production. (Though it is an annoyance during development.)

PS Specifically, error code 2 = SCAN_FAILED_APPLICATION_REGISTRATION_FAILED. In my experience, hitting the limit of scanners is the most common reason for this error, and that’s what this post is about. However, it’s worth keeping in mind that there might also be other conditions that trigger this error, and then this post wouldn’t apply.