aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/io/trygvis/android
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/io/trygvis/android')
-rw-r--r--app/src/main/java/io/trygvis/android/bt/BtCallback.java20
-rw-r--r--app/src/main/java/io/trygvis/android/bt/BtDevice.java21
-rw-r--r--app/src/main/java/io/trygvis/android/bt/BtPromise.java (renamed from app/src/main/java/io/trygvis/android/bt/BtActionExecutor.java)136
-rw-r--r--app/src/main/java/io/trygvis/android/bt/DefaultBtService.java2
4 files changed, 120 insertions, 59 deletions
diff --git a/app/src/main/java/io/trygvis/android/bt/BtCallback.java b/app/src/main/java/io/trygvis/android/bt/BtCallback.java
index 29ea9e1..8dab149 100644
--- a/app/src/main/java/io/trygvis/android/bt/BtCallback.java
+++ b/app/src/main/java/io/trygvis/android/bt/BtCallback.java
@@ -4,6 +4,8 @@ import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
+import static io.trygvis.android.bt.BtPromise.*;
+
public class BtCallback {
public final String name;
@@ -11,39 +13,39 @@ public class BtCallback {
this.name = name;
}
- public boolean onConnectionStateChange(BluetoothGatt gatt, int newState) {
+ public PromiseResult onConnectionStateChange(BluetoothGatt gatt, int newState) {
throw new NotOverriddenException();
}
- public boolean onServicesDiscovered(BluetoothGatt gatt) {
+ public PromiseResult onServicesDiscovered(BluetoothGatt gatt) {
throw new NotOverriddenException();
}
- public boolean onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
+ public PromiseResult onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
throw new NotOverriddenException();
}
- public boolean onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
+ public PromiseResult onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
throw new NotOverriddenException();
}
- public boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
+ public PromiseResult onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
throw new NotOverriddenException();
}
- public boolean onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor) {
+ public PromiseResult onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor) {
throw new NotOverriddenException();
}
- public boolean onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor) {
+ public PromiseResult onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor) {
throw new NotOverriddenException();
}
- public boolean onReliableWriteCompleted(BluetoothGatt gatt) {
+ public PromiseResult onReliableWriteCompleted(BluetoothGatt gatt) {
throw new NotOverriddenException();
}
- public boolean onReadRemoteRssi(BluetoothGatt gatt, int rssi) {
+ public PromiseResult onReadRemoteRssi(BluetoothGatt gatt, int rssi) {
throw new NotOverriddenException();
}
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 edad522..1c7666e 100644
--- a/app/src/main/java/io/trygvis/android/bt/BtDevice.java
+++ b/app/src/main/java/io/trygvis/android/bt/BtDevice.java
@@ -1,6 +1,8 @@
package io.trygvis.android.bt;
import android.bluetooth.BluetoothDevice;
+import android.bluetooth.BluetoothGatt;
+import android.bluetooth.BluetoothGattCallback;
import android.util.Log;
import java.util.Date;
@@ -10,6 +12,7 @@ public class BtDevice<A extends BtDevice.BtDeviceWrapper<A>> {
private final DefaultBtService btService;
private final BluetoothDevice bluetoothDevice;
+ private BluetoothGatt gatt;
private Integer rssi;
private BtScanResult scanResult;
private A tag;
@@ -24,8 +27,8 @@ public class BtDevice<A extends BtDevice.BtDeviceWrapper<A>> {
}
BtDevice(DefaultBtService btService, BluetoothDevice bluetoothDevice,
- BtService.BtDbIntegration<A> btDbIntegration, long id, Integer rssi,
- BtScanResult scanResult, boolean seenBefore, Date firstSeen, Date lastSeen) {
+ BtService.BtDbIntegration<A> btDbIntegration, long id, Integer rssi,
+ BtScanResult scanResult, boolean seenBefore, Date firstSeen, Date lastSeen) {
this.btService = btService;
this.bluetoothDevice = bluetoothDevice;
this.tag = btDbIntegration.createTag(this);
@@ -73,12 +76,20 @@ public class BtDevice<A extends BtDevice.BtDeviceWrapper<A>> {
this.lastSeen = lastSeen;
}
- public boolean connect(BtActionExecutor executor) {
- Log.i(TAG, "connect(), address=" + bluetoothDevice.getAddress() + ", queue=" + executor);
- bluetoothDevice.connectGatt(btService, false, executor.asCallback());
+ public synchronized boolean connect(BtPromise executor) {
+ Log.i(TAG, "connect(), address=" + bluetoothDevice.getAddress());
+ BluetoothGattCallback callback = executor.asCallback();
+ gatt = bluetoothDevice.connectGatt(btService, false, callback);
return true;
}
+ public synchronized void disconnect() {
+ if (gatt != null) {
+ gatt.disconnect();
+ gatt = null;
+ }
+ }
+
public BtScanResult getScanResult() {
return scanResult;
}
diff --git a/app/src/main/java/io/trygvis/android/bt/BtActionExecutor.java b/app/src/main/java/io/trygvis/android/bt/BtPromise.java
index 984c738..55b6315 100644
--- a/app/src/main/java/io/trygvis/android/bt/BtActionExecutor.java
+++ b/app/src/main/java/io/trygvis/android/bt/BtPromise.java
@@ -15,114 +15,134 @@ import java.util.Queue;
import io.trygvis.android.F2;
import io.trygvis.android.Function;
-import static io.trygvis.android.bt.BtActionExecutor.EventType.onCharacteristicChanged;
-import static io.trygvis.android.bt.BtActionExecutor.EventType.onCharacteristicRead;
-import static io.trygvis.android.bt.BtActionExecutor.EventType.onCharacteristicWrite;
-import static io.trygvis.android.bt.BtActionExecutor.EventType.onConnectionStateChange;
-import static io.trygvis.android.bt.BtActionExecutor.EventType.onDescriptorRead;
-import static io.trygvis.android.bt.BtActionExecutor.EventType.onDescriptorWrite;
-import static io.trygvis.android.bt.BtActionExecutor.EventType.onFailure;
-import static io.trygvis.android.bt.BtActionExecutor.EventType.onFinally;
-import static io.trygvis.android.bt.BtActionExecutor.EventType.onReliableWriteCompleted;
-import static io.trygvis.android.bt.BtActionExecutor.EventType.onServicesDiscovered;
-
-public class BtActionExecutor {
- private final static String TAG = BtActionExecutor.class.getSimpleName();
+import static io.trygvis.android.bt.BtPromise.EventType.onCharacteristicChanged;
+import static io.trygvis.android.bt.BtPromise.EventType.onCharacteristicRead;
+import static io.trygvis.android.bt.BtPromise.EventType.onCharacteristicWrite;
+import static io.trygvis.android.bt.BtPromise.EventType.onConnectionStateChange;
+import static io.trygvis.android.bt.BtPromise.EventType.onDescriptorRead;
+import static io.trygvis.android.bt.BtPromise.EventType.onDescriptorWrite;
+import static io.trygvis.android.bt.BtPromise.EventType.onFailure;
+import static io.trygvis.android.bt.BtPromise.EventType.onFinally;
+import static io.trygvis.android.bt.BtPromise.EventType.onReliableWriteCompleted;
+import static io.trygvis.android.bt.BtPromise.EventType.onServicesDiscovered;
+
+public class BtPromise {
+ private final static String TAG = BtPromise.class.getSimpleName();
private final Queue<BtCallback> actionQ = new ArrayDeque<>();
private final Queue<BtCallback> failureQ = new ArrayDeque<>();
private final Queue<BtCallback> finallyQ = new ArrayDeque<>();
- public synchronized BtActionExecutor onConnectionStateChange(F2<BluetoothGatt, Integer, Boolean> callback) {
+ public static final PromiseResult continueDownChain = new Continue();
+ public static final PromiseResult doneWithChain = new Done();
+
+ public abstract static class PromiseResult {
+ }
+
+ private static class Continue extends PromiseResult {
+ }
+
+ private static class Done extends PromiseResult {
+ }
+
+ private static class Detour extends PromiseResult {
+ final BtPromise promise;
+
+ private Detour(BtPromise promise) {
+ this.promise = promise;
+ }
+ }
+
+ public synchronized BtPromise onConnectionStateChange(F2<BluetoothGatt, Integer, PromiseResult> callback) {
actionQ.add(new BtCallback("onConnectionStateChange") {
@Override
- public boolean onConnectionStateChange(BluetoothGatt gatt, int newState) {
+ public PromiseResult onConnectionStateChange(BluetoothGatt gatt, int newState) {
return callback.apply(gatt, newState);
}
});
return this;
}
- public synchronized BtActionExecutor onServicesDiscovered(Function<BluetoothGatt, Boolean> callback) {
+ public synchronized BtPromise onServicesDiscovered(Function<BluetoothGatt, PromiseResult> callback) {
actionQ.add(new BtCallback("onServicesDiscovered") {
@Override
- public boolean onServicesDiscovered(BluetoothGatt gatt) {
+ public PromiseResult onServicesDiscovered(BluetoothGatt gatt) {
return callback.apply(gatt);
}
});
return this;
}
- public synchronized BtActionExecutor onCharacteristicRead(F2<BluetoothGatt, BluetoothGattCharacteristic, Boolean> callback) {
+ public synchronized BtPromise onCharacteristicRead(F2<BluetoothGatt, BluetoothGattCharacteristic, PromiseResult> callback) {
actionQ.add(new BtCallback("onCharacteristicRead") {
@Override
- public boolean onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
+ public PromiseResult onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
return callback.apply(gatt, characteristic);
}
});
return this;
}
- public synchronized BtActionExecutor onCharacteristicWrite(F2<BluetoothGatt, BluetoothGattCharacteristic, Boolean> callback) {
+ public synchronized BtPromise onCharacteristicWrite(F2<BluetoothGatt, BluetoothGattCharacteristic, PromiseResult> callback) {
actionQ.add(new BtCallback("onCharacteristicWrite") {
@Override
- public boolean onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
+ public PromiseResult onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
return callback.apply(gatt, characteristic);
}
});
return this;
}
- public synchronized BtActionExecutor onCharacteristicChanged(F2<BluetoothGatt, BluetoothGattCharacteristic, Boolean> callback) {
+ public synchronized BtPromise onCharacteristicChanged(F2<BluetoothGatt, BluetoothGattCharacteristic, PromiseResult> callback) {
actionQ.add(new BtCallback("onCharacteristicChanged") {
@Override
- public boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
+ public PromiseResult onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
return callback.apply(gatt, characteristic);
}
});
return this;
}
- public synchronized BtActionExecutor onDescriptorRead(F2<BluetoothGatt, BluetoothGattDescriptor, Boolean> callback) {
+ public synchronized BtPromise onDescriptorRead(F2<BluetoothGatt, BluetoothGattDescriptor, PromiseResult> callback) {
actionQ.add(new BtCallback("onDescriptorRead") {
@Override
- public boolean onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor) {
+ public PromiseResult onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor) {
return callback.apply(gatt, descriptor);
}
});
return this;
}
- public synchronized BtActionExecutor onDescriptorWrite(F2<BluetoothGatt, BluetoothGattDescriptor, Boolean> callback) {
+ public synchronized BtPromise onDescriptorWrite(F2<BluetoothGatt, BluetoothGattDescriptor, PromiseResult> callback) {
actionQ.add(new BtCallback("onDescriptorWrite") {
@Override
- public boolean onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor) {
+ public PromiseResult onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor) {
return callback.apply(gatt, descriptor);
}
});
return this;
}
- public synchronized BtActionExecutor onReliableWriteCompleted(Function<BluetoothGatt, Boolean> callback) {
+ public synchronized BtPromise onReliableWriteCompleted(Function<BluetoothGatt, PromiseResult> callback) {
actionQ.add(new BtCallback("onReliableWriteCompleted") {
@Override
- public boolean onReliableWriteCompleted(BluetoothGatt gatt) {
+ public PromiseResult onReliableWriteCompleted(BluetoothGatt gatt) {
return callback.apply(gatt);
}
});
return this;
}
- public synchronized BtActionExecutor onReadRemoteRssi(F2<BluetoothGatt, Integer, Boolean> callback) {
+ public synchronized BtPromise onReadRemoteRssi(F2<BluetoothGatt, Integer, PromiseResult> callback) {
actionQ.add(new BtCallback("onReadRemoteRssi") {
@Override
- public boolean onReadRemoteRssi(BluetoothGatt gatt, int rssi) {
+ public PromiseResult onReadRemoteRssi(BluetoothGatt gatt, int rssi) {
return callback.apply(gatt, rssi);
}
});
return this;
}
- public synchronized BtActionExecutor onFailure(Runnable callback) {
+ public synchronized BtPromise onFailure(Runnable callback) {
failureQ.add(new BtCallback("onFailure") {
@Override
public void onFailure() {
@@ -132,7 +152,7 @@ public class BtActionExecutor {
return this;
}
- public synchronized BtActionExecutor onFinally(Runnable callback) {
+ public synchronized BtPromise onFinally(Runnable callback) {
finallyQ.add(new BtCallback("finally") {
@Override
public void onFinally() {
@@ -160,8 +180,12 @@ public class BtActionExecutor {
return s.toString();
}
- public BluetoothGattCallback asCallback() {
- return new MyBluetoothGattCallback();
+ BtBluetoothGattCallback asCallback() {
+ return new BtBluetoothGattCallback(actionQ, failureQ, finallyQ);
+ }
+
+ public PromiseResult toDetour() {
+ return new Detour(this);
}
enum EventType {
@@ -180,7 +204,9 @@ public class BtActionExecutor {
onFinally,
}
- private static Boolean callCallback(EventType key, BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, BluetoothGattDescriptor descriptor, int newState, BtCallback btCallback) {
+ private static PromiseResult callCallback(EventType key, BluetoothGatt gatt,
+ BluetoothGattCharacteristic characteristic,
+ BluetoothGattDescriptor descriptor, int newState, BtCallback btCallback) {
switch (key) {
case onConnectionStateChange:
return btCallback.onConnectionStateChange(gatt, newState);
@@ -211,16 +237,25 @@ public class BtActionExecutor {
}
}
- private class MyBluetoothGattCallback extends BluetoothGattCallback {
+ static class BtBluetoothGattCallback extends BluetoothGattCallback {
- private Queue<BtCallback> initialActionQ = new ArrayDeque<>(actionQ);
+ private ArrayDeque<BtCallback> actionQ;
+ private ArrayDeque<BtCallback> failureQ;
+ ArrayDeque<BtCallback> finallyQ;
+// private ArrayDeque<BtCallback> initialActionQ;
private List<String> events = new ArrayList<>();
+ private BtBluetoothGattCallback(Queue<BtCallback> actionQ, Queue<BtCallback> failureQ, Queue<BtCallback> finallyQ) {
+ this.actionQ = new ArrayDeque<>(actionQ);
+ this.failureQ = new ArrayDeque<>(failureQ);
+ this.finallyQ = new ArrayDeque<>(finallyQ);
+ }
+
void onEvent(EventType key, String values, BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, BluetoothGattDescriptor descriptor, int status, int newState) {
boolean success = status == BluetoothGatt.GATT_SUCCESS;
- events.add(key + ", " + values + ", success=" + success);
+ events.add(key + "(" + values + "), success=" + success);
- Log.i(TAG, "Operation failed: " + key + ", " + values + ", success=" + success);
+ Log.i(TAG, key + "(" + values + "), success=" + success);
if (!success) {
doFailure();
@@ -246,11 +281,24 @@ public class BtActionExecutor {
}
try {
- boolean ok = callCallback(key, gatt, characteristic, descriptor, newState, btCallback);
+ PromiseResult result = callCallback(key, gatt, characteristic, descriptor, newState, btCallback);
- if (!ok) {
- Log.w(TAG, "The callback don't want to continue.");
+ if (result instanceof Done) {
+ Log.i(TAG, "The chain is done.");
doFinally();
+ } else if (result instanceof Detour) {
+ Log.i(TAG, "Adding detour");
+ BtPromise promise = ((Detour) result).promise;
+ if (!promise.failureQ.isEmpty()) {
+ Log.i(TAG, "Ignoring " + promise.failureQ.size() + " items from the failure Q");
+ }
+ if (!promise.finallyQ.isEmpty()) {
+ Log.i(TAG, "Ignoring " + promise.finallyQ.size() + " items from the finally Q");
+ }
+
+ for (BtCallback callback : promise.actionQ) {
+ actionQ.addFirst(callback);
+ }
}
if (actionQ.isEmpty()) {
@@ -266,7 +314,7 @@ public class BtActionExecutor {
StringBuilder msg = new StringBuilder();
msg.append("Expected events: \n");
- for (BtCallback cb : initialActionQ) {
+ for (BtCallback cb : actionQ) {
msg.append(" ").append(cb.name).append("\n");
}
diff --git a/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java b/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java
index 205421b..80a195a 100644
--- a/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java
+++ b/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java
@@ -45,7 +45,7 @@ public class DefaultBtService<A extends BtDevice.BtDeviceWrapper<A>> extends Ser
private BtDbIntegration<A> btDbIntegration;
- private BluetoothManager bluetoothManager;
+ BluetoothManager bluetoothManager;
private BluetoothAdapter bluetoothAdapter;