From 4a2ca2d94c827566f8682e8dbd6fbdf17d70b4dd Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 4 Jan 2015 23:48:53 +0100 Subject: o Adding a way to prepend callbacks on the promise's queue. Needed for devices that give you [disconnect, connect] events when connecting. Yay. o Reading meta data from the Soil Moisture device. --- .../main/java/io/trygvis/android/bt/BtDevice.java | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 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 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> { 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> { } BtDevice(DefaultBtService btService, BluetoothDevice bluetoothDevice, - BtService.BtDbIntegration btDbIntegration, long id, Integer rssi, - BtScanResult scanResult, boolean seenBefore, Date firstSeen, Date lastSeen) { + BtService.BtDbIntegration 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> { 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; } -- cgit v1.2.3