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 | 26 |
1 files changed, 20 insertions, 6 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 1c7666e..d8dfefc 100644 --- a/app/src/main/java/io/trygvis/android/bt/BtDevice.java +++ b/app/src/main/java/io/trygvis/android/bt/BtDevice.java @@ -3,6 +3,7 @@ package io.trygvis.android.bt; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCallback; +import android.database.sqlite.SQLiteDatabase; import android.util.Log; import java.util.Date; @@ -21,23 +22,25 @@ public class BtDevice<A extends BtDevice.BtDeviceWrapper<A>> { private final boolean seenBefore; private final Date firstSeen; private Date lastSeen; + private boolean connected; public static interface BtDeviceWrapper<A extends BtDevice.BtDeviceWrapper<A>> { BtDevice<A> getBtDevice(); } - BtDevice(DefaultBtService btService, BluetoothDevice bluetoothDevice, + BtDevice(DefaultBtService btService, BluetoothDevice bluetoothDevice, SQLiteDatabase db, 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); this.id = id; this.rssi = rssi; this.scanResult = scanResult; this.seenBefore = seenBefore; this.firstSeen = firstSeen; this.lastSeen = lastSeen; + + this.tag = btDbIntegration.createTag(db, this); } public long getId() { @@ -60,6 +63,10 @@ public class BtDevice<A extends BtDevice.BtDeviceWrapper<A>> { return rssi; } + public BtScanResult getScanResult() { + return scanResult; + } + public boolean isSeenBefore() { return seenBefore; } @@ -78,8 +85,15 @@ public class BtDevice<A extends BtDevice.BtDeviceWrapper<A>> { public synchronized boolean connect(BtPromise executor) { Log.i(TAG, "connect(), address=" + bluetoothDevice.getAddress()); - BluetoothGattCallback callback = executor.asCallback(); - gatt = bluetoothDevice.connectGatt(btService, false, callback); + BluetoothGattCallback callback = executor.asCallback(bluetoothDevice.getAddress()); + gatt = bluetoothDevice.connectGatt(btService, false, new WrappingBluetoothGattCallback(callback) { + @Override + public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { + BtDevice.this.connected = newState == BluetoothGatt.STATE_CONNECTED; + + super.onConnectionStateChange(gatt, status, newState); + } + }); return true; } @@ -90,8 +104,8 @@ public class BtDevice<A extends BtDevice.BtDeviceWrapper<A>> { } } - public BtScanResult getScanResult() { - return scanResult; + public boolean connected() { + return connected; } @Override |