diff options
Diffstat (limited to 'app/src/main/java/io/trygvis/soilmoisture')
-rw-r--r-- | app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java | 37 |
1 files changed, 4 insertions, 33 deletions
diff --git a/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java b/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java index 3ff6e66..174c245 100644 --- a/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java +++ b/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java @@ -30,8 +30,6 @@ import io.trygvis.android.bt.BtService; import io.trygvis.android.bt.DefaultBtService; import io.trygvis.bluetooth.TrygvisIoUuids; -import static io.trygvis.android.bt.BtPromise.PromiseResult.continueDirectly; -import static io.trygvis.android.bt.BtPromise.PromiseResult.detour; import static io.trygvis.android.bt.BtPromise.PromiseResult.stop; import static io.trygvis.android.bt.BtPromise.PromiseResult.waitForNextEvent; import static io.trygvis.android.bt.BtService.BtServiceListenerBroadcastReceiver; @@ -117,8 +115,6 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS if (!smDevice.isProbed()) { Log.i(TAG, "Probing " + address + ", name=" + btDevice.getName()); BtPromise executor = new BtPromise(). - ignoreFailureForNext(). - onConnectionStateChange(DefaultSoilMoistureService::defaultConnectCallback). onDirect(v -> { BluetoothGatt gatt = (BluetoothGatt) v; return gatt.discoverServices() ? waitForNextEvent() : stop(); @@ -162,15 +158,14 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS return stop(); }). onFinally(() -> { - btDevice.disconnect(); +// btDevice.disconnect(); if (smDevice.getIsUseful() == null) { smDevice.setIsUseful(false); } - }); - btDevice.connect(executor); + btDevice.withConnection(executor); } } }; @@ -208,29 +203,6 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS }); } - public static BtPromise.PromiseResult defaultConnectCallback(BluetoothGatt gatt, int status, Integer newState) { - Log.i(TAG, "defaultConnectCallback: status=" + status + ", newState=" + newState); - String address = gatt.getDevice().getAddress(); - //noinspection SimplifiableIfStatement - if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothGatt.STATE_CONNECTED) { - Log.i(TAG, "Connected to " + address); - return continueDirectly(gatt); - } else { - Log.i(TAG, "Disconnected from " + address + ", trying again"); - - return detour(new BtPromise().onConnectionStateChange((gatt2, status2, newState2) -> { - if (newState2 == BluetoothGatt.STATE_CONNECTED) { - Log.i(TAG, "Connected to " + address); - return continueDirectly(gatt); - } - - Log.i(TAG, "Could still not connect to " + address + ", failing."); - - return stop(); - })); - } - } - // ----------------------------------------------------------------------- // SmDevicesManager Implementation // ----------------------------------------------------------------------- @@ -300,7 +272,6 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS void readCurrentValue(SmSensor sensor) { BtPromise promise = new BtPromise(). - onConnectionStateChange(DefaultSoilMoistureService::defaultConnectCallback). onDirect(v -> { BluetoothGatt gatt = (BluetoothGatt) v; return gatt.discoverServices() ? waitForNextEvent() : stop(); @@ -327,14 +298,14 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS handleNewSensorValueReady(sensor, getSensorCountRes.value); - gatt.disconnect(); +// gatt.disconnect(); return stop(); }). onFinally(() -> { }); - sensor.getDevice().getBtDevice().connect(promise); + sensor.getDevice().getBtDevice().withConnection(promise); } // ----------------------------------------------------------------------- |