Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to configure session to use Bluetooth headset mic on Android? #106

Open
sveinbjornt opened this issue Sep 6, 2023 · 6 comments
Open

Comments

@sveinbjornt
Copy link

I've been struggling to get audio input via Bluetooth headset working on Android:

  final session = await AudioSession.instance;
  // await session.configure(AudioSessionConfiguration.speech());

  await session.configure(AudioSessionConfiguration(
    avAudioSessionCategory: AVAudioSessionCategory.playAndRecord,
    avAudioSessionCategoryOptions: AVAudioSessionCategoryOptions.defaultToSpeaker |
        AVAudioSessionCategoryOptions.allowBluetooth,
    //     AVAudioSessionCategoryOptions.duckOthers,
    // avAudioSessionMode: AVAudioSessionMode.spokenAudio,
    avAudioSessionMode: AVAudioSessionMode.defaultMode,
    avAudioSessionRouteSharingPolicy: AVAudioSessionRouteSharingPolicy.defaultPolicy,
    avAudioSessionSetActiveOptions: AVAudioSessionSetActiveOptions.none,
    androidAudioAttributes: const AndroidAudioAttributes(
      contentType: AndroidAudioContentType.speech,
      flags: AndroidAudioFlags.none,
      usage: AndroidAudioUsage.voiceCommunication,
    ),
    androidAudioFocusGainType: AndroidAudioFocusGainType.gain,
    androidWillPauseWhenDucked: true,
  ));
  await session.setActive(true);

```

Everything works fine on iOS, but no matter how I tweak the Android settings (which seem quite limited compared to iOS AVAudioSession), I cannot get audio input via the Bluetooth headset microphone. Anyone have any suggestions?
@mdk6309
Copy link

mdk6309 commented Jan 22, 2024

Hi.
I have the same issue. I try flutter_sound, but does not work. Have you solve this?
Thanks

@bilalahmed24
Copy link

In order to receive audio from Bluetooth microphone in Android, you have to implement the audio capture at native side while managing the Bluetooth remote device connect and disconnection by listening through ACTION_CONNECTION_STATE_CHANGED events while also managing the mobile Bluetooth disconnection through ACTION_STATE_CHANGED events.

In order to capture audio, you should implement AudioRecord class. Before calling the startRecording method of AudioRecord, call startBluetoothSco then setBluetoothScoOn(true) of AudioManager.

When Bluetooth remote device is reconnected after a disconnection, you have to wait for STATE_CONNECTED event then stop the audio recording(AudioRecord class) and add a delay of 5secs(thread sleep) so that device is connected and ready before starting the recording again.

When done remember to clean up the event listeners and calling stopBluetoothSco() setBluetoothScoOn(false) of AudioManager.

Hope this helps :)

@ryanheise
Copy link
Owner

That sounds like something that a good audio recorder plugin should manage.

The record plugin has a feature request for it: llfbandit/record#84 . There was also a PR llfbandit/record#225 which was later closed by its author 🤷

@bilalahmed24
Copy link

@ryanheise Agreed, it's out of scope for audio_session.

I am facing an issue; I think you can suggest something on it.
I am using audio_session for handling interruptions by subscribing to 'interruptionEventStream' but when the audio is interrupted by apps like music player, I receive unknown audio interruption type and audioFocus is lost and as such I don't receive further interruption events unless I set the audio_session to active again basically regaining audioFocus.

I was looking for a way I can listen to some event that would inform me that audioFocus is now available and not being used by other app so I can safely request for audioFocus. Reason being gaining audioFocus if other app is using it, will cause the music player to stop abruptly.

@ryanheise
Copy link
Owner

@bilalahmed24 I don't think the API supports this by design. If Android wants to tell you when the audio focus is available again, it will give you type pause instead of unknown. But rather than try to offer advice, I would say that this plugin will wrap whatever is in the original API, and so if you refer to the original Android API documentation and find a way to do something that is not possible in this wrapper, please let me know and I will add the missing API to this wrapper.

@bilalahmed24
Copy link

@ryanheise I believe this configuration, will provide the ability for app to regain audio focus: setAcceptsDelayedFocusGain

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants