Mute voice recognition bleep in Bluetooth Earpiece Android - android

I'm trying to get continuous voice recognition to work for a custom app on Android, using an ML18 bluetooth headset (by plantronics). I've managed to get rid of the beep produced by my phone by using the answers posted here and managed to route all other media to my bluetooth device using this.
However my bluetooth device is still beeping every time I call SpeechRecognizer.startListening.
Is there something I can do about this, or is this possibly hardwired into the software of the bluetooth device?
If you want to see specific sections of code, let me know. I do not think much of it is relevant however, as the two links above already cover the code that seems most relevant to me. Thanks in advance!
EDIT: So based on the comment below, I've modified my code into the following:
The start listening simply mutes all stream but the alarm stream (undone by unmuting all streams again).
public void startListening() {
if (canListen) {
Log.v(TAG, "Starting Listening");
Intent recognizerIntent = createRecognizerIntent();
recognizer.cancel();
recognizer.startListening(recognizerIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
((AudioManager) getApplicationContext().getSystemService(
Context.AUDIO_SERVICE)).setStreamSolo(
AudioManager.STREAM_ALARM, true);
}
}
}
The Bluetooth receiver does this when the device is connected:
AudioManager localAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
localAudioManager.setMode(AudioManager.MODE_NORMAL);
localAudioManager.setBluetoothScoOn(true);
localAudioManager.startBluetoothSco();
And then in the ACTION_SCO_AUDIO_STATE_UPDATED it sets the mode to AudioManager.MODE_IN_CALL.
Currently the behaviour is as follows:
When I first start the app and connect the bluetooth device, as well as trigger the continuous recognition loop, all works as expected. Yet after a first reply has been uttered (using TTS), the beep returns... Again, any help would be appreciated :)

Related

How do I detect skype/telegram/whatsapp calls when my messenger app is in a call or wants to make a call?

I am wondering if there is a way I can detect a skype call/telegram/whatsapp/fb messenger call etc in progress or incoming call etc just like the regular phone call with telephony manager/listener? I would like to have some kind of mechanism that detects an ongoing /incoming etc call from skype/telegram etc for my app. I came across this solution :Call Detection for skype in android but not sure if it'll work for all generic messenger apps. Is there a hack or any kind of listener I can implement on my side that allows me to detect these? Any ideas would be awesome.
Thanks!
You can use the AudioManager system service and check for audio mode using AudioManager#getMode(). According to the docs, AudioManager#getMode() returns the current audio mode which can be one of following:
MODE_NORMAL (0x00000000): Normal audio mode: not ringing and no call established.
MODE_RINGTONE (0x00000001): Ringing audio mode. An incoming is being signaled.
MODE_IN_CALL (0x00000002): In call audio mode. A telephony call is established.
MODE_IN_COMMUNICATION (0x00000003): In communication audio mode. An audio/video chat or VoIP call is established (added in API 11).
Using AudioManager#getMode() you can check if any other app is currently holding an audio focus of type MODE_IN_COMMUNICATION and handle your call accordingly.
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
final int mode = am.getMode();
if(AudioManager.MODE_IN_CALL == mode){
// device is in a telephony call
} else if(AudioManager.MODE_IN_COMMUNICATION == mode){
// device is in communiation mode, i.e. in a VoIP or video call
} else if(AudioManager.MODE_RINGTONE == mode){
// device is in ringing mode, some incoming is being signalled
} else {
// device is in normal mode, no incoming and no audio being played
}
P.S. I've tested this with whatsapp messenger and it works! Cannot say the same for other messenger apps.

How to play audio through Bluetooth speaker even when headset is plugged in?

I have my phone connected to a Bluetooth speaker and the headphones plugged in. Now I'd like to play audio through the Bluetooth speaker.
When I set the audio stream to AudioManager.STREAM_MUSIC it just plays over the headphones.
It doesn't matter if it plays on the headphones as well but I need it to play on the Bluetooth speaker.
How is this possible? The app SoundAbout manages to do that so there must be a way.
EDIT: When I plug in the headphones and only afterwards connect to the Bluetooth speakers all audio plays through the Bluetooth speakers which I want. But I can't expect the user to find that out and before having to show them a complicated message I'd rather find out a way to make the sound always play through BT speakers when connected to some.
Thanks
(Note this is not the same question as this: How to Play audio through speaker even when headset is plugged in?
I want it to play on Bluetooth speakers, not on the integrated speaker of the phone.)
Solution
Suppose you already tested STREAM_RING on your new instance of media player and not directly setting stream type, and it didn't work out, You need a correct profile for your bluetooth device.
Take a look at this article
Read the "Implementing HAL" section, there is alot of source for different profile that you may be able to use.
There is also an easy solution which is to change your device profile to HEADSET in your getServiceConnected() method, it will turn into a Stay connected device but the output will become mono! As I recall, Which is a shame for speakers, A2DP also may not be supported in some hardwares and still interrupted by wire headsets.
I suggest to create a new profile and use it, a little bit tricky working with HAL but will worth it,
Sorry that I can not provide a source code for you at the moment.
If you have your routing logic within your application then based on that you can decide on which output the audio to be played.
I have a test app written for the exact purpose.
My Github Link
You can also route audio as you want based on a requirement.You can refer this github link for routing
Bluetooth connection may work with below state is true .
After receive BluetoothA2dp.STATE_CONNECTED, you can play music as normal.
Java Code Examples for android.bluetooth.BluetoothA2dp.STATE_CONNECTED
public BluetoothHandsfree(Context context, CallManager cm) {
mCM = cm;
mContext = context;
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
boolean bluetoothCapable = (adapter != null);
mHeadset = null; // nothing connected yet
mA2dp = new BluetoothA2dp(mContext);
mA2dpState = BluetoothA2dp.STATE_DISCONNECTED;
mA2dpDevice = null;
mA2dpSuspended = false;
mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
mStartCallWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
TAG + ":StartCall");
mStartCallWakeLock.setReferenceCounted(false);
mStartVoiceRecognitionWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
TAG + ":VoiceRecognition");
mStartVoiceRecognitionWakeLock.setReferenceCounted(false);
mLocalBrsf = BRSF_AG_THREE_WAY_CALLING |
BRSF_AG_EC_NR |
BRSF_AG_REJECT_CALL |
BRSF_AG_ENHANCED_CALL_STATUS;
if (sVoiceCommandIntent == null) {
sVoiceCommandIntent = new Intent(Intent.ACTION_VOICE_COMMAND);
sVoiceCommandIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
if (mContext.getPackageManager().resolveActivity(sVoiceCommandIntent, 0) != null &&
BluetoothHeadset.isBluetoothVoiceDialingEnabled(mContext)) {
mLocalBrsf |= BRSF_AG_VOICE_RECOG;
}
mBluetoothPhoneState = new BluetoothPhoneState();
mUserWantsAudio = true;
mPhonebook = new BluetoothAtPhonebook(mContext, this);
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
cdmaSetSecondCallState(false);
if (bluetoothCapable) {
resetAtState();
}
}
please find below links : with sample codes it may help you.
Java Code Examples for android.bluetooth.BluetoothHeadset
Programmatically connect to paired Bluetooth speaker and play audio
You need to instantiate a new object of class MediaPlayer and use the following method on it
amediaplayer.setAudioStreamType(AudioManager.STREAM_RING)
Do not forget to check authorization to use bluetooth, you are unable to send anything to speaker via bluetooth without user privilege as you know.
Audiomanager overrides and routes audio to the latest connected device(either wired headset or bluetooth headset). In android, we do not have any option to override this setting unless it is a system app and route the audio wherever we wish to route. However, you can use reflection apis and override this setting. Audiomanager suspends bluetooth connection route(if already connected)if wired headset is connected and vice versa. You can look at the code here.
Hence using reflection apis you can toggle bluetooth audio route by invoking this method.

How to stream a audio of incoming call via bluetooth in Android

I am able to receive, hangup and dial a number from my headset to android mobile which are connected via bluetooth.
Now, once I receive a in-coming call of mobile in headset, I need to speak with the called person from headset. I need to stream my voice via bluetooth to android mobile. I am searching for this code.
Crawled n number of post but could not get appropriate answer, Currently am using below but its not working...
AudioManager audioManager;
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
audioManager.startBluetoothSco();
audioManager.setBluetoothScoOn(true);
audioManager.setSpeakerphoneOn(true);
I am not sure after this what line of code I need to write to achieve my requirement
Note: I receive in-coming call by sending mmOutStream.write("ATA\r".getBytes()); this AT command to my Android mobile.
Please correct and guide me to perform audio stream.
You should set speakerphone to false, otherwise your output will be re-directed there. It is enough to do this:
audioManager.setSpeakerphoneOn(false);
audioManager.startBluetoothSco();
audioManager.setBluetoothScoOn(true);
Also, you can optionally set microphone to un-mute state (just in case previous state was muted):
mAudioManager.setMicrophoneMute(false);

Route audio to bluetooth A2DP device while ringing [Android]

I am trying to identify how to route a very short audio stream (a notification) to a bluetooth headphone that is already paired with the device, while the device is ringing.
When I play any audio at any time, it is routed to the bluetooth device, no problem.
But if I try to start playing the audio when receiving an android.intent.action.PHONE_STATE, in RINGING state, the audio is not routed as expected.
I can see that the AudioManager's setBluetoothA2dpOn method has became deprecated, but I actually tried it but is seems has no effect.
I have tried the MediaRouter object, but I can see that MediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_AUDIO) points to the RouteInfo of the Bluetooth device while the device is ringing, and the AudioManager.isBluetoothA2dpOn is true.
So, can any one tell me why the audio route is like this in the ringing moment? is there any way to force the audio to be routed to the Bluetooth device in such case?
[UPDATED]
I have tried again today and I have discovered something that may be the cause of the problem.
I have created a BroadcaseReceiver to detect the change in the android.intent.action.PHONE_STATE. if an intent is received and the state is currently ringing, check for AudioManager's mode and you will find it is MODE_NORMAL. but few seconds later the phone will start actually ringing and the mode is going to be changed into MODE_RINGTONE. trying to manually set the mode using the method setMode(AudioManager.MODE_NORMAL) is useless then, the state remains MODE_RINGTONE even after setting it to MODE_NORMAL.
Now, I think the cause of the problem is that in the MODE_RINGTONE mode, all the streams are directed to the phone speaker and here there is no way offered by the android system to change the mode.
I think the Media player realease the bluetooth connection when the phone is ringing. You can try to obtain the bluetooth audio connection and see if Media player now play through your obtained connection. You can use my class at my answer Using the Android RecognizerIntent with a bluetooth headset and see if it works. The audio in the class is Sco only.
As stated in the JavaDoc for the StartBluetoothSco method:
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.`
I tried to start sco then play a music clip in normal mode, it played to the Bluetooth headset without problems, although I couldnot stop the microphone that caused the input stream plays to the headset. I then tried to call my target phone from another phone while the music is still playing, I found that the stream has got redirected automatically to the phone speaker. After the ringing mode is finished, the stream did not get redirected again to the Bluetooth headset and I think that behavior is normal according to what is stated in the JavaDoc above.
My guessing is that Android tries to protect the ringing and in-call modes as possible in order not to allow any unwanted interference from applications. In other words, when in ringing mode then no sound is going to be played to the headset until the call is accepted, and you cannot even change the AudioManager mode from ringing to another mode, your call for mode setter will be ignored.
I have tried the AudioTrack instead of MediaPlayer, but that makes no difference.
I have then tried the TextToSpeech engine like this:
in the main activity, initialize on create:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textToSpeech=new TextToSpeech(this, this);
textToSpeech.setLanguage(Locale.US);
textToSpeech.addEarcon("[wwww]", "XXXXXXXXXXXXXXXXXXXXXXX", R.raw.a);
}
in the broadcast receiver class when rining starting the Bluetooth utility class and adding the below to the onScoAudioConnected method
textToSpeech.playEarcon("[wwww]", TextToSpeech.QUEUE_FLUSH, null);
This did not work too.

Routing audio to Bluetooth Headset (non-A2DP) on Android

I have a non-A2DP single ear BT headset (Plantronics 510) and would like to use it with my Android HTC Magic to listen to low quality audio like podcasts/audio books.
After much googling I found that only phone call audio can be routed to the non-A2DP BT headsets. (I would like to know if you have found a ready solution to route all kinds of audio to non-A2DP BT headsets)
So I figured, somehow programmatically I can channel the audio to the stream that carries phone call audio. This way I will fool the phone to carry my mp3 audio to my BT headset. I wrote following simple code.
import android.content.*;
import android.app.Activity;
import android.os.Bundle;
import android.media.*;
import java.io.*;
import android.util.Log;
public class BTAudioActivity extends Activity
{
private static final String TAG = "BTAudioActivity";
private MediaPlayer mPlayer = null;
private AudioManager amanager = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
amanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
amanager.setBluetoothScoOn(true);
amanager.setMode(AudioManager.MODE_IN_CALL);
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(new FileInputStream(
"/sdcard/sample.mp3").getFD());
mPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
mPlayer.prepare();
mPlayer.start();
} catch(Exception e) {
Log.e(TAG, e.toString());
}
}
#Override
public void onDestroy()
{
mPlayer.stop();
amanager.setMode(AudioManager.MODE_NORMAL);
amanager.setBluetoothScoOn(false);
super.onDestroy();
}
}
As you can see I tried combinations of various methods that I thought will fool the phone to believe my audio is a phone call:
Using MediaPlayer's setAudioStreamType(STREAM_VOICE_CALL)
using AudioManager's setBluetoothScoOn(true)
using AudioManager's setMode(MODE_IN_CALL)
But none of the above worked. If I remove the AudioManager calls in the above code, the audio plays from speaker and if I replace them as shown above then the audio stops coming from speakers, but it doesn't come through the BT headset. So this might be a partial success.
I have checked that the BT headset works alright with phone calls.
There must be a reason for Android not supporting this. But I can't let go of the feeling that it is not possible to programmatically reroute the audio. Any ideas?
P.S. above code needs following permission
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
This thread may be long dead but for those who might be trying the same thing, some notes from the AudioManager docs may be useful. It looks like the missing element is the startBluetoothSco() command but there are restrictions on the use of this channel. From the Android Dev site here:
public void startBluetoothSco () Since: API Level 8 Start bluetooth SCO
audio connection.
Requires Permission:
MODIFY_AUDIO_SETTINGS.
This method can be used by
applications wanting to send and
received audio to/from a bluetooth SCO
headset while the phone is not in
call.
As the SCO connection establishment
can take several seconds, applications
should not rely on the connection to
be available when the method returns
but instead register to receive the
intent ACTION_SCO_AUDIO_STATE_CHANGED
and wait for the state to be
SCO_AUDIO_STATE_CONNECTED.
As the connection is not guaranteed to
succeed, applications must wait for
this intent with a timeout.
When finished with the SCO connection
or if the establishment times out, the
application must call
stopBluetoothSco() to clear the
request and turn down the bluetooth
connection.
Even if a SCO connection is
established, the following
restrictions apply on audio output
streams so that they can be routed to
SCO headset: - the stream type must be
STREAM_VOICE_CALL - the format must be
mono - the sampling must be 16kHz or
8kHz
The following restrictions apply on
input streams: - the format must be
mono - the sampling must be 8kHz
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.
See Also stopBluetoothSco()
ACTION_SCO_AUDIO_STATE_CHANGED
Note that I have not tested this, I'm just passing along a lead I found in researching a similar project. I think Jayesh was close to the solution and the restrictions above may have been what was keeping it from working.
To turn on:
localAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
localAudioManager.setMode(0);
localAudioManager.setBluetoothScoOn(true);
localAudioManager.startBluetoothSco();
localAudioManager.setMode(AudioManager.MODE_IN_CALL);
To turn off:
localAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
localAudioManager.setBluetoothScoOn(false);
localAudioManager.stopBluetoothSco();
localAudioManager.setMode(AudioManager.MODE_NORMAL);
I took it from here
Great Work it is working fine for me please do bit modification in your code it'll work perfectly i.e.
mPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
to
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
There should be a ALSA route path configured for Media player, so that it can open a different audio path and then route your audio to BT headset.
Cannot see a clear accepted working application so putting a new answer. This application routes music and audio to Non A2dp headsets. Try my application and find the source code also in github for reference code. https://github.com/sauravpradhan/AnySound2BT

Categories

Resources