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.java49
1 files changed, 33 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 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<A> {
+public class BtDevice<A> implements Comparable<BtDevice> {
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<A> {
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<A> {
BtDevice(DefaultBtService btService, BluetoothDevice bluetoothDevice, SQLiteDatabase db,
BtService.BtDbIntegration<A> 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<A> {
}
public String getAddress() {
- return bluetoothDevice.getAddress();
+ return address;
}
public String getName() {
@@ -71,10 +74,6 @@ public class BtDevice<A> {
return rssi;
}
- public BtScanResult getScanResult() {
- return scanResult;
- }
-
public boolean isSeenBefore() {
return seenBefore;
}
@@ -91,6 +90,14 @@ public class BtDevice<A> {
this.lastSeen = lastSeen;
}
+ public boolean isRecentlySeen() {
+ return recentlySeen;
+ }
+
+ public void setRecentlySeen(boolean recentlySeen) {
+ this.recentlySeen = recentlySeen;
+ }
+
/**
* The first handler must handle a onDirect().
* <p>
@@ -101,7 +108,7 @@ public class BtDevice<A> {
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<A> {
@Override
public String toString() {
- return "BtDevice{address=" + bluetoothDevice.getAddress() + '}';
+ return "BtDevice{address=" + address + '}';
}
@Override
@@ -174,12 +181,17 @@ public class BtDevice<A> {
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<A> {
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<A> {
} 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");
}
}
}