From 3cf61e7b3d8f2bcbe84310c0eb2f116dcd464b80 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Thu, 8 Feb 2018 13:04:47 +0000 Subject: [PATCH 1/3] Store Bluetooth address and UUID at first startup. --- .../plugin/bluetooth/BluetoothPlugin.java | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothPlugin.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothPlugin.java index 4f7afa034..928f8b034 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothPlugin.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothPlugin.java @@ -60,6 +60,7 @@ abstract class BluetoothPlugin implements DuplexPlugin, EventListener { private final AtomicBoolean used = new AtomicBoolean(false); private volatile boolean running = false, contactConnections = false; + private volatile String contactConnectionsUuid = null; private volatile SS socket = null; abstract void initialiseAdapter() throws IOException; @@ -98,6 +99,8 @@ abstract class BluetoothPlugin implements DuplexPlugin, EventListener { void onAdapterEnabled() { LOG.info("Bluetooth enabled"); + // We may not have been able to get the local address before + ioExecutor.execute(this::updateProperties); if (shouldAllowContactConnections()) bind(); } @@ -132,6 +135,7 @@ abstract class BluetoothPlugin implements DuplexPlugin, EventListener { throw new PluginException(e); } running = true; + updateProperties(); loadSettings(); if (shouldAllowContactConnections()) { if (isAdapterEnabled()) bind(); @@ -151,19 +155,10 @@ abstract class BluetoothPlugin implements DuplexPlugin, EventListener { private void bind() { ioExecutor.execute(() -> { if (!isRunning() || !shouldAllowContactConnections()) return; - String address = getBluetoothAddress(); - if (LOG.isLoggable(INFO)) - LOG.info("Local address " + scrubMacAddress(address)); - if (!StringUtils.isNullOrEmpty(address)) { - // Advertise our Bluetooth address to contacts - TransportProperties p = new TransportProperties(); - p.put(PROP_ADDRESS, address); - callback.mergeLocalProperties(p); - } // Bind a server socket to accept connections from contacts SS ss; try { - ss = openServerSocket(getUuid()); + ss = openServerSocket(contactConnectionsUuid); } catch (IOException e) { if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); return; @@ -179,17 +174,28 @@ abstract class BluetoothPlugin implements DuplexPlugin, EventListener { }); } - private String getUuid() { - String uuid = callback.getLocalProperties().get(PROP_UUID); + private void updateProperties() { + TransportProperties p = callback.getLocalProperties(); + String address = p.get(PROP_ADDRESS), uuid = p.get(PROP_UUID); + boolean changed = false; + if (address == null) { + address = getBluetoothAddress(); + if (LOG.isLoggable(INFO)) + LOG.info("Local address " + scrubMacAddress(address)); + if (!StringUtils.isNullOrEmpty(address)) { + p.put(PROP_ADDRESS, address); + changed = true; + } + } if (uuid == null) { byte[] random = new byte[UUID_BYTES]; secureRandom.nextBytes(random); uuid = UUID.nameUUIDFromBytes(random).toString(); - TransportProperties p = new TransportProperties(); p.put(PROP_UUID, uuid); - callback.mergeLocalProperties(p); + changed = true; } - return uuid; + contactConnectionsUuid = uuid; + if (changed) callback.mergeLocalProperties(p); } private void acceptContactConnections() { From cfe0d9a65604b8a76f42c3a0dfb0a2bb649696e8 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Thu, 8 Feb 2018 15:03:49 +0000 Subject: [PATCH 2/3] Don't set running = true until properties have been loaded. --- .../briarproject/bramble/plugin/bluetooth/BluetoothPlugin.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothPlugin.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothPlugin.java index 928f8b034..115b3437d 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothPlugin.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothPlugin.java @@ -134,8 +134,8 @@ abstract class BluetoothPlugin implements DuplexPlugin, EventListener { } catch (IOException e) { throw new PluginException(e); } - running = true; updateProperties(); + running = true; loadSettings(); if (shouldAllowContactConnections()) { if (isAdapterEnabled()) bind(); From 4a1f58705d4e1a2885a2e8667300432e924dd870 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Thu, 22 Feb 2018 12:52:49 +0000 Subject: [PATCH 3/3] Address review comments. --- .../bramble/plugin/bluetooth/BluetoothPlugin.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothPlugin.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothPlugin.java index 115b3437d..ce43261a4 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothPlugin.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothPlugin.java @@ -73,6 +73,10 @@ abstract class BluetoothPlugin implements DuplexPlugin, EventListener { abstract void setEnabledByUs(); + /** + * Returns the local Bluetooth address, or null if no valid address can + * be found. + */ @Nullable abstract String getBluetoothAddress(); @@ -176,7 +180,8 @@ abstract class BluetoothPlugin implements DuplexPlugin, EventListener { private void updateProperties() { TransportProperties p = callback.getLocalProperties(); - String address = p.get(PROP_ADDRESS), uuid = p.get(PROP_UUID); + String address = p.get(PROP_ADDRESS); + String uuid = p.get(PROP_UUID); boolean changed = false; if (address == null) { address = getBluetoothAddress();