diff --git a/src/org/thoughtcrime/securesms/webrtc/audio/BluetoothStateManager.java b/src/org/thoughtcrime/securesms/webrtc/audio/BluetoothStateManager.java index b49a6fb358..558efe71f3 100644 --- a/src/org/thoughtcrime/securesms/webrtc/audio/BluetoothStateManager.java +++ b/src/org/thoughtcrime/securesms/webrtc/audio/BluetoothStateManager.java @@ -19,6 +19,7 @@ import org.thoughtcrime.securesms.logging.Log; import org.thoughtcrime.securesms.util.ServiceUtil; import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; public class BluetoothStateManager { @@ -37,6 +38,7 @@ public class BluetoothStateManager { private BluetoothScoReceiver bluetoothScoReceiver; private BluetoothConnectionReceiver bluetoothConnectionReceiver; private final BluetoothStateListener listener; + private final AtomicBoolean destroyed; private BluetoothHeadset bluetoothHeadset = null; private ScoConnection scoConnection = ScoConnection.DISCONNECTED; @@ -48,6 +50,7 @@ public class BluetoothStateManager { this.bluetoothScoReceiver = new BluetoothScoReceiver(); this.bluetoothConnectionReceiver = new BluetoothConnectionReceiver(); this.listener = listener; + this.destroyed = new AtomicBoolean(false); if (this.bluetoothAdapter == null) return; @@ -66,6 +69,8 @@ public class BluetoothStateManager { } public void onDestroy() { + destroyed.set(true); + if (bluetoothHeadset != null && bluetoothAdapter != null) { this.bluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, bluetoothHeadset); } @@ -137,6 +142,11 @@ public class BluetoothStateManager { @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB) @Override public void onServiceConnected(int profile, BluetoothProfile proxy) { + if (destroyed.get()) { + Log.w(TAG, "Got bluetooth profile event after the service was destroyed. Ignoring."); + return; + } + if (profile == BluetoothProfile.HEADSET) { synchronized (LOCK) { bluetoothHeadset = (BluetoothHeadset) proxy;