diff --git a/briar-android/src/org/briarproject/plugins/droidtooth/DroidtoothPlugin.java b/briar-android/src/org/briarproject/plugins/droidtooth/DroidtoothPlugin.java index 70b6a134c1eb9247eae665503b3154fe929cb99e..3355a984ae2c48495813748643039382690e71c1 100644 --- a/briar-android/src/org/briarproject/plugins/droidtooth/DroidtoothPlugin.java +++ b/briar-android/src/org/briarproject/plugins/droidtooth/DroidtoothPlugin.java @@ -228,8 +228,18 @@ class DroidtoothPlugin implements DuplexPlugin { tryToClose(socket); // Disable Bluetooth if we enabled it and it's still enabled if(wasDisabled && adapter.isEnabled()) { - if(adapter.disable()) LOG.info("Disabling Bluetooth"); - else LOG.info("Could not disable Bluetooth"); + // Try to disable the adapter and wait for the result + LOG.info("Disabling Bluetooth"); + IntentFilter filter = new IntentFilter(ACTION_STATE_CHANGED); + DisableBluetoothReceiver receiver = new DisableBluetoothReceiver(); + appContext.registerReceiver(receiver, filter); + if(adapter.disable()) { + LOG.info("Disabling Bluetooth"); + receiver.waitForStateChange(); + } else { + LOG.info("Could not disable Bluetooth"); + } + appContext.unregisterReceiver(receiver); } } @@ -382,6 +392,28 @@ class DroidtoothPlugin implements DuplexPlugin { } } + private class DisableBluetoothReceiver extends BroadcastReceiver { + + private final CountDownLatch latch = new CountDownLatch(1); + + public void onReceive(Context ctx, Intent intent) { + int state = intent.getIntExtra(EXTRA_STATE, 0); + if(state == STATE_OFF) { + LOG.info("Bluetooth disabled"); + latch.countDown(); + } + } + + private void waitForStateChange() { + try { + latch.await(); + } catch(InterruptedException e) { + LOG.info("Interrupted while disabling Bluetooth"); + Thread.currentThread().interrupt(); + } + } + } + private class DiscoveryThread extends Thread { private final LatchedReference<BluetoothSocket> socketLatch;