twilio / audioswitch

An Android audio management library for real-time communication apps.
Apache License 2.0
160 stars 49 forks source link

audioDevices do not update after granting bluetooth permission (Android 12) #118

Closed neubami94 closed 1 year ago

neubami94 commented 2 years ago

Describe the bug audioDevices do not update after granting bluetooth permission (Android 12)

To Reproduce Steps to reproduce the behavior:

  1. turn on bluetooth on your android 12 device and connect to bluetooth audio device
  2. open app and grant BLUETOOTH_CONNECT permission
  3. call audioSwitch.start
  4. audioDevices contain Bluetooth headset ✅
  5. remove permission, app restarts automatically
  6. audioDevices now do not contain Bluetooth headset ✅ BUT
  7. grant permission again, app does not restart automatically
  8. audioDevices still do not contain Bluetooth headset ❌

AND

  1. turn on bluetooth on your android 12 device and connect to bluetooth audio device
  2. open app and call audioSwitch.start
  3. grant BLUETOOTH_CONNECT permission
  4. audioDevices still do not contain Bluetooth headset ❌

Expected behavior audioDevices emit and contain Bluetooth headset

Actual behavior audioDevices do not emit and do not contain Bluetooth headset

Application Logs

2022-03-25 16:02:47.408 24131-24131/com.nn.my2ncommunicator.dev.debug D/AS/AudioSwitch: AudioSwitch(1.1.4)
2022-03-25 16:02:47.409 24131-24131/com.nn.my2ncommunicator.dev.debug D/AS/AudioSwitch: Preferred device list = [BluetoothHeadset, WiredHeadset, Earpiece, Speakerphone]
2022-03-25 16:02:47.429 24131-24171/com.nn.my2ncommunicator.dev.debug W/AS/BluetoothHeadsetManager: Bluetooth unsupported, permissions not granted
2022-03-25 16:02:47.432 24131-24171/com.nn.my2ncommunicator.dev.debug W/AS/BluetoothHeadsetManager: Bluetooth unsupported, permissions not granted
2022-03-25 16:02:47.433 24131-24171/com.nn.my2ncommunicator.dev.debug D/AS/AudioDeviceManager: Earpiece available
2022-03-25 16:02:47.445 24131-24171/com.nn.my2ncommunicator.dev.debug D/AS/AudioDeviceManager: Speakerphone available
2022-03-25 16:02:47.446 24131-24171/com.nn.my2ncommunicator.dev.debug D/AS/AudioSwitch: Available AudioDevice list updated: [Earpiece(name=Earpiece), Speakerphone(name=Speakerphone)]
2022-03-25 16:02:47.446 24131-24171/com.nn.my2ncommunicator.dev.debug D/AS/AudioSwitch: Current user selected AudioDevice = null

---- Permission granted here ------

2022-03-25 16:03:01.830 24131-24171/com.nn.my2ncommunicator.dev.debug W/AS/BluetoothHeadsetManager: Cannot deactivate when in the Disconnected state
2022-03-25 16:03:01.837 24131-24171/com.nn.my2ncommunicator.dev.debug D/AS/AudioSwitch: Selected AudioDevice = Speakerphone(name=Speakerphone)
2022-03-25 16:03:01.837 24131-24171/com.nn.my2ncommunicator.dev.debug W/AS/BluetoothHeadsetManager: Bluetooth unsupported, permissions not granted
2022-03-25 16:03:01.837 24131-24171/com.nn.my2ncommunicator.dev.debug D/AS/AudioDeviceManager: Earpiece available
2022-03-25 16:03:01.848 24131-24171/com.nn.my2ncommunicator.dev.debug D/AS/AudioDeviceManager: Speakerphone available
2022-03-25 16:03:01.848 24131-24171/com.nn.my2ncommunicator.dev.debug D/AS/AudioSwitch: Available AudioDevice list updated: [Earpiece(name=Earpiece), Speakerphone(name=Speakerphone)]
2022-03-25 16:03:01.848 24131-24171/com.nn.my2ncommunicator.dev.debug D/AS/AudioSwitch: Current user selected AudioDevice = Speakerphone(name=Speakerphone)
2022-03-25 16:03:01.849 24131-24171/com.nn.my2ncommunicator.dev.debug W/AS/BluetoothHeadsetManager: Cannot deactivate when in the Disconnected state
2022-03-25 16:03:17.557 24131-24171/com.nn.my2ncommunicator.dev.debug W/AS/BluetoothHeadsetManager: Cannot deactivate when in the Disconnected state
2022-03-25 16:05:22.182 24131-24171/com.nn.my2ncommunicator.dev.debug W/AS/BluetoothHeadsetManager: Cannot deactivate when in the Disconnected state
2022-03-25 16:05:27.259 24131-24171/com.nn.my2ncommunicator.dev.debug W/AS/BluetoothHeadsetManager: Cannot deactivate when in the Disconnected state
2022-03-25 16:05:48.282 24131-24171/com.nn.my2ncommunicator.dev.debug W/AS/BluetoothHeadsetManager: Cannot deactivate when in the Disconnected state

AudioSwitch Version

Android Device (please complete the following information):

zelenmi6 commented 2 years ago

We are currently dealing with the same issue.

neubami94 commented 2 years ago

I tried calling audioSwitch.availableAudioDevices after granting the permissions and it does not contain the bluetooth device. So thats why it does not emit.

neubami94 commented 2 years ago

I hotfixed the issue by writing a bluetooth permission manager, observing when a permission is granted and then calling this:

@Synchronized
fun restart(): Completable {
    return Completable.fromCallable {
        logThread("Restart")
        audioSwitch.stop()
        audioSwitch.start { audioDevices, selectedAudioDevice ->
            selectedAudioDevice?.let { currentlyActiveOutput.onNext(selectedAudioDevice) }
            availableAudioOutputs.onNext(audioDevices)
        }
    }
}
tronku commented 1 year ago

@zelenmi6 any update on this?? i am still facing the error with 1.1.5 audioswitch in the listener I'm getting the Bluetooth device name but the audio is still coming from the earpiece and later on the listener also gives Earpiece

neubami94 commented 1 year ago

@tronku we ditched this twilio lib and implemented a custom solution

davidliu commented 1 year ago

There's no way for AudioSwitch to automatically know that the bluetooth permission is granted, so you'd need to manually restart it yourself.

afalls-twilio commented 1 year ago

@davidliu is correct. AudioSwitch expects the permissions it requires to be granted before it is started. If permissions are granted afterwards, then it should be restarted.

tech-ajit-mob commented 1 month ago

After granting the permission if we call the below code _audioSwitch.start { listAudioDevice: List<AudioDevice?>?, selectedDevice: AudioDevice? ->}

got error message: Redundant start() invocation while already in the started or activated state

How to restart the audio switch? @afalls-twilio @davidliu

davidliu commented 1 month ago

stop then start.

tech-ajit-mob commented 1 month ago

@davidliu Thank you so much it's working fine.

I have one more query regarding the audio switch. Might have you help me? Precondition: Bluetooth is connected When I select audio from Bluetooth to phone/speaker sometimes voice comes in speaker/phone and sometimes in Bluetooth.

For more info please check this one:https://github.com/twilio/audioswitch/issues/163