Merge branch '1164-store-bluetooth-properties' into 'master'

Store Bluetooth address and UUID at first startup

Closes #1164

See merge request akwizgran/briar!694
This commit is contained in:
akwizgran
2018-02-22 15:11:01 +00:00

View File

@@ -60,6 +60,7 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
private final AtomicBoolean used = new AtomicBoolean(false); private final AtomicBoolean used = new AtomicBoolean(false);
private volatile boolean running = false, contactConnections = false; private volatile boolean running = false, contactConnections = false;
private volatile String contactConnectionsUuid = null;
private volatile SS socket = null; private volatile SS socket = null;
abstract void initialiseAdapter() throws IOException; abstract void initialiseAdapter() throws IOException;
@@ -72,6 +73,10 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
abstract void setEnabledByUs(); abstract void setEnabledByUs();
/**
* Returns the local Bluetooth address, or null if no valid address can
* be found.
*/
@Nullable @Nullable
abstract String getBluetoothAddress(); abstract String getBluetoothAddress();
@@ -98,6 +103,8 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
void onAdapterEnabled() { void onAdapterEnabled() {
LOG.info("Bluetooth enabled"); LOG.info("Bluetooth enabled");
// We may not have been able to get the local address before
ioExecutor.execute(this::updateProperties);
if (shouldAllowContactConnections()) bind(); if (shouldAllowContactConnections()) bind();
} }
@@ -131,6 +138,7 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
} catch (IOException e) { } catch (IOException e) {
throw new PluginException(e); throw new PluginException(e);
} }
updateProperties();
running = true; running = true;
loadSettings(); loadSettings();
if (shouldAllowContactConnections()) { if (shouldAllowContactConnections()) {
@@ -151,19 +159,10 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
private void bind() { private void bind() {
ioExecutor.execute(() -> { ioExecutor.execute(() -> {
if (!isRunning() || !shouldAllowContactConnections()) return; 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 // Bind a server socket to accept connections from contacts
SS ss; SS ss;
try { try {
ss = openServerSocket(getUuid()); ss = openServerSocket(contactConnectionsUuid);
} catch (IOException e) { } catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
return; return;
@@ -179,17 +178,29 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
}); });
} }
private String getUuid() { private void updateProperties() {
String uuid = callback.getLocalProperties().get(PROP_UUID); TransportProperties p = callback.getLocalProperties();
String address = p.get(PROP_ADDRESS);
String 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) { if (uuid == null) {
byte[] random = new byte[UUID_BYTES]; byte[] random = new byte[UUID_BYTES];
secureRandom.nextBytes(random); secureRandom.nextBytes(random);
uuid = UUID.nameUUIDFromBytes(random).toString(); uuid = UUID.nameUUIDFromBytes(random).toString();
TransportProperties p = new TransportProperties();
p.put(PROP_UUID, uuid); p.put(PROP_UUID, uuid);
callback.mergeLocalProperties(p); changed = true;
} }
return uuid; contactConnectionsUuid = uuid;
if (changed) callback.mergeLocalProperties(p);
} }
private void acceptContactConnections() { private void acceptContactConnections() {