Added a config setting to not enable Bluetooth automatically (bug #28).

There's currently no way to set this setting, so the bug isn't fixed.
This commit is contained in:
akwizgran
2014-03-03 15:31:05 +00:00
parent 74159f6e71
commit 56f79755a4

View File

@@ -67,7 +67,7 @@ class DroidtoothPlugin implements DuplexPlugin {
private final long maxLatency, pollingInterval; private final long maxLatency, pollingInterval;
private volatile boolean running = false; private volatile boolean running = false;
private volatile boolean wasEnabled = false, isEnabled = false; private volatile boolean wasDisabled = false;
private volatile BluetoothServerSocket socket = null; private volatile BluetoothServerSocket socket = null;
// Non-null if running has ever been true // Non-null if running has ever been true
@@ -119,7 +119,6 @@ class DroidtoothPlugin implements DuplexPlugin {
return false; return false;
} }
running = true; running = true;
wasEnabled = isEnabled = adapter.isEnabled();
pluginExecutor.execute(new Runnable() { pluginExecutor.execute(new Runnable() {
public void run() { public void run() {
bind(); bind();
@@ -155,8 +154,13 @@ class DroidtoothPlugin implements DuplexPlugin {
} }
private boolean enableBluetooth() { private boolean enableBluetooth() {
isEnabled = adapter.isEnabled(); if(adapter.isEnabled()) return true;
if(isEnabled) return true; String enable = callback.getConfig().get("enable");
if("false".equals(enable)) {
if(LOG.isLoggable(INFO)) LOG.info("Not enabling Bluetooth");
return false;
}
wasDisabled = true;
// Try to enable the adapter and wait for the result // Try to enable the adapter and wait for the result
if(LOG.isLoggable(INFO)) LOG.info("Enabling Bluetooth"); if(LOG.isLoggable(INFO)) LOG.info("Enabling Bluetooth");
IntentFilter filter = new IntentFilter(ACTION_STATE_CHANGED); IntentFilter filter = new IntentFilter(ACTION_STATE_CHANGED);
@@ -164,11 +168,11 @@ class DroidtoothPlugin implements DuplexPlugin {
appContext.registerReceiver(receiver, filter); appContext.registerReceiver(receiver, filter);
try { try {
if(adapter.enable()) { if(adapter.enable()) {
isEnabled = receiver.waitForStateChange(); boolean enabled = receiver.waitForStateChange();
if(LOG.isLoggable(INFO)) LOG.info("Enabled: " + isEnabled); if(LOG.isLoggable(INFO)) LOG.info("Enabled: " + enabled);
return isEnabled; return enabled;
} else { } else {
if(LOG.isLoggable(INFO)) LOG.info("Could not enable adapter"); if(LOG.isLoggable(INFO)) LOG.info("Could not enable Bluetooth");
return false; return false;
} }
} catch(InterruptedException e) { } catch(InterruptedException e) {
@@ -207,7 +211,7 @@ class DroidtoothPlugin implements DuplexPlugin {
s = socket.accept(); s = socket.accept();
} catch(IOException e) { } catch(IOException e) {
// This is expected when the socket is closed // This is expected when the socket is closed
if(LOG.isLoggable(INFO)) LOG.log(INFO, e.toString(), e); if(LOG.isLoggable(INFO)) LOG.info(e.toString());
tryToClose(socket); tryToClose(socket);
return; return;
} }
@@ -223,13 +227,12 @@ class DroidtoothPlugin implements DuplexPlugin {
public void stop() { public void stop() {
running = false; running = false;
if(socket != null) tryToClose(socket); if(socket != null) tryToClose(socket);
// Disable Bluetooth if we enabled it at startup // Disable Bluetooth if we enabled it at any point
if(isEnabled && !wasEnabled) disableBluetooth(); if(wasDisabled) disableBluetooth();
} }
private void disableBluetooth() { private void disableBluetooth() {
isEnabled = adapter.isEnabled(); if(!adapter.isEnabled()) return;
if(!isEnabled) return;
// Try to disable the adapter and wait for the result // Try to disable the adapter and wait for the result
if(LOG.isLoggable(INFO)) LOG.info("Disabling Bluetooth"); if(LOG.isLoggable(INFO)) LOG.info("Disabling Bluetooth");
IntentFilter filter = new IntentFilter(ACTION_STATE_CHANGED); IntentFilter filter = new IntentFilter(ACTION_STATE_CHANGED);
@@ -237,10 +240,11 @@ class DroidtoothPlugin implements DuplexPlugin {
appContext.registerReceiver(receiver, filter); appContext.registerReceiver(receiver, filter);
try { try {
if(adapter.disable()) { if(adapter.disable()) {
isEnabled = receiver.waitForStateChange(); boolean enabled = receiver.waitForStateChange();
if(LOG.isLoggable(INFO)) LOG.info("Enabled: " + isEnabled); if(LOG.isLoggable(INFO)) LOG.info("Enabled: " + enabled);
} else { } else {
if(LOG.isLoggable(INFO)) LOG.info("Could not disable adapter"); if(LOG.isLoggable(INFO))
LOG.info("Could not disable Bluetooth");
} }
} catch(InterruptedException e) { } catch(InterruptedException e) {
if(LOG.isLoggable(INFO)) if(LOG.isLoggable(INFO))
@@ -310,7 +314,8 @@ class DroidtoothPlugin implements DuplexPlugin {
if(LOG.isLoggable(INFO)) LOG.info("Connected to " + address); if(LOG.isLoggable(INFO)) LOG.info("Connected to " + address);
return s; return s;
} catch(IOException e) { } catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); if(LOG.isLoggable(INFO))
LOG.info("Failed to connect to " + address);
tryToClose(s); tryToClose(s);
return null; return null;
} }
@@ -344,6 +349,7 @@ class DroidtoothPlugin implements DuplexPlugin {
public DuplexTransportConnection createInvitationConnection(PseudoRandom r, public DuplexTransportConnection createInvitationConnection(PseudoRandom r,
long timeout) { long timeout) {
if(!running) return null; if(!running) return null;
if(!enableBluetooth()) return null;
// Use the invitation codes to generate the UUID // Use the invitation codes to generate the UUID
byte[] b = r.nextBytes(UUID_BYTES); byte[] b = r.nextBytes(UUID_BYTES);
UUID uuid = UUID.nameUUIDFromBytes(b); UUID uuid = UUID.nameUUIDFromBytes(b);
@@ -508,7 +514,7 @@ class DroidtoothPlugin implements DuplexPlugin {
} }
} catch(IOException e) { } catch(IOException e) {
// This is expected when the socket is closed // This is expected when the socket is closed
if(LOG.isLoggable(INFO)) LOG.log(INFO, e.toString(), e); if(LOG.isLoggable(INFO)) LOG.info(e.toString());
} }
} }
} }