mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-21 15:19:53 +01:00
Turn off Bluetooth at shutdown if we turned it on at startup.
Also, turn on Bluetooth if necessary before polling. Fixes issue #3611935
This commit is contained in:
@@ -65,6 +65,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;
|
||||||
|
|
||||||
// Non-null if running has ever been true
|
// Non-null if running has ever been true
|
||||||
private volatile BluetoothAdapter adapter = null;
|
private volatile BluetoothAdapter adapter = null;
|
||||||
@@ -109,8 +110,12 @@ class DroidtoothPlugin implements DuplexPlugin {
|
|||||||
} catch(ExecutionException e) {
|
} catch(ExecutionException e) {
|
||||||
throw new IOException(e.toString());
|
throw new IOException(e.toString());
|
||||||
}
|
}
|
||||||
if(adapter == null) return false; // Bluetooth not supported
|
if(adapter == null) {
|
||||||
|
if(LOG.isLoggable(INFO)) LOG.info("Bluetooth is not supported");
|
||||||
|
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();
|
||||||
@@ -121,10 +126,7 @@ class DroidtoothPlugin implements DuplexPlugin {
|
|||||||
|
|
||||||
private void bind() {
|
private void bind() {
|
||||||
if(!running) return;
|
if(!running) return;
|
||||||
if(!enableBluetooth()) {
|
if(!enableBluetooth()) return;
|
||||||
if(LOG.isLoggable(INFO)) LOG.info("Could not enable Bluetooth");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(LOG.isLoggable(INFO))
|
if(LOG.isLoggable(INFO))
|
||||||
LOG.info("Local address " + adapter.getAddress());
|
LOG.info("Local address " + adapter.getAddress());
|
||||||
// Advertise the Bluetooth address to contacts
|
// Advertise the Bluetooth address to contacts
|
||||||
@@ -148,15 +150,22 @@ class DroidtoothPlugin implements DuplexPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean enableBluetooth() {
|
private boolean enableBluetooth() {
|
||||||
if(!running) return false;
|
isEnabled = adapter.isEnabled();
|
||||||
if(adapter.isEnabled()) return true;
|
if(isEnabled) return 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");
|
||||||
IntentFilter filter = new IntentFilter(ACTION_STATE_CHANGED);
|
IntentFilter filter = new IntentFilter(ACTION_STATE_CHANGED);
|
||||||
BluetoothStateReceiver receiver = new BluetoothStateReceiver();
|
BluetoothStateReceiver receiver = new BluetoothStateReceiver();
|
||||||
appContext.registerReceiver(receiver, filter);
|
appContext.registerReceiver(receiver, filter);
|
||||||
try {
|
try {
|
||||||
if(!adapter.enable()) return false;
|
if(adapter.enable()) {
|
||||||
return receiver.waitForStateChange();
|
isEnabled = receiver.waitForStateChange();
|
||||||
|
if(LOG.isLoggable(INFO)) LOG.info("Enabled: " + isEnabled);
|
||||||
|
return isEnabled;
|
||||||
|
} else {
|
||||||
|
if(LOG.isLoggable(INFO)) LOG.info("Could not enable adapter");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
} catch(InterruptedException e) {
|
} catch(InterruptedException e) {
|
||||||
if(LOG.isLoggable(INFO))
|
if(LOG.isLoggable(INFO))
|
||||||
LOG.info("Interrupted while enabling Bluetooth");
|
LOG.info("Interrupted while enabling Bluetooth");
|
||||||
@@ -206,6 +215,30 @@ class DroidtoothPlugin implements DuplexPlugin {
|
|||||||
|
|
||||||
public void stop() {
|
public void stop() {
|
||||||
running = false;
|
running = false;
|
||||||
|
// Disable Bluetooth if we enabled it at startup
|
||||||
|
if(isEnabled && !wasEnabled) disableBluetooth();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void disableBluetooth() {
|
||||||
|
isEnabled = adapter.isEnabled();
|
||||||
|
if(!isEnabled) return;
|
||||||
|
// Try to disable the adapter and wait for the result
|
||||||
|
if(LOG.isLoggable(INFO)) LOG.info("Disabling Bluetooth");
|
||||||
|
IntentFilter filter = new IntentFilter(ACTION_STATE_CHANGED);
|
||||||
|
BluetoothStateReceiver receiver = new BluetoothStateReceiver();
|
||||||
|
appContext.registerReceiver(receiver, filter);
|
||||||
|
try {
|
||||||
|
if(adapter.disable()) {
|
||||||
|
isEnabled = receiver.waitForStateChange();
|
||||||
|
if(LOG.isLoggable(INFO)) LOG.info("Enabled: " + isEnabled);
|
||||||
|
} else {
|
||||||
|
if(LOG.isLoggable(INFO)) LOG.info("Could not disable adapter");
|
||||||
|
}
|
||||||
|
} catch(InterruptedException e) {
|
||||||
|
if(LOG.isLoggable(INFO))
|
||||||
|
LOG.info("Interrupted while disabling Bluetooth");
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean shouldPoll() {
|
public boolean shouldPoll() {
|
||||||
@@ -218,6 +251,7 @@ class DroidtoothPlugin implements DuplexPlugin {
|
|||||||
|
|
||||||
public void poll(Collection<ContactId> connected) {
|
public void poll(Collection<ContactId> connected) {
|
||||||
if(!running) return;
|
if(!running) return;
|
||||||
|
if(!enableBluetooth()) return;
|
||||||
// Try to connect to known devices in parallel
|
// Try to connect to known devices in parallel
|
||||||
Map<ContactId, TransportProperties> remote =
|
Map<ContactId, TransportProperties> remote =
|
||||||
callback.getRemoteProperties();
|
callback.getRemoteProperties();
|
||||||
|
|||||||
Reference in New Issue
Block a user