0

Maybe I'm not understanding the situation correctly, but we're told it's important to have only one instance of the RxBleClient.

Couldn't this be easily accomplished by making it a static member of the Application class?

class MyApp extends Application {
    static RxBleClient rxBleClient; 
    ...
}

Also, I'm having a hard time understanding this code (from your Application class):

   public static RxBleClient getRxBleClient(Context context) {
       RxBleApp application = (RxBleApp) context.getApplicationContext();
       return application.rxBleClient;
   }

Could you help me understand what it's doing, and why? Why couldn't it simply

return rxBleClient;
Robert Lewis
  • 1,847
  • 2
  • 18
  • 43

1 Answers1

0

It can be accomplished with taking the static member of the class. Tough it is more elegant to pass it through the instance of RxBleApp which make it more testable (if there were any tests for the sample part).

The RxBleClient is referenced in the RxBleApp which is the android Application. The application context can be retrieved from the Context.

Dariusz Seweryn
  • 3,212
  • 2
  • 14
  • 21
  • I am refactoring and planning to put the BLE code into a Bound Service. Is this reasonable? The idea: in my new BluetoothService class, put this: `static RxBleClient mBleClient;`. And in its onCreate(), put `mBleClient = RxBleClient.create(this);`. Then add a method `public static RxBleClient getRxBleClient() { return mBleClient; }`. If I understand, when I bind a Service I am given a reference to an _instance_ of it, but it is OK to call its static methods with this reference. I still don't fully understand why you create and then discard a new instance of the Application. Could you explain? – Robert Lewis Feb 07 '17 at 17:06