Bluetooth SCO to single android application, not to entire android system - android

I am using following code in my application to use bluetooth headset for audio IN and OUT. It is working fine.
AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
am.startBluetoothSco();
am.setBluetoothScoOn(true);
But, When I played song using Music Player app, along with my application also running, the song is coming in bluetooth headset. This is because I changed the Audio Manager of android system to Bluetooth SCO.
But my requirement is the audio IN and OUT must work with bluetooth headset to my application only and it should not disturb other apps, Is it possible ?? or not ??
Please provide your valuable suggestions. Any help will be appreciated. Thanks.

When you add a SCO channel you are essentially adding an audio channel. Think of it like a wired handsfree. When you connect the handsfree all audio is routed to it and not just one specific applications's audio. To use the BT headset only for your app, connect the SCO channel when the app starts and disconnect when the app is exited or goes into background.

Related

On Android 11, audio is not playing for other apps while my app is connected to a Bluetooth speaker mic

My application allows for users to transmit and receive audio, similar to a walkie talkie. Much like a walkie talkie, there is not always audio being transmitted/received, and we handle the audio focus as appropriate. Users are able to do this using both the phones built-in speaker + mic, and Bluetooth connected devices which provide speaker or mic. On Android 11, whenever my app is active while a Bluetooth device is connected, audio from other applications does not play.
As an example, say the user is using my app with a Bluetooth speaker mic connected. They are able to correctly receive (hear) my app audio through the Bluetooth device, and transmit (speak) audio through the Bluetooth device. However, if they attempt to play audio from another app, like say a video on YouTube - the video plays but the audio does not come through; either through the Bluetooth device or the phone speaker.
This incorrect behavior is new to my app on Android 11; audio works properly on prior versions of Android. Likewise, audio works properly when there is no connected Bluetooth audio device; other app audio plays as expected through the phone speaker when my app is not receiving/transmitting audio.
In my research into this issue, it appears there are some long-standing issues with Bluetooth on Android 11, with regards to both Android Auto and audio devices generally. My app utilizes the AudioManager SCO states to determine when an audio device is connected, and I suspect that a change in Android 11 related to this area is the culprit.
I haven't found many programmatic discussions about this issue, so if anyone has any ideas or suggestions about things to try, I would be most appreciative. Thanks for your time.

speech recognition through bluetooth headset in android while playing music

I am currently recognizing Hot word(like "ok google") using pocketsphinx library.
The app works fine except that it always uses only the phone's mic to recognize the speech.
My use case is something like this:
I listen for a hot word and use the MediaPlayer to play some music.
Keep listening for next commands while keeping the music being played and react accordingly.
The app works fine and the music is played via the bluetooth headset and the voice is also simultaneously recognized, but it always uses the phone's mic. Even though the bluetooth headset is connected or not connected, it still uses the phone's mic.
I tried using:
AudioManager mAudioManager =
(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
mAudioManager.startBluetoothSco();
I tried using:
AudioManager mAudioManager =
(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
mAudioManager.startBluetoothSco();
In both the approaches, it cuts off the music and listens in bluetooth mic.
I need an approach where the Music player is not cut from bluetooth headset but still I am able to speech recognize from bluetooth headset.
For example:
I was listening to music via bluetooth headset.
I opened voice recording app(https://play.google.com/store/apps/details?id=com.andrwq.recorder)
I am able to record the audio through bluetooth headset even while listening to music.
This makes me feel that it is possible but I don't know how.
Please help me out. Thanks in advance.
It is correct to start SCO mode, after that pocketsphinx will listen for bluetooth headset.
To disable phone microphone while in sco mode use am.setMicrophoneMute(true);
To play audio through SCO you need the player to play through STREAM_VOICE_CALL. Maybe player must be restarted after SCO switch.

Android Bluetooth App audioManager issues

I am developing an app to control a speaker. I want the app to be able to playback music, do karaoke (play music and mic at same time), and provide a basic EQ to the user. My problem so far has been playing music through Bluetooth at all. My app: -discovers all Bluetooth speakers nearby -puts them in a list -pairs with clicked device and sends user to Bluetooth settings to connect to it
Now my problem is successfully playing music over the bluetooth speakers. I use a AudioManager and set it to play over SCO:
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.startBluetoothSco();
audioManager.setBluetoothScoOn(true);
And then try to play a music file, but this only plays over the devices speakers and not the connected Bluetooth speakers.
So my question is what am i missing? I read a bit about a A2DP sink but it seems to not be available in recent versions. Any suggestions? Thanks in advance.
TDLR: How do I playback music to paired Bluetooth speakers?
The startBluetoothSco() method returns before SCO is setup. You need to register a BroadcastReceiver for ACTION_SCO_AUDIO_STATE_UPDATED before calling the above method.
http://developer.android.com/reference/android/media/AudioManager.html#startBluetoothSco%28%29
it's a sticky intent to so you can always check the status before calling the setBluetoothScoOn(true).

android route specific audio to bluetooth headset

i'm trying to write an app that will get audio notifications and will stream them on a bluetooth headset. i want this done while other audio frames are playing through the device's speaker (for instance, there is a song playing on another app).
i only want the audio notification to play on the headset, other audio frames should still come out of the speaker.
right now, it doesn't matter whether the other audio frame will pause while the notification is played on the headset or not.
i've looked and found no example of how to do this.
is it at all possible?
can someone direct me?
with regards,
Tamir.

Routing input to Bluetooth headset in android

Is it possible to record an audio (not a phone call) using the bluetooth input and not the device microphone?
I am able to playback whatever am saying through device's mic but not able to figure out how to use the bluetooth mic instead.
Any assistance would be greatly appreciated.
(Edited)
I got it working by using following two lines of code
startBluetoothSco();
setBluetoothScoOn(true);
Got the bluetooth headset play back whatever am saying by using
STREAM_VOICE_CALL in audioTrack initialization.

Categories

Resources