diff --git a/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRuffyService.aidl b/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRuffyService.aidl index 3baa4116e1..ded119c7b4 100644 --- a/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRuffyService.aidl +++ b/app/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRuffyService.aidl @@ -6,9 +6,22 @@ import org.monkey.d.ruffy.ruffy.driver.IRTHandler; interface IRuffyService { - void setHandler(IRTHandler handler); + void addHandler(IRTHandler handler); + void removeHandler(IRTHandler handler); + + /** Connect to the pump + * + * @return 0 if successful, -1 otherwise + */ int doRTConnect(); + + /** Disconnect from the pump */ void doRTDisconnect(); + + /*What's the meaning of 'changed'? + * changed means if a button state has been changed, like btton pressed is a change and button release another*/ void rtSendKey(byte keyCode, boolean changed); void resetPairing(); + boolean isConnected(); + boolean isBoundToPump(); } diff --git a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java index 1da4b681f4..72fe99c75d 100644 --- a/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java +++ b/app/src/main/java/de/jotomo/ruffyscripter/RuffyScripter.java @@ -48,16 +48,36 @@ public class RuffyScripter { private volatile boolean connected = false; private volatile long lastDisconnected = 0; + private boolean started = false; + public RuffyScripter(final IRuffyService ruffyService) { this.ruffyService = ruffyService; + } + + public void start() { try { - ruffyService.setHandler(mHandler); + ruffyService.addHandler(mHandler); idleDisconnectMonitorThread.start(); + started = true; } catch (RemoteException e) { throw new RuntimeException(e); } } + public void stop() { + if (started) { + try { + ruffyService.removeHandler(mHandler); + } catch (RemoteException e) { + log.warn("Removing IRTHandler from Ruffy service failed, ignoring", e); + } + } + } + + public boolean isRunning() { + return started; + } + private Thread idleDisconnectMonitorThread = new Thread(new Runnable() { @Override public void run() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java index 83e155075f..f714ef7bff 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java @@ -16,7 +16,6 @@ import android.support.v4.app.NotificationCompat; import com.squareup.otto.Subscribe; -import org.json.JSONException; import org.json.JSONObject; import org.monkey.d.ruffy.ruffy.driver.IRuffyService; import org.slf4j.Logger; @@ -189,11 +188,14 @@ public class ComboPlugin implements PluginBase, PumpInterface { @Override public void onServiceConnected(ComponentName name, IBinder service) { ruffyScripter = new RuffyScripter(IRuffyService.Stub.asInterface(service)); + ruffyScripter.start(); log.debug("ruffy serivce connected"); } @Override public void onServiceDisconnected(ComponentName name) { + ruffyScripter.stop(); + ruffyScripter = null; log.debug("ruffy service disconnected"); } };