From fc2ac5bfe8e5ca3738ce62ea7fe0b590dd66b1c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Rymanowski?= Date: Thu, 17 Mar 2022 12:14:30 +0000 Subject: Bluetooth: Fix common issue in registerCallback APIs This fixes race if callback is registered before BT is On. Bug: 150670922 Test: compile/manual Sponsor: @jpawlowski Change-Id: If7e9adba30ce0bba57acb503feb0f47c730da5d3 --- framework/java/android/bluetooth/BluetoothVolumeControl.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'framework/java/android/bluetooth/BluetoothVolumeControl.java') diff --git a/framework/java/android/bluetooth/BluetoothVolumeControl.java b/framework/java/android/bluetooth/BluetoothVolumeControl.java index a359c91206..080b9a6d64 100644 --- a/framework/java/android/bluetooth/BluetoothVolumeControl.java +++ b/framework/java/android/bluetooth/BluetoothVolumeControl.java @@ -344,6 +344,13 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose synchronized (mCallbackExecutorMap) { // If the callback map is empty, we register the service-to-app callback if (mCallbackExecutorMap.isEmpty()) { + if (!mAdapter.isEnabled()) { + /* If Bluetooth is off, just store callback and it will be registered + * when Bluetooth is on + */ + mCallbackExecutorMap.put(callback, executor); + return; + } try { final IBluetoothVolumeControl service = getService(); if (service != null) { @@ -355,7 +362,7 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose } catch (RemoteException e) { Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); throw e.rethrowFromSystemServer(); - } catch (IllegalStateException | TimeoutException e) { + } catch (TimeoutException e) { Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); } } -- cgit v1.2.3