diff --git a/android/app/src/com/android/bluetooth/a2dp/A2dpService.java b/android/app/src/com/android/bluetooth/a2dp/A2dpService.java index 061f96ee144351c6d8e366f5e788e1b9c3b073f2..7e4d791185d079bbf17c8af268e58bfb05ffb3e3 100644 --- a/android/app/src/com/android/bluetooth/a2dp/A2dpService.java +++ b/android/app/src/com/android/bluetooth/a2dp/A2dpService.java @@ -49,7 +49,9 @@ import android.media.BluetoothProfileConnectionInfo; import android.os.Binder; import android.os.Build; import android.os.Bundle; +import android.os.Handler; import android.os.HandlerThread; +import android.os.Looper; import android.sysprop.BluetoothProperties; import android.util.Log; @@ -87,6 +89,7 @@ public class A2dpService extends ProfileService { private AdapterService mAdapterService; private DatabaseManager mDatabaseManager; private HandlerThread mStateMachinesThread; + private Handler mHandler = null; private final A2dpNativeInterface mNativeInterface; @VisibleForTesting @@ -167,6 +170,8 @@ public class A2dpService extends ProfileService { Log.i(TAG, "Max connected audio devices set to " + mMaxConnectedAudioDevices); // Step 3: Start handler thread for state machines + // Setup Handler. + mHandler = new Handler(Looper.getMainLooper()); mStateMachines.clear(); mStateMachinesThread = new HandlerThread("A2dpService.StateMachines"); mStateMachinesThread.start(); @@ -1248,6 +1253,10 @@ public class A2dpService extends ProfileService { } } + void handleConnectionStateChanged(BluetoothDevice device, int fromState, int toState) { + mHandler.post(() -> connectionStateChanged(device, fromState, toState)); + } + void connectionStateChanged(BluetoothDevice device, int fromState, int toState) { if ((device == null) || (fromState == toState)) { return; diff --git a/android/app/src/com/android/bluetooth/a2dp/A2dpStateMachine.java b/android/app/src/com/android/bluetooth/a2dp/A2dpStateMachine.java index 7a69e216d77ccce1dfa066406e58784e50f74acd..1a58e200c62b614db1350326e1cfd412eacae7cf 100644 --- a/android/app/src/com/android/bluetooth/a2dp/A2dpStateMachine.java +++ b/android/app/src/com/android/bluetooth/a2dp/A2dpStateMachine.java @@ -735,7 +735,7 @@ final class A2dpStateMachine extends StateMachine { intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice); intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); - mA2dpService.connectionStateChanged(mDevice, prevState, newState); + mA2dpService.handleConnectionStateChanged(mDevice, prevState, newState); Utils.sendBroadcast(mA2dpService, intent, BLUETOOTH_CONNECT, Utils.getTempAllowlistBroadcastOptions()); }