From 9515e938578567ee0c275f1a0f8d215aa085964a Mon Sep 17 00:00:00 2001 From: akwizgran Date: Thu, 11 Oct 2018 18:18:05 +0100 Subject: [PATCH] Cancel discovery after 10 seconds and try to connect. --- .../bluetooth/AndroidBluetoothPlugin.java | 25 +++++++++++++++---- .../AndroidBluetoothPluginFactory.java | 7 ++++-- .../keyagreement/KeyAgreementConstants.java | 2 +- .../briarproject/briar/android/AppModule.java | 2 +- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPlugin.java b/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPlugin.java index 5e3973045..65e0211fa 100644 --- a/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPlugin.java +++ b/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPlugin.java @@ -16,6 +16,7 @@ import org.briarproject.bramble.api.plugin.PluginException; import org.briarproject.bramble.api.plugin.duplex.DuplexPluginCallback; import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection; import org.briarproject.bramble.api.system.AndroidExecutor; +import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.util.AndroidUtils; import java.io.Closeable; @@ -23,6 +24,8 @@ import java.io.IOException; import java.security.SecureRandom; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.List; import java.util.UUID; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutionException; @@ -45,6 +48,7 @@ import static android.bluetooth.BluetoothAdapter.STATE_OFF; import static android.bluetooth.BluetoothAdapter.STATE_ON; import static android.bluetooth.BluetoothDevice.ACTION_FOUND; import static android.bluetooth.BluetoothDevice.EXTRA_DEVICE; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.logging.Level.INFO; import static java.util.logging.Level.WARNING; import static org.briarproject.bramble.util.LogUtils.logException; @@ -57,8 +61,11 @@ class AndroidBluetoothPlugin extends BluetoothPlugin { private static final Logger LOG = Logger.getLogger(AndroidBluetoothPlugin.class.getName()); + private static final int MAX_DISCOVERY_MS = 10_000; + private final AndroidExecutor androidExecutor; private final Context appContext; + private final Clock clock; private volatile boolean wasEnabledByUs = false; private volatile BluetoothStateReceiver receiver = null; @@ -68,12 +75,13 @@ class AndroidBluetoothPlugin extends BluetoothPlugin { AndroidBluetoothPlugin(BluetoothConnectionLimiter connectionLimiter, Executor ioExecutor, AndroidExecutor androidExecutor, - Context appContext, SecureRandom secureRandom, Backoff backoff, - DuplexPluginCallback callback, int maxLatency) { + Context appContext, SecureRandom secureRandom, Clock clock, + Backoff backoff, DuplexPluginCallback callback, int maxLatency) { super(connectionLimiter, ioExecutor, secureRandom, backoff, callback, maxLatency); this.androidExecutor = androidExecutor; this.appContext = appContext; + this.clock = clock; } @Override @@ -213,7 +221,7 @@ class AndroidBluetoothPlugin extends BluetoothPlugin { } private Collection discoverDevices() { - Collection addresses = new ArrayList<>(); + List addresses = new ArrayList<>(); BlockingQueue intents = new LinkedBlockingQueue<>(); DiscoveryReceiver receiver = new DiscoveryReceiver(intents); IntentFilter filter = new IntentFilter(); @@ -223,8 +231,11 @@ class AndroidBluetoothPlugin extends BluetoothPlugin { appContext.registerReceiver(receiver, filter); try { if (adapter.startDiscovery()) { - while (true) { - Intent i = intents.take(); + long now = clock.currentTimeMillis(); + long end = now + MAX_DISCOVERY_MS; + while (now < end) { + Intent i = intents.poll(end - now, MILLISECONDS); + if (i == null) break; String action = i.getAction(); if (ACTION_DISCOVERY_STARTED.equals(action)) { LOG.info("Discovery started"); @@ -239,6 +250,7 @@ class AndroidBluetoothPlugin extends BluetoothPlugin { if (!addresses.contains(address)) addresses.add(address); } + now = clock.currentTimeMillis(); } } else { LOG.info("Could not start discovery"); @@ -247,9 +259,12 @@ class AndroidBluetoothPlugin extends BluetoothPlugin { LOG.info("Interrupted while discovering devices"); Thread.currentThread().interrupt(); } finally { + LOG.info("Cancelling discovery"); adapter.cancelDiscovery(); appContext.unregisterReceiver(receiver); } + // Shuffle the addresses so we don't always try the same one first + Collections.shuffle(addresses); return addresses; } diff --git a/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPluginFactory.java b/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPluginFactory.java index c4b546739..53a127caa 100644 --- a/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPluginFactory.java +++ b/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPluginFactory.java @@ -11,6 +11,7 @@ import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin; import org.briarproject.bramble.api.plugin.duplex.DuplexPluginCallback; import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory; import org.briarproject.bramble.api.system.AndroidExecutor; +import org.briarproject.bramble.api.system.Clock; import java.security.SecureRandom; import java.util.concurrent.Executor; @@ -33,17 +34,19 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory { private final Context appContext; private final SecureRandom secureRandom; private final EventBus eventBus; + private final Clock clock; private final BackoffFactory backoffFactory; public AndroidBluetoothPluginFactory(Executor ioExecutor, AndroidExecutor androidExecutor, Context appContext, - SecureRandom secureRandom, EventBus eventBus, + SecureRandom secureRandom, EventBus eventBus, Clock clock, BackoffFactory backoffFactory) { this.ioExecutor = ioExecutor; this.androidExecutor = androidExecutor; this.appContext = appContext; this.secureRandom = secureRandom; this.eventBus = eventBus; + this.clock = clock; this.backoffFactory = backoffFactory; } @@ -65,7 +68,7 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory { MAX_POLLING_INTERVAL, BACKOFF_BASE); AndroidBluetoothPlugin plugin = new AndroidBluetoothPlugin( connectionLimiter, ioExecutor, androidExecutor, appContext, - secureRandom, backoff, callback, MAX_LATENCY); + secureRandom, clock, backoff, callback, MAX_LATENCY); eventBus.addListener(plugin); return plugin; } diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/keyagreement/KeyAgreementConstants.java b/bramble-api/src/main/java/org/briarproject/bramble/api/keyagreement/KeyAgreementConstants.java index 078b824a8..4041d0ce4 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/keyagreement/KeyAgreementConstants.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/keyagreement/KeyAgreementConstants.java @@ -21,7 +21,7 @@ public interface KeyAgreementConstants { /** * The connection timeout in milliseconds. */ - long CONNECTION_TIMEOUT = 40 * 1000; + long CONNECTION_TIMEOUT = 60_000; /** * The transport identifier for Bluetooth. diff --git a/briar-android/src/main/java/org/briarproject/briar/android/AppModule.java b/briar-android/src/main/java/org/briarproject/briar/android/AppModule.java index 606647958..bbfb919ce 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/AppModule.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/AppModule.java @@ -107,7 +107,7 @@ public class AppModule { Context appContext = app.getApplicationContext(); DuplexPluginFactory bluetooth = new AndroidBluetoothPluginFactory(ioExecutor, androidExecutor, - appContext, random, eventBus, backoffFactory); + appContext, random, eventBus, clock, backoffFactory); DuplexPluginFactory tor = new AndroidTorPluginFactory(ioExecutor, scheduler, appContext, networkManager, locationUtils, eventBus, torSocketFactory, backoffFactory, resourceProvider,