From f489da1a21da07c22284716d5970e858f554c22c Mon Sep 17 00:00:00 2001 From: akwizgran <akwizgran@users.sourceforge.net> Date: Sat, 5 Apr 2014 21:36:05 +0100 Subject: [PATCH] Wait for Bluetooth to be disabled before exiting. This may have been the cause of Bluetooth not always being disabled at shutdown on the Sony Xperia Tipo. --- .../plugins/droidtooth/DroidtoothPlugin.java | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/briar-android/src/org/briarproject/plugins/droidtooth/DroidtoothPlugin.java b/briar-android/src/org/briarproject/plugins/droidtooth/DroidtoothPlugin.java index 70b6a134c1..3355a984ae 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; -- GitLab