0

My first attempt at using RxAndroidBle. Scanning for a single instance of a specific kind of device:

    static boolean scanForDevice() { 
    asScanSubscription = asBleClient 
            .scanBleDevices( asServiceId ) // returns Observable<RxBleScanResult>
            .first() 
            .map( rxBleScanResult -> rxBleScanResult.getBleDevice() ) // returns Observable of the one device
            .subscribe( bleDevice -> asBleDevice = bleDevice, // save in a variable
                        throwable -> { throw new BleScanException( 0 ); },
                        ( ) -> { throw new BleServiceNotFoundException( asServiceId ); } // didn't find it
                      );
    return true; 
}

My problem is in the "throwable" code for the subscribe call. I would like to be able to throw a BleScanException, but I don't know where to get the correct integer parameter describing the reason for the error. How can I find this out?

In general, where can I get details about the specific Throwable that is passed to a subscriber on errors?

Would appreciate any additional comments about this approach.

Robert Lewis
  • 1,847
  • 2
  • 18
  • 43

1 Answers1

0

First of all - BleScanException is used by the library and should not be instantiated outside of it. Unfortunately java doesn't allow to hide the constructor so it won't be visible from outside the library.

You can find out about the BleScanException simply by looking into the source files.

At last - why do you even need to create a BleScanException? It will be created for you and passed to onError(). You should handle the exception in this place.

Dariusz Seweryn
  • 3,212
  • 2
  • 14
  • 21
  • Thanks, @s_noopy. I tried `throw throwable` as you recommend, but I get an error: _Unhandled exception: java.lang.Throwable_. Adding `throws Throwable` to the method declaration doesn't fix the error. Also, is it OK to instantiate `BleServiceNotFoundException` as I've done? Is there a better approach? – Robert Lewis Jan 22 '17 at 21:59
  • Every exception from the library was created to notify external user about issues - not to instantiate it outside. I have advised you wrongly - you should handle the throwable where you get it (I have edited the answer) – Dariusz Seweryn Jan 22 '17 at 22:33
  • OK, what I have now is `throwable -> { throw new RuntimeException( "Error scanning for device: " + throwable.getMessage(), throwable ); }, // ` Does that make sense? – Robert Lewis Jan 23 '17 at 01:11
  • Why can't you return `Observable scanForDevice()` and process the `Throwable` where you know what to do with it? You are mixing reactive and standard java flows. – Dariusz Seweryn Jan 23 '17 at 07:41
  • I'm sorry, I don't understand your last question. Could you explain? – Robert Lewis Jan 25 '17 at 04:40
  • Instead of subscribing to the observable inside the `scanForDevice()` function you could return an `Observable` from it and subscribe in the part of the code you will use the device instead of passing it through a variable `asBleDevice`. The code you have attached in the original post will most probably crash the application if any error will happen during a scan. – Dariusz Seweryn Jan 25 '17 at 19:36