rpc/bt: make ble discovery a bit stable
* Change to call Gatt connection in another thread instead of
calling from Gatt/BLE callbacks.
* Remove useless hacks.
Change-Id: Iacc16c6367f52aa08e220d461e093e1f710264e4
diff --git a/android-lib/src/main/java/io/v/android/impl/google/discovery/plugins/ble/GattReader.java b/android-lib/src/main/java/io/v/android/impl/google/discovery/plugins/ble/GattReader.java
index ebc87a0..7db12b7 100644
--- a/android-lib/src/main/java/io/v/android/impl/google/discovery/plugins/ble/GattReader.java
+++ b/android-lib/src/main/java/io/v/android/impl/google/discovery/plugins/ble/GattReader.java
@@ -129,6 +129,44 @@
return;
}
+ // This is called from Bluetooth callbacks. It seems to be more reliable to
+ // call Bluetooth APIs in a new thread although it is not clear why this helps.
+ mExecutor.submit(
+ new Runnable() {
+ @Override
+ public void run() {
+ connectGatt();
+ }
+ });
+ }
+
+ private synchronized void finishAndMaybeReadNextDevice() {
+ mCurrentGattConnectionTimeout.cancel(false);
+ mCurrentGatt.close();
+
+ maybeReadNextDevice();
+ }
+
+ private synchronized void cancelAndMaybeReadNextDevice() {
+ mCurrentGattConnectionTimeout.cancel(false);
+ mCurrentGatt.close();
+
+ final BluetoothDevice device = mCurrentDevice;
+ mExecutor.submit(
+ new Runnable() {
+ @Override
+ public void run() {
+ mHandler.onGattReadFailed(device);
+ }
+ });
+ maybeReadNextDevice();
+ }
+
+ private synchronized void connectGatt() {
+ if (mCurrentDevice == null) {
+ return;
+ }
+
mCurrentGatt = mCurrentDevice.connectGatt(mContext, false, this);
mCurrentGattConnectionTimeout =
mExecutor.schedule(
@@ -143,37 +181,6 @@
TimeUnit.MILLISECONDS);
}
- private synchronized void finishAndMaybeReadNextDevice() {
- mCurrentGattConnectionTimeout.cancel(false);
- mCurrentGatt.close();
-
- maybeReadNextDevice();
- }
-
- private synchronized void cancelAndMaybeReadNextDevice() {
- mCurrentGattConnectionTimeout.cancel(false);
- // Try to refresh the failed Gatt.
- try {
- Method method = mCurrentGatt.getClass().getMethod("refresh", new Class[0]);
- if (method != null) {
- method.invoke(mCurrentGatt, new Object[0]);
- }
- } catch (Exception e) {
- Log.e(TAG, "An exception occured while refreshing device");
- }
- mCurrentGatt.close();
-
- final BluetoothDevice device = mCurrentDevice;
- mExecutor.submit(
- new Runnable() {
- @Override
- public void run() {
- mHandler.onGattReadFailed(device);
- }
- });
- maybeReadNextDevice();
- }
-
@Override
public synchronized void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
@@ -195,11 +202,6 @@
return;
}
- // TODO(jhahn): Do we really need this?
- if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
- gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH);
- }
-
// MTU exchange is not allowed on a BR/EDR physical link.
// (Bluetooth Core Specification Volume 3, Part G, 4.3.1)
//
diff --git a/lib/src/main/java/io/v/impl/google/lib/discovery/GlobalDiscovery.java b/lib/src/main/java/io/v/impl/google/lib/discovery/GlobalDiscovery.java
index 9031baa..0082135 100644
--- a/lib/src/main/java/io/v/impl/google/lib/discovery/GlobalDiscovery.java
+++ b/lib/src/main/java/io/v/impl/google/lib/discovery/GlobalDiscovery.java
@@ -22,7 +22,7 @@
/**
* Returns a new global {@link Discovery} instance that uses the Vanadium namespace
- * under {@link path} with default mount ttl (120s) and scan interval (90s).
+ * under {@code path} with default mount ttl (120s) and scan interval (90s).
*
* @param ctx current context
* @param path a namespace path to use for global discovery
@@ -34,11 +34,11 @@
/**
* Returns a new global {@link Discovery} instance that uses the Vanadium
- * namespace under {@link path}.
+ * namespace under {@code path}.
*
* @param ctx current context
* @param path a namespace path to use for global discovery
- * @param mountTTL the duration for which the advertisement lives in a mounttable
+ * @param mountTtl the duration for which the advertisement lives in a mounttable
* after advertising is cancelled
* @param scanInterval the duration to scan a mounttable periodically
* @throws VException if a new discovery instance cannot be created