summaryrefslogtreecommitdiff
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.java35
1 files changed, 14 insertions, 21 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 6a4d92e..23fd397 100644
--- a/app/src/main/java/io/trygvis/android/bt/BtDevice.java
+++ b/app/src/main/java/io/trygvis/android/bt/BtDevice.java
@@ -12,7 +12,8 @@ import java.util.Date;
import static io.trygvis.android.bt.BtPromise.BtBluetoothGattCallback;
import static io.trygvis.android.bt.BtPromise.PromiseResult.detour;
-import static io.trygvis.android.bt.BtPromise.PromiseResult.stop;
+import static io.trygvis.android.bt.BtPromise.PromiseResult.fail;
+import static io.trygvis.android.bt.BtPromise.PromiseResult.waitForNextEvent;
public class BtDevice<A> {
private final static String TAG = BtDevice.class.getSimpleName();
@@ -90,22 +91,10 @@ public class BtDevice<A> {
this.lastSeen = lastSeen;
}
-// public synchronized boolean connect(BtPromise executor) {
-// Log.i(TAG, "connect(), address=" + bluetoothDevice.getAddress());
-// 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;
-// }
-
/**
* The first handler must handle a onDirect().
+ * <p>
+ * Services will be discovered.
*/
public synchronized void withConnection(BtPromise promise) {
if (callback != null) {
@@ -124,22 +113,26 @@ public class BtDevice<A> {
btService.sendBroadcast(btService.createDeviceConnection(address));
if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothGatt.STATE_CONNECTED) {
- Log.i(TAG, "Connected to " + address);
- return detour(promise);
+ Log.i(TAG, "Connected to " + address + ", discovering services");
+ return gatt.discoverServices() ? waitForNextEvent() : fail();
} else {
Log.i(TAG, "Could not connect to " + address + ", trying again");
return detour(new BtPromise().onConnectionStateChange((gatt2, status2, newState2) -> {
if (status2 == BluetoothGatt.GATT_SUCCESS && newState2 == BluetoothGatt.STATE_CONNECTED) {
- Log.i(TAG, "Connected to " + address);
- return detour(promise);
+ Log.i(TAG, "Connected to " + address + ", discovering services");
+ return gatt.discoverServices() ? waitForNextEvent() : fail();
}
Log.i(TAG, "Could still not connect to " + address + ", failing.");
- return stop();
+ return fail();
}));
}
+ }).
+ onServicesDiscovered(gatt -> {
+ Log.i(TAG, "Services discovered, has " + gatt.getServices().size() + " services");
+ return detour(promise);
});
} else {
newPromise = promise;
@@ -157,7 +150,7 @@ public class BtDevice<A> {
} else {
callback.onEvent(BtPromise.EventType.onDirect, "", gatt, null, null,
BluetoothGatt.GATT_SUCCESS,
- BluetoothGatt.STATE_CONNECTED, gatt);
+ BluetoothGatt.STATE_CONNECTED);
}
}