audioswitch icon indicating copy to clipboard operation
audioswitch copied to clipboard

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

Open neubami94 opened this issue 2 years ago • 5 comments

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

  • Version: (e.g. 1.1.4)

Android Device (please complete the following information):

  • Device: Pixel 3a
  • API Version: 31

neubami94 avatar Mar 25 '22 15:03 neubami94

We are currently dealing with the same issue.

zelenmi6 avatar Mar 28 '22 11:03 zelenmi6

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 avatar Mar 28 '22 16:03 neubami94

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)
        }
    }
}

neubami94 avatar Apr 06 '22 13:04 neubami94

@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

tronku avatar Jul 21 '22 20:07 tronku

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

neubami94 avatar Aug 05 '22 11:08 neubami94

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

davidliu avatar Dec 19 '22 20:12 davidliu

@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.

afalls-twilio avatar Jan 26 '23 23:01 afalls-twilio