From 3e619a735e63a1222e71060d9e65b354a156b158 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Wed, 28 Jan 2015 23:45:38 +0100 Subject: o Major refactoring on the BtPromise, mainly internal. Renaming BtPromise to BtSequence and BtSequencer. --- .../main/java/io/trygvis/android/bt/BtDevice.java | 29 ++++++++++------------ 1 file changed, 13 insertions(+), 16 deletions(-) (limited to 'app/src/main/java/io/trygvis/android/bt/BtDevice.java') diff --git a/app/src/main/java/io/trygvis/android/bt/BtDevice.java b/app/src/main/java/io/trygvis/android/bt/BtDevice.java index b90ac4f..07e4e52 100644 --- a/app/src/main/java/io/trygvis/android/bt/BtDevice.java +++ b/app/src/main/java/io/trygvis/android/bt/BtDevice.java @@ -10,10 +10,9 @@ import android.util.Log; import java.util.Date; -import static io.trygvis.android.bt.BtPromise.BtBluetoothGattCallback; -import static io.trygvis.android.bt.BtPromise.PromiseResult.detour; -import static io.trygvis.android.bt.BtPromise.PromiseResult.fail; -import static io.trygvis.android.bt.BtPromise.PromiseResult.waitForNextEvent; +import static io.trygvis.android.bt.BtSequence.SequenceResult.detour; +import static io.trygvis.android.bt.BtSequence.SequenceResult.fail; +import static io.trygvis.android.bt.BtSequence.SequenceResult.waitForNextEvent; public class BtDevice implements Comparable { private final static String TAG = BtDevice.class.getSimpleName(); @@ -103,16 +102,16 @@ public class BtDevice implements Comparable { *

* Services will be discovered. */ - public synchronized void withConnection(BtPromise promise) { + public synchronized void withConnection(BtSequence sequence) { if (callback != null) { throw new RuntimeException("The current callback is not done."); } Log.i(TAG, "withConnection(), address=" + address + ", connected: " + (gatt != null)); - BtPromise newPromise; + BtSequence newSequence; if (gatt == null) { - newPromise = new BtPromise(). + newSequence = new BtSequence(). ignoreFailureForNext(). onConnectionStateChange((gatt, status, newState) -> { Log.i(TAG, "defaultConnectCallback: status=" + status + ", newState=" + newState); @@ -125,7 +124,7 @@ public class BtDevice implements Comparable { } else { Log.i(TAG, "Could not connect to " + address + ", trying again"); - return detour(new BtPromise().onConnectionStateChange((gatt2, status2, newState2) -> { + return detour(new BtSequence().onConnectionStateChange((gatt2, status2, newState2) -> { if (status2 == BluetoothGatt.GATT_SUCCESS && newState2 == BluetoothGatt.STATE_CONNECTED) { Log.i(TAG, "Connected to " + address + ", discovering services"); return gatt.discoverServices() ? waitForNextEvent() : fail(); @@ -139,25 +138,23 @@ public class BtDevice implements Comparable { }). onServicesDiscovered(gatt -> { Log.i(TAG, "Services discovered, has " + gatt.getServices().size() + " services"); - return detour(promise); + return detour(sequence); }); } else { - newPromise = promise; + newSequence = sequence; } - callback = newPromise. + BtSequencer sequencer = new BtSequencer(bluetoothDevice.getAddress(), newSequence. onFinally(success -> { Log.i(TAG, "Promise done, device is available again: address=" + address + ", success=" + success); callback = null; - }). - asCallback(bluetoothDevice.getAddress()); + })); + callback = new BtBluetoothGattCallback(sequencer); if (gatt == null) { gatt = bluetoothDevice.connectGatt(btService, false, wrappingCallback); } else { - callback.onEvent(BtPromise.EventType.onDirect, "", gatt, null, null, - BluetoothGatt.GATT_SUCCESS, - BluetoothGatt.STATE_CONNECTED); + sequencer.onDirect(); } } -- cgit v1.2.3