amazon-chime-sdk-android icon indicating copy to clipboard operation
amazon-chime-sdk-android copied to clipboard

fix(audio): Defer Bluetooth audio route changes until connection is r…

Open avinashmidathada opened this issue 4 months ago • 0 comments

ℹ️ Description

When switching to a Bluetooth audio device, the SDK was changing the audio route before the Bluetooth connection was established. This caused a race condition where Android would reject Bluetooth routing and fall back to Handset.

This fix introduces the BluetoothAudioRouter abstraction to encapsulate Bluetooth connection management. The router defers route changes until the Bluetooth connection is confirmed ready, with implementations selected via BluetoothAudioRouterFactory based on API level. This design isolates the complex asynchronous Bluetooth handling from DefaultDeviceController and allows each platform path to be tested independently.

ScoBluetoothAudioRouter (API < 31):

  • Monitor ACTION_SCO_AUDIO_STATE_UPDATED broadcasts for connection state
  • Execute deferred setRoute() only when SCO reaches CONNECTED state
  • Handle SCO connection failures and timeouts (3 seconds)
  • Distinguish connection failure from device switching for multi-device support

CommunicationDeviceBluetoothAudioRouter (API 31+):

  • Use OnCommunicationDeviceChangedListener for connection confirmation
  • Retry on mismatch when setCommunicationDevice() succeeds but listener reports a different device (500ms delay, max 3 retries)
  • 5-second timeout for device switching per Android documentation

Additional changes:

  • Update MediaDevice to track device IDs for API 31+ routing
  • Add test coverage for both API paths

Issue #, if available

Type of change

  • [X] Bug fix (non-breaking change which fixes an issue)
  • [ ] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • [ ] This change requires a documentation update
    • [ ] README update
    • [ ] CHANGELOG update
    • [ ] guides update
  • [ ] This change requires a dependency update
    • [ ] Amazon Chime SDK Media
    • [ ] Other (update corresponding legal documents)

🧪 How Has This Been Tested?

Manually verified with a Pixel 8 device running Android

Unit test coverage

Added comprehensive unit tests

Additional Manual Test

  • [ ] Pause and resume remote video
  • [ ] Switch local camera
  • [ ] Rotate screen back and forth

📱 Screenshots, if available

provide screenshots/video record if there's a UI change in demo app

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

avinashmidathada avatar Dec 06 '25 01:12 avinashmidathada