diff options
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.java | 81 |
1 files changed, 56 insertions, 25 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 2a4a5d8..6a4d92e 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,11 @@ 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.stop; -public class BtDevice<A extends BtDevice.BtDeviceWrapper<A>> { +public class BtDevice<A> { private final static String TAG = BtDevice.class.getSimpleName(); private final DefaultBtService btService; @@ -22,6 +23,7 @@ public class BtDevice<A extends BtDevice.BtDeviceWrapper<A>> { private Integer rssi; private BtScanResult scanResult; private A tag; + private final String address; private final long id; private final boolean seenBefore; @@ -30,9 +32,7 @@ public class BtDevice<A extends BtDevice.BtDeviceWrapper<A>> { private boolean connected; private final WrappingBluetoothGattCallback wrappingCallback = new WrappingBluetoothGattCallback(); - public static interface BtDeviceWrapper<A extends BtDevice.BtDeviceWrapper<A>> { - BtDevice<A> getBtDevice(); - } + private BtBluetoothGattCallback callback; BtDevice(DefaultBtService btService, BluetoothDevice bluetoothDevice, SQLiteDatabase db, BtService.BtDbIntegration<A> btDbIntegration, long id, Integer rssi, @@ -47,6 +47,7 @@ public class BtDevice<A extends BtDevice.BtDeviceWrapper<A>> { this.lastSeen = lastSeen; this.tag = btDbIntegration.createTag(db, this); + this.address = bluetoothDevice.getAddress(); } public long getId() { @@ -65,7 +66,7 @@ public class BtDevice<A extends BtDevice.BtDeviceWrapper<A>> { return bluetoothDevice.getName(); } - public int getRssi() { + public Integer getRssi() { return rssi; } @@ -103,8 +104,9 @@ public class BtDevice<A extends BtDevice.BtDeviceWrapper<A>> { // return true; // } - private BluetoothGattCallback callback; - + /** + * The first handler must handle a onDirect(). + */ public synchronized void withConnection(BtPromise promise) { if (callback != null) { throw new RuntimeException("The current callback is not done."); @@ -112,13 +114,15 @@ public class BtDevice<A extends BtDevice.BtDeviceWrapper<A>> { Log.i(TAG, "withConnection(), address=" + bluetoothDevice.getAddress() + ", connected: " + (gatt != null)); + BtPromise newPromise; if (gatt == null) { - callback = new BtPromise(). + newPromise = new BtPromise(). ignoreFailureForNext(). onConnectionStateChange((gatt, status, newState) -> { Log.i(TAG, "defaultConnectCallback: status=" + status + ", newState=" + newState); - String address = gatt.getDevice().getAddress(); - //noinspection SimplifiableIfStatement + + btService.sendBroadcast(btService.createDeviceConnection(address)); + if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothGatt.STATE_CONNECTED) { Log.i(TAG, "Connected to " + address); return detour(promise); @@ -136,21 +140,26 @@ public class BtDevice<A extends BtDevice.BtDeviceWrapper<A>> { return stop(); })); } - }). - asCallback(bluetoothDevice.getAddress()); + }); } else { - callback = promise.asCallback(bluetoothDevice.getAddress()); + newPromise = promise; } - gatt = bluetoothDevice.connectGatt(btService, false, wrappingCallback); - } + callback = newPromise. + onFinally(() -> { + Log.i(TAG, "Promise done, device is available again: address=" + address); + callback = null; + }). + asCallback(bluetoothDevice.getAddress()); -// public synchronized void disconnect() { -// if (gatt != null) { -// gatt.disconnect(); -// gatt = null; -// } -// } + if (gatt == null) { + gatt = bluetoothDevice.connectGatt(btService, false, wrappingCallback); + } else { + callback.onEvent(BtPromise.EventType.onDirect, "", gatt, null, null, + BluetoothGatt.GATT_SUCCESS, + BluetoothGatt.STATE_CONNECTED, gatt); + } + } public boolean connected() { return connected; @@ -184,10 +193,32 @@ public class BtDevice<A extends BtDevice.BtDeviceWrapper<A>> { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { - BtDevice.this.connected = newState == BluetoothGatt.STATE_CONNECTED; - - if (callback != null) { - callback.onConnectionStateChange(gatt, status, newState); + Log.i(TAG, "Wrapping: onConnectionStateChange: " + + "address=" + gatt.getDevice().getAddress() + ", " + + "status=" + status + ", " + + "newState=" + newState); + + boolean oldConnected = connected; + + BtDevice.this.connected = status == BluetoothGatt.GATT_SUCCESS && + newState == BluetoothGatt.STATE_CONNECTED; + + try { + if (callback != null) { + callback.onConnectionStateChange(gatt, status, newState); + } + } finally { + if (!BtDevice.this.connected) { + if (oldConnected) { + Log.i(TAG, "Wrapper: Lost connection, removing gatt. gatt=" + gatt); + } else { + Log.i(TAG, "Wrapper: Lost connection, was not connected. gatt=" + gatt); + } + + BtDevice.this.gatt = null; + } else { + Log.i(TAG, "Wrapper: connected"); + } } } |