From c4685214d8db34166213ffa373a16af1a99401a5 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 27 Jan 2015 21:23:50 +0100 Subject: o Adding 'recently seen' on BtDevice. Updated when scanning. o Removing BtScanResult, it was never used. o Getting MainActivity to listen on device property changed so the UI is properly updated. o Adding a status bar with color to indicate if the device is available, connected or not seen. --- .../main/java/io/trygvis/android/bt/BtDevice.java | 49 +++++++++++++++------- 1 file changed, 33 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 fd9e2b9..b90ac4f 100644 --- a/app/src/main/java/io/trygvis/android/bt/BtDevice.java +++ b/app/src/main/java/io/trygvis/android/bt/BtDevice.java @@ -15,14 +15,13 @@ 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; -public class BtDevice { +public class BtDevice implements Comparable { private final static String TAG = BtDevice.class.getSimpleName(); private final DefaultBtService btService; private final BluetoothDevice bluetoothDevice; private BluetoothGatt gatt; private Integer rssi; - private BtScanResult scanResult; private A tag; private final String address; @@ -30,6 +29,10 @@ public class BtDevice { private final boolean seenBefore; private final Date firstSeen; private Date lastSeen; + /** + * If seen in last scan. + */ + private boolean recentlySeen; private boolean connected; private final WrappingBluetoothGattCallback wrappingCallback = new WrappingBluetoothGattCallback(); @@ -37,15 +40,15 @@ public class BtDevice { BtDevice(DefaultBtService btService, BluetoothDevice bluetoothDevice, SQLiteDatabase db, BtService.BtDbIntegration btDbIntegration, long id, Integer rssi, - BtScanResult scanResult, boolean seenBefore, Date firstSeen, Date lastSeen) { + boolean seenBefore, Date firstSeen, Date lastSeen, boolean recentlySeen) { this.btService = btService; this.bluetoothDevice = bluetoothDevice; this.id = id; this.rssi = rssi; - this.scanResult = scanResult; this.seenBefore = seenBefore; this.firstSeen = firstSeen; this.lastSeen = lastSeen; + this.recentlySeen = recentlySeen; this.tag = btDbIntegration.createTag(db, this); this.address = bluetoothDevice.getAddress(); @@ -60,7 +63,7 @@ public class BtDevice { } public String getAddress() { - return bluetoothDevice.getAddress(); + return address; } public String getName() { @@ -71,10 +74,6 @@ public class BtDevice { return rssi; } - public BtScanResult getScanResult() { - return scanResult; - } - public boolean isSeenBefore() { return seenBefore; } @@ -91,6 +90,14 @@ public class BtDevice { this.lastSeen = lastSeen; } + public boolean isRecentlySeen() { + return recentlySeen; + } + + public void setRecentlySeen(boolean recentlySeen) { + this.recentlySeen = recentlySeen; + } + /** * The first handler must handle a onDirect(). *

@@ -101,7 +108,7 @@ public class BtDevice { throw new RuntimeException("The current callback is not done."); } - Log.i(TAG, "withConnection(), address=" + bluetoothDevice.getAddress() + ", connected: " + (gatt != null)); + Log.i(TAG, "withConnection(), address=" + address + ", connected: " + (gatt != null)); BtPromise newPromise; if (gatt == null) { @@ -160,7 +167,7 @@ public class BtDevice { @Override public String toString() { - return "BtDevice{address=" + bluetoothDevice.getAddress() + '}'; + return "BtDevice{address=" + address + '}'; } @Override @@ -174,12 +181,17 @@ public class BtDevice { BtDevice other = (BtDevice) o; - return getAddress().equals(other.getAddress()); + return address.equals(other.getAddress()); } @Override public int hashCode() { - return getAddress().hashCode(); + return address.hashCode(); + } + + @Override + public int compareTo(BtDevice that) { + return address.compareTo(that.address); } private class WrappingBluetoothGattCallback extends BluetoothGattCallback { @@ -196,6 +208,11 @@ public class BtDevice { BtDevice.this.connected = status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothGatt.STATE_CONNECTED; + if (oldConnected && BtDevice.this.connected) { + Log.i(TAG, "Wrapping: Extra 'onConnectionStateChange' event, ignoring. gatt=" + gatt); + return; + } + try { if (callback != null) { callback.onConnectionStateChange(gatt, status, newState); @@ -203,14 +220,14 @@ public class BtDevice { } finally { if (!BtDevice.this.connected) { if (oldConnected) { - Log.i(TAG, "Wrapper: Lost connection, removing gatt. gatt=" + gatt); + Log.i(TAG, "Wrapping: Lost connection, removing gatt. gatt=" + gatt); } else { - Log.i(TAG, "Wrapper: Lost connection, was not connected. gatt=" + gatt); + Log.i(TAG, "Wrapping: Lost connection, was not connected. gatt=" + gatt); } BtDevice.this.gatt = null; } else { - Log.i(TAG, "Wrapper: connected"); + Log.i(TAG, "Wrapping: connected"); } } } -- cgit v1.2.3