Is there a way to detect headphones natively? [duplicate] - android

I've got my code working using the isWiredHeadsetOn() method in AudioManager as long as I use the headset that came with my phone. If I use a pair of headphones without an inbuilt microphone, isWiredHeadsetOn() returns false, which I suppose is fair enough, but isn't what I want! Is there a way to detect both headsets and headphones?

The best way is to use a broadcast receiver for ACTION_HEADSET_PLUG. Then you get events when the headset is connected or disconnected.
I think it should work for bluetooth devices but am not 100% sure to be honest.

Related

why do we need to specify startBluetoothSco if android takes care of it itself?

what's the benefits of calling startBluetoothSco when android takes care of that on Androids end, and when you connect a BT headset to android the sound automatically passes to android
so why would we want to specify startBluetoothSco anyway?
is it just if we have a dedicated button to move to Bluetooth ? and then when it's pressed we can move to the headset ?
or not all android versions move to the headset automatically?
By default, nothing connects to a BT SCO(hands-free profile) headset automatically. You may be thinking of A2DP (stereo profile), which does get "auto-enabled".
You may also be confused because some headsets have both profiles, and the OS auto-switches to the A2DP one. But if you actually want to use SCO, it always has to be manually connected.

Android bluetooth sco not picking headset microphone after pairing

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.

How to know if phone call is on process

I need to know from glass, if there is a phone call in process. I can made it easy on android device ... but I have problems with glass, the same code to telephonymanager and so on ... doesn't works on glass.
Any idea how to catch the broadcasting process or PhoneCallManager to know if the phone has is talking?
thanks for advice.
Glass works as a bluetooth headset when making a phone call (see user guide). It is not making the calls itself.
I think you'll have to use the BluetoothHeadset class which will allow you to interact with the Headset bluetooth profile. You can then retrieve the state of the headset connection.

Android and bluetooth headset volume

My problem is - how to set up bluetooth incall audio volume.
Since there some apps for this, no one of them works.
Anyway - no app - create one for yourself. I'm very begginer in android development, so i've tried use audioManager.setStreamVolume(), but without any luck - only internal audio has changed after this.
Even playing with startBluetoothSCO() wasn't the answer.
After looking in logcat, how os responds to pressing volume keys, i found out, that after startBluetoothSCO() volume keys in my phone sends AT commands instead of mangling streams (AT+VGM=0...15).
So my code should look something like this:
AudioManager audioManager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
audioManager.startBluetoothSco();
// send AT commands
audioManager.stopBluetoothSco();
Crawling thru web provide me answer that there's no way to directly access modem (and propably other comm ports) from Android API.
So, maybe someone has any goddamn idea how to prevent resetting BT volume on each connect/disconnect to headset? Using code that i could use by myself, or using some 3-rd party app that is available somewhere.
OS: GB 2.3.4, SGS
Thanks for any ideas.
You can send AT commands via RFCOMM connection. But you must do this before the Android has created Headset/Handsfree Profile connection (Called Phone audio in bluetooth settings menu), or at least disconnect the HSP/HFP then connect via RFCOMM on either of the two (Both HSP and HFP should work with unsolicited +VGS commands), set the volume, and then reconnect.

Detected phone call taken with bluetooth headset or speaker on Android

I can already detect when an Android phone makes or takes a phone call, but I would also like to know if a bluetooth headset is being used for the call and/or if the phone's speaker is being used for the call. I have looked around a bit but I don't see anything that can do this. Is this possible?
I think you can use AudioManager's isBluetoothScoOn(), it should work fine.

Categories

Resources