From 991620666551c1eaf655a0aa0b1fe08d2bc19681 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 30 Jan 2015 18:54:26 +0100 Subject: o This variant actually work on a device. --- .../main/java/io/trygvis/android/bt/BtDevice.java | 4 +- .../io/trygvis/android/bt/DefaultBtService.java | 7 +++ .../android/bt/BtBluetoothGattCallback.java | 9 +-- .../java/io/trygvis/android/bt/BtSequencer.java | 69 +++++++++++++++------- .../main/java/android/bluetooth/BluetoothGatt.java | 2 +- .../src/main/java/android/util/Log.java | 20 ------- 6 files changed, 63 insertions(+), 48 deletions(-) delete mode 100644 trygvis-io-bt-stubs/src/main/java/android/util/Log.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 07e4e52..50fe707 100644 --- a/app/src/main/java/io/trygvis/android/bt/BtDevice.java +++ b/app/src/main/java/io/trygvis/android/bt/BtDevice.java @@ -107,7 +107,7 @@ public class BtDevice implements Comparable { throw new RuntimeException("The current callback is not done."); } - Log.i(TAG, "withConnection(), address=" + address + ", connected: " + (gatt != null)); + Log.i(TAG, "withConnection(), address=" + address + ", connected: " + connected()); BtSequence newSequence; if (gatt == null) { @@ -154,7 +154,7 @@ public class BtDevice implements Comparable { if (gatt == null) { gatt = bluetoothDevice.connectGatt(btService, false, wrappingCallback); } else { - sequencer.onDirect(); + sequencer.onDirect(gatt); } } 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 2487bd8..fc1819c 100644 --- a/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java +++ b/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java @@ -57,6 +57,13 @@ public class DefaultBtService extends Service implements BtService { private Scanner scanner = new Scanner(); + static { + BtSequencer.d = msg -> Log.d(BtSequencer.TAG, msg); + BtSequencer.i = msg -> Log.i(BtSequencer.TAG, msg); + BtSequencer.w = msg -> Log.w(BtSequencer.TAG, msg); + BtSequencer.we = pair -> Log.w(BtSequencer.TAG, pair.getKey(), pair.getValue()); + } + // ----------------------------------------------------------------------- // BtService Implementation // ----------------------------------------------------------------------- diff --git a/bt/src/main/java/io/trygvis/android/bt/BtBluetoothGattCallback.java b/bt/src/main/java/io/trygvis/android/bt/BtBluetoothGattCallback.java index 1f4921f..1ee538c 100644 --- a/bt/src/main/java/io/trygvis/android/bt/BtBluetoothGattCallback.java +++ b/bt/src/main/java/io/trygvis/android/bt/BtBluetoothGattCallback.java @@ -11,6 +11,7 @@ import static io.trygvis.android.bt.BtSequencer.EventType.onCharacteristicWrite; import static io.trygvis.android.bt.BtSequencer.EventType.onConnectionStateChange; import static io.trygvis.android.bt.BtSequencer.EventType.onDescriptorRead; import static io.trygvis.android.bt.BtSequencer.EventType.onDescriptorWrite; +import static io.trygvis.android.bt.BtSequencer.EventType.onReadRemoteRssi; import static io.trygvis.android.bt.BtSequencer.EventType.onReliableWriteCompleted; import static io.trygvis.android.bt.BtSequencer.EventType.onServicesDiscovered; @@ -66,8 +67,8 @@ public class BtBluetoothGattCallback extends BluetoothGattCallback { sequencer.onEvent(onReliableWriteCompleted, "status=" + status, gatt, null, null, status, 0); } -// @Override -// public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) { -// sequencer.onEvent(onReadRemoteRssi, "status=" + status, gatt, null, null, status, 0); -// } + @Override + public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) { + sequencer.onEvent(onReadRemoteRssi, "status=" + status, gatt, null, null, status, 0); + } } \ No newline at end of file diff --git a/bt/src/main/java/io/trygvis/android/bt/BtSequencer.java b/bt/src/main/java/io/trygvis/android/bt/BtSequencer.java index 7eecbed..f0863ae 100644 --- a/bt/src/main/java/io/trygvis/android/bt/BtSequencer.java +++ b/bt/src/main/java/io/trygvis/android/bt/BtSequencer.java @@ -3,13 +3,16 @@ package io.trygvis.android.bt; import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattDescriptor; -import android.util.Log; +import java.util.AbstractMap; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Deque; import java.util.Iterator; import java.util.List; +import java.util.Map; + +import io.trygvis.android.Consumer; import static io.trygvis.android.bt.BtSequence.ContinueDirectly; import static io.trygvis.android.bt.BtSequence.Detour; @@ -20,7 +23,7 @@ import static io.trygvis.android.bt.BtSequencer.EventType.onDirect; import static io.trygvis.android.bt.BtSequencer.EventType.onFinally; class BtSequencer { - private final static String TAG = BtSequencer.class.getSimpleName(); + public final static String TAG = BtSequencer.class.getSimpleName(); enum EventType { onConnectionStateChange, @@ -39,6 +42,14 @@ class BtSequencer { onFinally, } + public static Consumer d = System.out::println; + public static Consumer i = System.out::println; + public static Consumer w = System.out::println; + public static Consumer> we = pair -> { + System.out.println(pair.getKey()); + pair.getValue().printStackTrace(System.out); + }; + private final String address; private final Deque sequences = new ArrayDeque<>(); private final Deque> iterators = new ArrayDeque<>(); @@ -69,8 +80,8 @@ class BtSequencer { this.callbacks = sequence.actionQ(); } - public void onDirect() { - onEvent(onDirect, "initial onDirect", null, null, null, BluetoothGatt.GATT_SUCCESS, + public void onDirect(BluetoothGatt gatt) { + onEvent(onDirect, "initial onDirect", gatt, null, null, BluetoothGatt.GATT_SUCCESS, BluetoothGatt.STATE_CONNECTED); } @@ -78,19 +89,19 @@ class BtSequencer { BluetoothGattCharacteristic characteristic, BluetoothGattDescriptor descriptor, int status, int newState) { if (finallyDone) { - Log.w(TAG, "Got event after finally has been executed: " + key + "(" + values + ")."); + w("Got event after finally has been executed: " + key + "(" + values + ")."); return; } boolean success = status == BluetoothGatt.GATT_SUCCESS; events.add(key + "(" + values + "), success=" + success); - Log.i(TAG, "event: " + key + "(" + values + "), success=" + success); + i("event: " + key + "(" + values + "), success=" + success); BtCallback callback; synchronized (this) { if (!callbacks.hasNext() && sequence.getNext().isPresent()) { - Log.i(TAG, "Switching to next sequence"); + i("Switching to next sequence"); doFinally(success, sequence); @@ -100,11 +111,11 @@ class BtSequencer { if (!callbacks.hasNext()) { if (!sequences.isEmpty()) { - Log.d(TAG, "Sequence is done, continuing on previous sequence"); + d("Sequence is done, continuing on previous sequence"); doFinally(true, sequence); pop(); } else { - Log.d(TAG, "Sequence is done, no more sequences"); + d("Sequence is done, no more sequences"); doFinally(true); return; } @@ -117,31 +128,31 @@ class BtSequencer { doFinally(false); return; } else { - Log.i(TAG, "Last status was a failure, but the callback still want it."); + i("Last status was a failure, but the callback still want it."); } } } try { - Log.i(TAG, "Executing bt action: " + callback.type); + i("Executing bt action: " + callback.type); SequenceResult result = callCallback(key, gatt, characteristic, descriptor, status, newState, null, callback); if (result instanceof Stop) { - Log.i(TAG, "The sequence stopped."); + i("The sequence stopped."); doFinally(true); return; } if (result instanceof Fail) { - Log.i(TAG, "The sequence failed."); + i("The sequence failed."); doFinally(false); return; } if (result instanceof Detour) { BtSequence detour = ((Detour) result).sequence; - Log.i(TAG, "Adding detour: " + detour); + i("Adding detour: " + detour); if (!detour.firstIsOnDirect()) { throw new IllegalArgumentException("The first handler in a detour must be an onDirect."); @@ -161,20 +172,20 @@ class BtSequencer { if (!callbacks.hasNext()) { if (!sequences.isEmpty()) { - Log.d(TAG, "Sequence is done, continuing on previous sequence"); + d("Sequence is done, continuing on previous sequence"); doFinally(true, sequence); pop(); } else { - Log.d(TAG, "Sequence is done, no more sequences"); + d("Sequence is done, no more sequences"); doFinally(true); } } } catch (NotOverriddenException e) { - Log.w(TAG, "Unexpected callback by listener: " + key); + w("Unexpected callback by listener: " + key); // doFailure(); doFinally(false); } catch (Exception e) { - Log.w(TAG, "Exception in callback", e); + w("Exception in callback", e); // doFailure(); doFinally(false); } @@ -194,7 +205,7 @@ class BtSequencer { private void doFinally(boolean success, BtSequence s) { List q = s.finallyQ(); - Log.w(TAG, "Executing " + q.size() + " finally handlers, success=" + success); + w("Executing " + q.size() + " finally handlers, success=" + success); for (BtCallback callback : q) { try { callCallback(onFinally, null, null, null, 0, 0, success, callback); @@ -224,7 +235,7 @@ class BtSequencer { msg.append("- ").append(event).append("\n"); } - Log.w(TAG, msg.toString()); + w(msg.toString()); } private static SequenceResult callCallback(EventType key, BluetoothGatt gatt, @@ -258,8 +269,24 @@ class BtSequencer { btCallback.onFinally(success); return null; default: - Log.w(TAG, "Unknown callback: " + key); + w("Unknown callback: " + key); return null; } } + + public static void d(String msg) { + d.accept(msg); + } + + public static void i(String msg) { + i.accept(msg); + } + + public static void w(String msg) { + w.accept(msg); + } + + public static void w(String msg, Exception e) { + we.accept(new AbstractMap.SimpleEntry<>(msg, e)); + } } diff --git a/trygvis-io-bt-stubs/src/main/java/android/bluetooth/BluetoothGatt.java b/trygvis-io-bt-stubs/src/main/java/android/bluetooth/BluetoothGatt.java index 13b33ed..d5c4f36 100644 --- a/trygvis-io-bt-stubs/src/main/java/android/bluetooth/BluetoothGatt.java +++ b/trygvis-io-bt-stubs/src/main/java/android/bluetooth/BluetoothGatt.java @@ -4,7 +4,7 @@ import java.util.List; import java.util.UUID; public class BluetoothGatt { - public static final int GATT_SUCCESS = 1; + public static final int GATT_SUCCESS = 0; public static final int STATE_CONNECTED = 2; public boolean discoverServices() { diff --git a/trygvis-io-bt-stubs/src/main/java/android/util/Log.java b/trygvis-io-bt-stubs/src/main/java/android/util/Log.java deleted file mode 100644 index 036ce22..0000000 --- a/trygvis-io-bt-stubs/src/main/java/android/util/Log.java +++ /dev/null @@ -1,20 +0,0 @@ -package android.util; - -public class Log { - public static void d(String tag, String s) { - System.out.println("d: " + tag + ": " + s); - } - - public static void i(String tag, String s) { - System.out.println("i: " + tag + ": " + s); - } - - public static void w(String tag, String s) { - System.out.println("w: " + tag + ": " + s); - } - - public static void w(String tag, String s, Exception e) { - System.out.println("w: " + tag + ": " + s); - e.printStackTrace(System.out); - } -} -- cgit v1.2.3