I've implemented this solution to get AudioRecord working with bluetooth earpieces.
This works well until no sound is transferred to and from the bluetooth device, however if there is no sound output, and no microphone output for a few seconds, the micro
phone stops listening.
I tried to listen almost all intent actions, but I have nothing when the microphone stops.
How could I get a message on this kind of timeout, or how could I keep this connection on ?
Related
I'm trying to correctly setup bluetooth SCO in my app. After many attempts it seems that I've found a way to enable it "almost" reliably, in the sense that it works almost always, but in one case: if I launch my app and AFTER this I pair the headset, the headset mic is not picked (the phone mic is picked instead), while the headset earphones are correctly picked.
If instead I pair the headset BEFORE launching the app, everything seems to work fine.
The strange thing is that, even when the phone mic is picked, calling getRoutedDevice() on my AudioRecord object returns the bluetooth headset.
I tried to use SetPreferredDevice on the AudioRecord object, but apparently it has no effect.
The only way I found to ALWAYS pick the headset microphone is to call
audioManager.setMode(AudioManager.MODE_NORMAL);
However in this case, the output comes from the phone speaker and not from the BT headset earphones!
If instead i call:
audioManager.setMode(AudioManager.MODE_IN_CALL);
the output comes consistently (and correctly) from the BT headset earphones, but I have the above mentioned mic issue (pairing the headset after launching the app, the phone mic is picked instead of the BT headset one).
The sequence I am using is:
I have a thread that checks if a bluetooth headset is connected and works pretty much reliably.
When isHeadsetConnected is true I do:
arec.stop(); // my AudioRecord object
atrack.stop(); // my AudioTrack object
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.startBluetoothSco();
In the listener of AudioManager.EXTRA_SCO_AUDIO_STATE, when I receive SCO_AUDIO_STATE_CONNECTED) I do:
audioManager.setBluetoothScoOn(true);
atrack.play();
arec.startRecording();
After struggling a lot, this seems to be the "almost" perfect sequence, with the caveat that it does not work fully if the first pairing is made with the application running.
I've made an attempt to pair the same headset during a Whatsapp call, and the bluetooth headset starts without any problem both input and output, so THERE MUST be a way to accomplish this!
I have read tons of questions/answers but none of these seems to consistently solve my problem.
I am using Android 11 on a CAT S62 Pro.
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.
I am developing an app which requires that I play a song in two phones via Bluetooth. At the time I am able to do so, but with a delay in playing sounds.
The way I am doing this is by sending a string over bluetooth which when sent tells the app to play a song and when received do the same.
Of course there will be a delay because of the bluetooth connection but the main problem is that this delay is not constant at all so I am having trouble in synchronize both well..
Any suggestions will be really appreciated :)
I've managed to get text-to-speech audio streaming through my SCO headset using startBluetoothSco(), but it's not interrupted by incoming phone calls, as documented at http://developer.android.com/reference/android/media/AudioManager.html:
Note that the phone application always has the priority on the usage of the SCO connection for telephony. If this method is called while the phone is in call it will be ignored. Similarly, if a call is received or sent while an application is using the SCO connection, the connection will be lost for the application and NOT returned automatically when the call ends.
When a call comes in, I can hear both the phone call audio and my text-to-speech audio at the same time. Has anyone successfully gotten the correct behavior with incoming phone calls? I'm testing on a Samsung Captivate running Android 2.2. Thanks!
What stream are you using? Make sure you are not using VOICE_CALL stream.
How exactly does Android handle the transition to a bluetooth headset when the connection is made? With the default music application, my device stops the audio from coming out the speaker, then routes it to the headphones as soon as a connection is made. When turning off the bluetooth headset, the device immediately detects the lost connection and starts routing through the device speaker again. Is this handled by the Android OS or by the application itself?
I ask because I am working on an app that plays streams off the internet. My app only behaves as described above 50% of the time. Sometimes when connecting the bluetooth headphones the audio does not automatically switch until I stop the output and start it again. The same goes for a disconnection. The device detects that the headphones are gone, but my audio does not automatically return back through the speakers until I restart the stream.