aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/io/trygvis/android/bt/BtDevice.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/io/trygvis/android/bt/BtDevice.java')
-rw-r--r--app/src/main/java/io/trygvis/android/bt/BtDevice.java29
1 files changed, 13 insertions, 16 deletions
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<A> implements Comparable<BtDevice> {
private final static String TAG = BtDevice.class.getSimpleName();
@@ -103,16 +102,16 @@ public class BtDevice<A> implements Comparable<BtDevice> {
* <p>
* 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<A> implements Comparable<BtDevice> {
} 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<A> implements Comparable<BtDevice> {
}).
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();
}
}