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 | 21 |
1 files changed, 16 insertions, 5 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 edad522..1c7666e 100644 --- a/app/src/main/java/io/trygvis/android/bt/BtDevice.java +++ b/app/src/main/java/io/trygvis/android/bt/BtDevice.java @@ -1,6 +1,8 @@ package io.trygvis.android.bt; import android.bluetooth.BluetoothDevice; +import android.bluetooth.BluetoothGatt; +import android.bluetooth.BluetoothGattCallback; import android.util.Log; import java.util.Date; @@ -10,6 +12,7 @@ public class BtDevice<A extends BtDevice.BtDeviceWrapper<A>> { private final DefaultBtService btService; private final BluetoothDevice bluetoothDevice; + private BluetoothGatt gatt; private Integer rssi; private BtScanResult scanResult; private A tag; @@ -24,8 +27,8 @@ public class BtDevice<A extends BtDevice.BtDeviceWrapper<A>> { } BtDevice(DefaultBtService btService, BluetoothDevice bluetoothDevice, - BtService.BtDbIntegration<A> btDbIntegration, long id, Integer rssi, - BtScanResult scanResult, boolean seenBefore, Date firstSeen, Date lastSeen) { + 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); @@ -73,12 +76,20 @@ public class BtDevice<A extends BtDevice.BtDeviceWrapper<A>> { this.lastSeen = lastSeen; } - public boolean connect(BtActionExecutor executor) { - Log.i(TAG, "connect(), address=" + bluetoothDevice.getAddress() + ", queue=" + executor); - bluetoothDevice.connectGatt(btService, false, executor.asCallback()); + public synchronized boolean connect(BtPromise executor) { + Log.i(TAG, "connect(), address=" + bluetoothDevice.getAddress()); + BluetoothGattCallback callback = executor.asCallback(); + gatt = bluetoothDevice.connectGatt(btService, false, callback); return true; } + public synchronized void disconnect() { + if (gatt != null) { + gatt.disconnect(); + gatt = null; + } + } + public BtScanResult getScanResult() { return scanResult; } |