Using the Android RecognizerIntent with a bluetooth headset - android

I use the following code to launch speech recognition in Android:
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() == 0) {
displayWarning("This device does not support speech recognition");
return;
}
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
This works fine. However, it doesn't seem to accept voice input from a bluetooth headset that is paired and connected using the "Phone audio" profile.
I can use an app called SoundAbout to force "Media Audio" to "Bluetooth (mono) (SCO)". With this app set, my voice recognition now works taking my speech input from my headset.
How can I use RecognizerIntent and get speech input from a bluetooth headset?
I see in API level 16 there is a new intent action ACTION_VOICE_SEARCH_HANDS_FREE. This is too new for me to use, but would this solve my problem?
Do I have to muck around in the AudioManager (like I assume SoundAbout is doing) to route the audio input using setBluetoothScoOn() or startBluetoothSco()?

Manifest permission
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
Create an inner class BluetoothHelper extends BluetoothHeadSetUtils in your Activity or Service. Declare a class member mBluetoothHelper and instantiate it in onCreate()
BluetoothHelper mBluetoothHelper;
#Override
public void onCreate()
{
mBluetoothHelper = new BluetoothHelper(this);
}
#Override
onResume()
{
mBluetoothHelper.start();
}
#Override
onPause()
{
mBluetoothHelper.stop();
}
// inner class
// BluetoothHeadsetUtils is an abstract class that has
// 4 abstracts methods that need to be implemented.
private class BluetoothHelper extends BluetoothHeadSetUtils
{
public BluetoothHelper(Context context)
{
super(context);
}
#Override
public void onScoAudioDisconnected()
{
// Cancel speech recognizer if desired
}
#Override
public void onScoAudioConnected()
{
// Should start speech recognition here if not already started
}
#Override
public void onHeadsetDisconnected()
{
}
#Override
public void onHeadsetConnected()
{
}
}
To use bluetooth headset with Text To Speech you need to set the AudioManager to STREAM_VOICE_CALL before calling speak. Or use the code below
protected void speak(String text)
{
HashMap<String, String> myHashRender = new HashMap<String, String>();
if (mBluetoothHelper.isOnHeadsetSco())
{
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
String.valueOf(AudioManager.STREAM_VOICE_CALL));
}
mTts.speak(text, TextToSpeech.QUEUE_FLUSH, myHashRender);
}
Copy the BluetoothHeadsetUtils class to your project.
import java.util.List;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Build;
import android.os.CountDownTimer;
import android.util.Log;
/**
* This is a utility to detect bluetooth headset connection and establish audio connection
* for android API >= 8. This includes a work around for API < 11 to detect already connected headset
* before the application starts. This work around would only fails if Sco audio
* connection is accepted but the connected device is not a headset.
*
* #author Hoan Nguyen
*
*/
public abstract class BluetoothHeadsetUtils
{
private Context mContext;
private BluetoothAdapter mBluetoothAdapter;
private BluetoothHeadset mBluetoothHeadset;
private BluetoothDevice mConnectedHeadset;
private AudioManager mAudioManager;
private boolean mIsCountDownOn;
private boolean mIsStarting;
private boolean mIsOnHeadsetSco;
private boolean mIsStarted;
private static final String TAG = "BluetoothHeadsetUtils"; //$NON-NLS-1$
/**
* Constructor
* #param context
*/
public BluetoothHeadsetUtils(Context context)
{
mContext = context;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
}
/**
* Call this to start BluetoothHeadsetUtils functionalities.
* #return The return value of startBluetooth() or startBluetooth11()
*/
public boolean start()
{
if (!mIsStarted)
{
mIsStarted = true;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
{
mIsStarted = startBluetooth();
}
else
{
mIsStarted = startBluetooth11();
}
}
return mIsStarted;
}
/**
* Should call this on onResume or onDestroy.
* Unregister broadcast receivers and stop Sco audio connection
* and cancel count down.
*/
public void stop()
{
if (mIsStarted)
{
mIsStarted = false;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
{
stopBluetooth();
}
else
{
stopBluetooth11();
}
}
}
/**
*
* #return true if audio is connected through headset.
*/
public boolean isOnHeadsetSco()
{
return mIsOnHeadsetSco;
}
public abstract void onHeadsetDisconnected();
public abstract void onHeadsetConnected();
public abstract void onScoAudioDisconnected();
public abstract void onScoAudioConnected();
/**
* Register for bluetooth headset connection states and Sco audio states.
* Try to connect to bluetooth headset audio by calling startBluetoothSco().
* This is a work around for API < 11 to detect if a headset is connected before
* the application starts.
*
* The official documentation for startBluetoothSco() states
*
* "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."
*
* Does this mean that startBluetoothSco() would fail if the connected bluetooth device
* is not a headset?
*
* Thus if a call to startBluetoothSco() is successful, i.e mBroadcastReceiver will receive
* an ACTION_SCO_AUDIO_STATE_CHANGED with intent extra SCO_AUDIO_STATE_CONNECTED, then
* we assume that a headset is connected.
*
* #return false if device does not support bluetooth or current platform does not supports
* use of SCO for off call.
*/
#SuppressWarnings("deprecation")
private boolean startBluetooth()
{
Log.d(TAG, "startBluetooth"); //$NON-NLS-1$
// Device support bluetooth
if (mBluetoothAdapter != null)
{
if (mAudioManager.isBluetoothScoAvailableOffCall())
{
mContext.registerReceiver(mBroadcastReceiver,
new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));
mContext.registerReceiver(mBroadcastReceiver,
new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED));
mContext.registerReceiver(mBroadcastReceiver,
new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));
// Need to set audio mode to MODE_IN_CALL for call to startBluetoothSco() to succeed.
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
mIsCountDownOn = true;
// mCountDown repeatedly tries to start bluetooth Sco audio connection.
mCountDown.start();
// need for audio sco, see mBroadcastReceiver
mIsStarting = true;
return true;
}
}
return false;
}
/**
* Register a headset profile listener
* #return false if device does not support bluetooth or current platform does not supports
* use of SCO for off call or error in getting profile proxy.
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private boolean startBluetooth11()
{
Log.d(TAG, "startBluetooth11"); //$NON-NLS-1$
// Device support bluetooth
if (mBluetoothAdapter != null)
{
if (mAudioManager.isBluetoothScoAvailableOffCall())
{
// All the detection and audio connection are done in mHeadsetProfileListener
if (mBluetoothAdapter.getProfileProxy(mContext,
mHeadsetProfileListener,
BluetoothProfile.HEADSET))
{
return true;
}
}
}
return false;
}
/**
* API < 11
* Unregister broadcast receivers and stop Sco audio connection
* and cancel count down.
*/
private void stopBluetooth()
{
Log.d(TAG, "stopBluetooth"); //$NON-NLS-1$
if (mIsCountDownOn)
{
mIsCountDownOn = false;
mCountDown.cancel();
}
// Need to stop Sco audio connection here when the app
// change orientation or close with headset still turns on.
mContext.unregisterReceiver(mBroadcastReceiver);
mAudioManager.stopBluetoothSco();
mAudioManager.setMode(AudioManager.MODE_NORMAL);
}
/**
* API >= 11
* Unregister broadcast receivers and stop Sco audio connection
* and cancel count down.
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
protected void stopBluetooth11()
{
Log.d(TAG, "stopBluetooth11"); //$NON-NLS-1$
if (mIsCountDownOn)
{
mIsCountDownOn = false;
mCountDown11.cancel();
}
if (mBluetoothHeadset != null)
{
// Need to call stopVoiceRecognition here when the app
// change orientation or close with headset still turns on.
mBluetoothHeadset.stopVoiceRecognition(mConnectedHeadset);
mContext.unregisterReceiver(mHeadsetBroadcastReceiver);
mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset);
mBluetoothHeadset = null;
}
}
/**
* Broadcast receiver for API < 11
* Handle headset and Sco audio connection states.
*/
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver()
{
#SuppressWarnings({"deprecation", "synthetic-access"})
#Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED))
{
mConnectedHeadset = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
BluetoothClass bluetoothClass = mConnectedHeadset.getBluetoothClass();
if (bluetoothClass != null)
{
// Check if device is a headset. Besides the 2 below, are there other
// device classes also qualified as headset?
int deviceClass = bluetoothClass.getDeviceClass();
if (deviceClass == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE
|| deviceClass == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET)
{
// start bluetooth Sco audio connection.
// Calling startBluetoothSco() always returns faIL here,
// that why a count down timer is implemented to call
// startBluetoothSco() in the onTick.
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
mIsCountDownOn = true;
mCountDown.start();
// override this if you want to do other thing when the device is connected.
onHeadsetConnected();
}
}
Log.d(TAG, mConnectedHeadset.getName() + " connected"); //$NON-NLS-1$
}
else if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED))
{
Log.d(TAG, "Headset disconnected"); //$NON-NLS-1$
if (mIsCountDownOn)
{
mIsCountDownOn = false;
mCountDown.cancel();
}
mAudioManager.setMode(AudioManager.MODE_NORMAL);
// override this if you want to do other thing when the device is disconnected.
onHeadsetDisconnected();
}
else if (action.equals(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED))
{
int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE,
AudioManager.SCO_AUDIO_STATE_ERROR);
if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED)
{
mIsOnHeadsetSco = true;
if (mIsStarting)
{
// When the device is connected before the application starts,
// ACTION_ACL_CONNECTED will not be received, so call onHeadsetConnected here
mIsStarting = false;
onHeadsetConnected();
}
if (mIsCountDownOn)
{
mIsCountDownOn = false;
mCountDown.cancel();
}
// override this if you want to do other thing when Sco audio is connected.
onScoAudioConnected();
Log.d(TAG, "Sco connected"); //$NON-NLS-1$
}
else if (state == AudioManager.SCO_AUDIO_STATE_DISCONNECTED)
{
Log.d(TAG, "Sco disconnected"); //$NON-NLS-1$
// Always receive SCO_AUDIO_STATE_DISCONNECTED on call to startBluetooth()
// which at that stage we do not want to do anything. Thus the if condition.
if (!mIsStarting)
{
mIsOnHeadsetSco = false;
// Need to call stopBluetoothSco(), otherwise startBluetoothSco()
// will not be successful.
mAudioManager.stopBluetoothSco();
// override this if you want to do other thing when Sco audio is disconnected.
onScoAudioDisconnected();
}
}
}
}
};
/**
* API < 11
* Try to connect to audio headset in onTick.
*/
private CountDownTimer mCountDown = new CountDownTimer(10000, 1000)
{
#SuppressWarnings("synthetic-access")
#Override
public void onTick(long millisUntilFinished)
{
// When this call is successful, this count down timer will be canceled.
mAudioManager.startBluetoothSco();
Log.d(TAG, "\nonTick start bluetooth Sco"); //$NON-NLS-1$
}
#SuppressWarnings("synthetic-access")
#Override
public void onFinish()
{
// Calls to startBluetoothSco() in onStick are not successful.
// Should implement something to inform user of this failure
mIsCountDownOn = false;
mAudioManager.setMode(AudioManager.MODE_NORMAL);
Log.d(TAG, "\nonFinish fail to connect to headset audio"); //$NON-NLS-1$
}
};
/**
* API >= 11
* Check for already connected headset and if so start audio connection.
* Register for broadcast of headset and Sco audio connection states.
*/
private BluetoothProfile.ServiceListener mHeadsetProfileListener = new BluetoothProfile.ServiceListener()
{
/**
* This method is never called, even when we closeProfileProxy on onPause.
* When or will it ever be called???
*/
#Override
public void onServiceDisconnected(int profile)
{
Log.d(TAG, "Profile listener onServiceDisconnected"); //$NON-NLS-1$
stopBluetooth11();
}
#SuppressWarnings("synthetic-access")
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
public void onServiceConnected(int profile, BluetoothProfile proxy)
{
Log.d(TAG, "Profile listener onServiceConnected"); //$NON-NLS-1$
// mBluetoothHeadset is just a headset profile,
// it does not represent a headset device.
mBluetoothHeadset = (BluetoothHeadset) proxy;
// If a headset is connected before this application starts,
// ACTION_CONNECTION_STATE_CHANGED will not be broadcast.
// So we need to check for already connected headset.
List<BluetoothDevice> devices = mBluetoothHeadset.getConnectedDevices();
if (devices.size() > 0)
{
// Only one headset can be connected at a time,
// so the connected headset is at index 0.
mConnectedHeadset = devices.get(0);
onHeadsetConnected();
// Should not need count down timer, but just in case.
// See comment below in mHeadsetBroadcastReceiver onReceive()
mIsCountDownOn = true;
mCountDown11.start();
Log.d(TAG, "Start count down"); //$NON-NLS-1$
}
// During the active life time of the app, a user may turn on and off the headset.
// So register for broadcast of connection states.
mContext.registerReceiver(mHeadsetBroadcastReceiver,
new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED));
// Calling startVoiceRecognition does not result in immediate audio connection.
// So register for broadcast of audio connection states. This broadcast will
// only be sent if startVoiceRecognition returns true.
mContext.registerReceiver(mHeadsetBroadcastReceiver,
new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED));
}
};
/**
* API >= 11
* Handle headset and Sco audio connection states.
*/
private BroadcastReceiver mHeadsetBroadcastReceiver = new BroadcastReceiver()
{
#SuppressWarnings("synthetic-access")
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
int state;
if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED))
{
state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE,
BluetoothHeadset.STATE_DISCONNECTED);
Log.d(TAG, "\nAction = " + action + "\nState = " + state); //$NON-NLS-1$ //$NON-NLS-2$
if (state == BluetoothHeadset.STATE_CONNECTED)
{
mConnectedHeadset = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Calling startVoiceRecognition always returns false here,
// that why a count down timer is implemented to call
// startVoiceRecognition in the onTick.
mIsCountDownOn = true;
mCountDown11.start();
// override this if you want to do other thing when the device is connected.
onHeadsetConnected();
Log.d(TAG, "Start count down"); //$NON-NLS-1$
}
else if (state == BluetoothHeadset.STATE_DISCONNECTED)
{
// Calling stopVoiceRecognition always returns false here
// as it should since the headset is no longer connected.
if (mIsCountDownOn)
{
mIsCountDownOn = false;
mCountDown11.cancel();
}
mConnectedHeadset = null;
// override this if you want to do other thing when the device is disconnected.
onHeadsetDisconnected();
Log.d(TAG, "Headset disconnected"); //$NON-NLS-1$
}
}
else // audio
{
state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_AUDIO_DISCONNECTED);
Log.d(TAG, "\nAction = " + action + "\nState = " + state); //$NON-NLS-1$ //$NON-NLS-2$
if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED)
{
Log.d(TAG, "\nHeadset audio connected"); //$NON-NLS-1$
mIsOnHeadsetSco = true;
if (mIsCountDownOn)
{
mIsCountDownOn = false;
mCountDown11.cancel();
}
// override this if you want to do other thing when headset audio is connected.
onScoAudioConnected();
}
else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED)
{
mIsOnHeadsetSco = false;
// The headset audio is disconnected, but calling
// stopVoiceRecognition always returns true here.
mBluetoothHeadset.stopVoiceRecognition(mConnectedHeadset);
// override this if you want to do other thing when headset audio is disconnected.
onScoAudioDisconnected();
Log.d(TAG, "Headset audio disconnected"); //$NON-NLS-1$
}
}
}
};
/**
* API >= 11
* Try to connect to audio headset in onTick.
*/
private CountDownTimer mCountDown11 = new CountDownTimer(10000, 1000)
{
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#SuppressWarnings("synthetic-access")
#Override
public void onTick(long millisUntilFinished)
{
// First stick calls always returns false. The second stick
// always returns true if the countDownInterval is set to 1000.
// It is somewhere in between 500 to a 1000.
mBluetoothHeadset.startVoiceRecognition(mConnectedHeadset);
Log.d(TAG, "onTick startVoiceRecognition"); //$NON-NLS-1$
}
#SuppressWarnings("synthetic-access")
#Override
public void onFinish()
{
// Calls to startVoiceRecognition in onStick are not successful.
// Should implement something to inform user of this failure
mIsCountDownOn = false;
Log.d(TAG, "\nonFinish fail to connect to headset audio"); //$NON-NLS-1$
}
};
}
(April 30 2013) Edit to change to #TargetApi(Build.VERSION_CODES.HONEYCOMB) when necessary. If you have problem with java.lang.NoClassDefFoundError for API 8 or 9, just remove all the API >= 11 codes. The startBluetoothSco() works for all API versions.

I think all you have to do is change the audio settings on your application.
you should put the following code when you want to receive and send the sound from your headset via bluetooth.
AudioManager audioManager;
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
audioManager.startBluetoothSco();
audioManager.setBluetoothScoOn(true);
Do not forget to restore the normal settings on the phone in the following way when you're not using the bluetooth.
audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.stopBluetoothSco();
audioManager.setBluetoothScoOn(false);
the permissions that are needed are the following:
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
it's easy!

Even though the Bluetooth headset is paired and connected to the Phone audio profile (HF/HS), the actual audio connection (SCO) is established only when a call comes in and accepted.
For your VR application to accept the voice input from a Bluetooth Headset your application will have to establish a SCO to the headset on some VR input trigger,
You will need to use the following -
isBluetoothScoAvailableOffCall to check if the platform supports this capability, then use the
startBluetoothSco and stopBluetoothSco to start the SCO to the headset.

Add these permissions:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
Then create a Broadcast Receiver for Bluetooth.
public class BTReceiver extends BroadcastReceiver {
private static final String TAG = "BTReceiver";
int state = 0;
AudioManager audioManager;
#Override
public void onReceive(Context context, Intent intent) {
Log.d("Z", "Received: Bluetooth");
try {
Bundle extras = intent.getExtras();
if (extras != null) { //Do something
audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);
String action = intent.getAction();
Toast.makeText(context, action, Toast.LENGTH_LONG).show();
int state;
if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE,
BluetoothHeadset.STATE_DISCONNECTED);
Log.d(TAG, "\nAction = " + action + "\nState = " + state); //$NON-NLS-1$ //$NON-NLS-2$
if (state == BluetoothHeadset.STATE_CONNECTED) {
setModeBluetooth();
} else if (state == BluetoothHeadset.STATE_DISCONNECTED) {
// Calling stopVoiceRecognition always returns false here
// as it should since the headset is no longer connected.
setModeNormal();
Log.d(TAG, "Headset disconnected"); //$NON-NLS-1$
}
} else // audio
{
state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_AUDIO_DISCONNECTED);
Log.d(TAG, "\nAction = " + action + "\nState = " + state); //$NON-NLS-1$ //$NON-NLS-2$
if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED) {
Log.d(TAG, "\nHeadset audio connected"); //$NON-NLS-1$
setModeBluetooth();
} else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED) {
setModeNormal();
Log.d(TAG, "Headset audio disconnected"); //$NON-NLS-1$
}
}
}
} catch (Exception e) {
Log.d("Exception", "Exception " + e.toString());
}
}
private void setModeBluetooth() {
try {
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
audioManager.startBluetoothSco();
audioManager.setBluetoothScoOn(true);
} catch (Exception e) {
e.printStackTrace();
}
}
private void setModeNormal() {
try {
audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.stopBluetoothSco();
audioManager.setBluetoothScoOn(false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Register the receiver in Manifest:
<receiver
android:name=".speech.BTReceiver"
android:enabled="true"
android:permission="android.permission.BLUETOOTH">
<intent-filter>
<action android:name="android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED" />
</intent-filter>
</receiver>
In your activity Register and UnRegister this receiver:
First initialize it, in onCreate
mHeadsetBroadcastReceiver = new BTReceiver();
then
#Override
protected void onResume() {
super.onResume();
registerReceiver(mHeadsetBroadcastReceiver,
new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED));
// Calling startVoiceRecognition does not result in immediate audio connection.
// So register for broadcast of audio connection states. This broadcast will
// only be sent if startVoiceRecognition returns true.
registerReceiver(mHeadsetBroadcastReceiver,
new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED));
}
#Override
protected void onPause() {
unregisterReceiver(mHeadsetBroadcastReceiver);
super.onPause();
}

startBluetoothSco takes a long time to establish, which is problem if you need to use it for voice control.
Is there a quick way to use the bluetooth mic to listen and then turn it off after listening?
If the connection is on the whole time then it is not possible to stream audio via A2DP.
So, ideal world:
Audio out via A2DP. When it starts listening, it uses SCO bluetooth mic. Any response is A2DP again.
In fact, if it is already connected - can you switch on the fly by change the media stream to call stream? If so, is there any noticeable delay?

Hoan Nguyen has done a great job!
Testing his code I have noticed that in some cases stopBluetoothSco() is not called.
I propose a little change in CountDownTimer:
private CountDownTimer mCountDown11 = new CountDownTimer(10000, 1000)
{
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#SuppressWarnings("synthetic-access")
#Override
public void onTick(long millisUntilFinished)
{
mBluetoothHeadset.startVoiceRecognition(mConnectedHeadset);
Log.d(TAG, "onTick startVoiceRecognition"); //$NON-NLS-1$
}
#SuppressWarnings("synthetic-access")
#Override
public void onFinish()
{
mIsCountDownOn = false;
/* START EDIT: Unregister broadcast receivers and stop Sco audio connection
and cancel count down if fails to connect. */
stopBluetooth11();
/* END EDIT */
Log.d(TAG, "\nonFinish fail to connect to headset audio"); //$NON-NLS-1$
}
};

Related

Recording with Bluetooth Mic

After connecting with paired devices, Bluetooth is not taking his Mic, instead it is taking mobile's Mic
Below is my code
public abstract class BluetoothHeadsetUtils {
private Context mContext;
private BluetoothAdapter mBluetoothAdapter;
private BluetoothHeadset mBluetoothHeadset;
private BluetoothDevice mConnectedHeadset;
private AudioManager mAudioManager;
private boolean mIsCountDownOn;
private boolean mIsStarting;
private boolean mIsOnHeadsetSco;
private boolean mIsStarted;
ExminatingHelper exminatingHelper = ExminatingHelper.getInstance();
private static final String TAG = "BluetoothHeadsetUtils";
/**
* Constructor
*
* #param context
*/
public BluetoothHeadsetUtils(Context context) {
mContext = context;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
}
/**
* Call this to start BluetoothHeadsetUtils functionalities.
*
* #return The return value of startBluetooth() or startBluetooth11()
*/
public boolean start() {
if (!mIsStarted) {
mIsStarted = true;
mIsStarted = startBluetooth();
/*if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
mIsStarted = startBluetooth();
} else {
System.out.println("");
// mIsStarted = startBluetooth11();
}*/
}
return mIsStarted;
}
/**
* Should call this on onResume or onDestroy.
* Unregister broadcast receivers and stop Sco audio connection
* and cancel count down.
*/
public void stop() {
if (mIsStarted) {
mIsStarted = false;
stopBluetooth();
/*if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
stopBluetooth();
} else {
System.out.println("");
// stopBluetooth11();
}*/
}
}
/**
* #return true if audio is connected through headset.
*/
public boolean isOnHeadsetSco() {
return mIsOnHeadsetSco;
}
public abstract void onHeadsetDisconnected();
public abstract void onHeadsetConnected();
public abstract void onScoAudioDisconnected();
public abstract void onScoAudioConnected();
#SuppressWarnings("deprecation")
private boolean startBluetooth() {
Log.d(TAG, "startBluetooth"); //$NON-NLS-1$
// Device support bluetooth
if (mBluetoothAdapter != null) {
if (mAudioManager.isBluetoothScoAvailableOffCall()) {
mContext.registerReceiver(mBroadcastReceiver,
new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED));
mIsCountDownOn = true;
// mCountDown repeatedly tries to start bluetooth Sco audio connection.
mCountDown.start();
// need for audio sco, see mBroadcastReceiver
mIsStarting = true;
return true;
}
}
return false;
}
private CountDownTimer mCountDown = new CountDownTimer(10000, 1000) {
#SuppressWarnings("synthetic-access")
#Override
public void onTick(long millisUntilFinished) {
// When this call is successful, this count down timer will be canceled.
mAudioManager.startBluetoothSco();
Log.d(TAG, "\nonTick start bluetooth Sco"); //$NON-NLS-1$
}
#SuppressWarnings("synthetic-access")
#Override
public void onFinish() {
// Calls to startBluetoothSco() in onStick are not successful.
// Should implement something to inform user of this failure
mIsCountDownOn = false;
Log.d(TAG, "\nonFinish fail to connect to headset audio"); //$NON-NLS-1$
}
};
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
#SuppressWarnings({"deprecation", "synthetic-access"})
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
int getRecrodingDevice = exminatingHelper.getRecordingDevice();
if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
mConnectedHeadset = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
BluetoothClass bluetoothClass = mConnectedHeadset.getBluetoothClass();
if (bluetoothClass != null) {
// Check if device is a headset. Besides the 2 below, are there other
// device classes also qualified as headset?
int deviceClass = bluetoothClass.getDeviceClass();
if (deviceClass == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE
|| deviceClass == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET
|| deviceClass == BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES
|| deviceClass == BluetoothClass.Device.AUDIO_VIDEO_LOUDSPEAKER
) {
// start bluetooth Sco audio connection.
// Calling startBluetoothSco() always returns faIL here,
// that why a count down timer is implemented to call
// startBluetoothSco() in the onTick.
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
mIsCountDownOn = true;
mCountDown.start();
// override this if you want to do other thing when the device is connected.
onHeadsetConnected();
}
}
Log.d(TAG, mConnectedHeadset.getName() + " connected"); //$NON-NLS-1$
} else if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
Log.d(TAG, "Headset disconnected"); //$NON-NLS-1$
if (mIsCountDownOn) {
mIsCountDownOn = false;
mCountDown.cancel();
}
// override this if you want to do other thing when the device is disconnected.
onHeadsetDisconnected();
} else if (action.equals(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED) && getRecrodingDevice != 2) {
int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE,
AudioManager.SCO_AUDIO_STATE_ERROR);
if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
mIsOnHeadsetSco = true;
if (mIsStarting) {
// When the device is connected before the application starts,
// ACTION_ACL_CONNECTED will not be received, so call onHeadsetConnected here
mIsStarting = false;
onHeadsetConnected();
}
if (mIsCountDownOn) {
mIsCountDownOn = false;
mCountDown.cancel();
}
// override this if you want to do other thing when Sco audio is connected.
onScoAudioConnected();
Log.d(TAG, "Sco connected"); //$NON-NLS-1$
} else if (state == AudioManager.SCO_AUDIO_STATE_DISCONNECTED) {
Log.d(TAG, "Sco disconnected"); //$NON-NLS-1$
// Always receive SCO_AUDIO_STATE_DISCONNECTED on call to startBluetooth()
// which at that stage we do not want to do anything. Thus the if condition.
if (!mIsStarting) {
mIsOnHeadsetSco = false;
// Need to call stopBluetoothSco(), otherwise startBluetoothSco()
// will not be successful.
mAudioManager.stopBluetoothSco();
// override this if you want to do other things when Sco audio is disconnected.
onScoAudioDisconnected();
}
}
}
}
};
private void stopBluetooth() {
Log.d(TAG, "stopBluetooth"); //$NON-NLS-1$
if (mIsCountDownOn) {
mIsCountDownOn = false;
mCountDown.cancel();
}
// Need to stop Sco audio connection here when the app
// change orientation or close with headset still turns on.
mContext.unregisterReceiver(mBroadcastReceiver);
mAudioManager.stopBluetoothSco();
mAudioManager.setMode(AudioManager.MODE_NORMAL);
}
}
}
This works well when using the phone in a normal fashion. However, it does not detect the presence of a Bluetooth headset and still uses the phone's own microphone even when the headset is plugged in.
Here's a clip of mine, there was this Bluetooth receiver class shared on SO, but it didn't work well with me (long story short, don't use the (state == AudioManager.SCO_AUDIO_STATE_DISCONNECTED) because when I trigger it via clicking the button, there's usually a false positive).
Try following the logs..
and on
onHeadsetConnected();
like
private void onHeadsetConnected(){
speech.startListening(theIntent);
}
while to capture when you have "disconnected", instead of counting SCO_AUDIO_STATE_DISCONNECTED, just capture onError, onPartialResult, onResult and recheck whether the device is still there .. as so, or .. you know .. recheck it when you need to listen to it again instead of when it's done ..
mBluetoothHeadset.getConnectionState(mConnectedHeadset) == BluetoothHeadset.STATE_CONNECTED
And I do this to ensure I'm connecting to the right headset (that has a mic!)
if (mBluetoothHeadset.getConnectedDevices().size()>0) {
mConnectedHeadset = null;
for (int i=0;i<mBluetoothHeadset.getConnectedDevices().size(); i ++){
int deviceClass = mBluetoothHeadset.getConnectedDevices().get(i).getBluetoothClass().getDeviceClass();
switch (deviceClass){
case BluetoothClass.Device.Major.PHONE:
case BluetoothClass.Device.Major.AUDIO_VIDEO:
case BluetoothClass.Device.AUDIO_VIDEO_MICROPHONE:
case BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE:
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
mConnectedHeadset = mBluetoothHeadset.getConnectedDevices().get(i);
registerReceiver(mHeadsetBroadcastReceiver,
new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED));
return;
}
}
}
Hope this helps you / or someone .. since I tried to implement that class, way too much of a hassle, so I just took parts of it into my activity and use what I needed (definitely didn't need support for old android)

Android BLE Gatt connection change statuses

I have an android app to connect to a BLE device and write to it. I can successfully connect, read and write to it. As a part of testing, we are trying different disconnection scenarios.
Sometimes, if BLE device disconnects the connection, I get the connection change as disconnect with status value as 19. Also if there is any bond error, status equals 22. If I programmatically disconnect the connection, this status gives me 0. But none of these states except 0 are specified in android documentation.
Posting a sample BluetoothGattCallback
private BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() {
#Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
Log.i(TAG, "onConnectionStateChange status: "+status+", newState: "+newState);
/*i need to know the possible values for this status variable*/
if(newState == BluetoothProfile.STATE_CONNECTED) {
gatt.discoverServices();
} else {
gatt.close();
}
}
#Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.i(TAG, "onServicesDiscovered service discovered");
}
};
Does anyone face this same problem and sorted out the list of statuses. I need to know the possible values for status variable in onConnectionStateChange method
Here is the list of codes i have
Programmatically disconnected - 0
Device went out of range - 8
Disconnected by device - 19
Issue with bond - 22
Device not found - 133(some phone it gives 62)
I have tested disconnect scenario's in 5.0.2, 5.1, 6.0 and 6.0.1. But only found this bond issue code in 6.0.1 android version.
Sorry to bring up an old question, but here is the solution for many of the problems i've had with Bluetooth (BLE) 4.0. Sorry again for the big classes below but be sure they are needed and no method there is irrelevant or unused.
public abstract class AbstractBluetoothBroadcaster extends BroadcastReceiver {
protected static final String LOG_TAG = BluetoothLowEnergy.LOG_TAG;
protected BluetoothLowEnergy bluetoothLowEnergy;
public AbstractBluetoothBroadcaster(BluetoothLowEnergy bluetoothLowEnergy, String action){
super();
this.bluetoothLowEnergy = bluetoothLowEnergy;
IntentFilter intentFilterStateChange = new IntentFilter(action);
intentFilterStateChange.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
this.bluetoothLowEnergy.getActivity().registerReceiver(this, intentFilterStateChange);
}
public void onDestroy(){
this.bluetoothLowEnergy.getActivity().unregisterReceiver(this);
}
}
public class BluetoothBondStateBroadcaster extends AbstractBluetoothBroadcaster {
private BluetoothLowEnergy bluetoothLowEnergy;
private boolean deviceBonded;
public BluetoothBondStateBroadcaster(BluetoothLowEnergy bluetoothLowEnergy) {
super(bluetoothLowEnergy, BluetoothDevice.ACTION_BOND_STATE_CHANGED);
this.bluetoothLowEnergy = bluetoothLowEnergy;
this.deviceBonded = false;
}
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action == null){
return;
}
BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED) &&
bluetoothDevice != null &&
bluetoothDevice.getAddress().equals(bluetoothLowEnergy.getDeviceUUID())) {
int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
switch (state) {
case BluetoothDevice.BOND_NONE:
Log.d(LOG_TAG, " NOT BONDED - dev " + bluetoothDevice.getAddress());
this.deviceBonded = false;
break;
case BluetoothDevice.BOND_BONDING:
Log.d(LOG_TAG, " BONDING ... - dev " + bluetoothDevice.getAddress());
break;
case BluetoothDevice.BOND_BONDED:
Log.d(LOG_TAG, " BONDED - dev " + bluetoothDevice.getAddress());
deviceBonded = true;
bluetoothLowEnergy.onBluetoothBonded();
break;
default:
break;
}
}
}
public void resetDeviceBonded(){
this.deviceBonded = false;
}
public boolean isDeviceBonded() {
return deviceBonded;
}
}
public class BluetoothPairingBroadcaster extends AbstractBluetoothBroadcaster {
private String devicePIN;
public BluetoothPairingBroadcaster(BluetoothLowEnergy bluetoothLowEnergy){
super(bluetoothLowEnergy, BluetoothDevice.ACTION_PAIRING_REQUEST);
this.devicePIN = "";
}
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action == null){
return;
}
BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int pairingType = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
if (action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST) &&
bluetoothDevice != null &&
bluetoothDevice.getAddress().equals(bluetoothLowEnergy.getDeviceUUID()) &&
!getDevicePIN().isEmpty()) {
if (pairingType == BluetoothDevice.PAIRING_VARIANT_PIN){
bluetoothDevice.setPin(getDevicePIN().getBytes());
Log.d(LOG_TAG," Auto-entering pin - " + getDevicePIN());
bluetoothDevice.createBond();
Log.d(LOG_TAG," pin entered and request sent...");
abortBroadcast();
}
}
}
public void setDevicePIN(String pin){
this.devicePIN = pin;
}
public String getDevicePIN(){
return this.devicePIN ;
}
}
public class BluetoothLowEnergy extends BluetoothGattCallback {
// listener that has the methods that the application (activity)
// will use to send / receive data, or to reflect the system state
// in the UI
public interface BluetoothListener {
/**
* Triggered when the scanning has started successfully
*/
void onBluetoothStartScan();
/**
* Triggered when the scanning stops
* #param scanResults results of the scanning
*/
void onBluetoothStopScan(Collection<BluetoothDevice> scanResults);
/**
* Triggered when the device is ready to send/receive data
*/
void onBluetoothConnectionReady();
/**
* Triggered when a bluetooth msg is received
* #param msg message received
*/
void onBluetoothReceiveMsg(String msg);
/**
* Triggered whenever data is send
* #param success true means data was sent fine to the remote device, false otherwise
*/
void onBluetoothSend(String data, boolean success);
/**
* Triggered if no bluetooth is connected, and we need a connection
* to send / receive / discover services
*/
void onBluetoothNotConnected();
}
// custom exceptions
public class BluetoothNotEnabledException extends Exception { }
public class BluetoothLowEnergyNotSupported extends Exception { }
public class BluetoothDeviceNotFound extends Exception { }
// service and characteristic uuids that are going to be used to
// send / receive data between central and peripheral GATTs
private static final String SERVICE_UUID = "FFE0-";
private static final String CHARACTERISTIC_UUID = "FFE1-";
// timeout for bluetooth scan (in ms)
public static final int SCAN_TIMEOUT = 5000;
// BLE LOG TAG
public static final String LOG_TAG = "BLUETOOTH_BLE";
// model
private boolean bluetoothScanning;
private boolean bluetoothConnected;
private Map<String, BluetoothDevice> bluetoothScanResults;
// gui
private Activity activity;
// bluetooth
private BluetoothAdapter bluetoothAdapter;
private BluetoothLeScanner bluetoothLeScanner;
private ScanCallback bluetoothScanCallback;
private BluetoothGatt bluetoothGatt;
private BluetoothGattCharacteristic characteristic;
public BluetoothLowEnergy(Activity activity, BluetoothListener bluetoothListener){
this.activity = activity;
this.bluetoothListener = bluetoothListener;
// this keeps track of the scanning and connection states
this.bluetoothScanning = this.bluetoothConnected = false;
// keeps track of the scanning results
this.bluetoothScanResults = new HashMap<>();
// set bluetooth pairing request and bonded callback
// these broadcasters will be responsible to detect and validate
// the bonded state of your device
this.pairingRequestBroadcaster = new BluetoothPairingBroadcaster(this);
this.bondedBroadcaster = new BluetoothBondStateBroadcaster(this);
// set the scan callback methods that will add results to
// this.bluetoothScanResults map
this.bluetoothScanCallback = new ScanCallback() {
#Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
addScanResult(result);
}
#Override
public void onBatchScanResults(List<ScanResult> results) {
super.onBatchScanResults(results);
for (ScanResult result: results) {
addScanResult(result);
}
}
#Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
Log.e(LOG_TAG, "Scan Failed with code " + errorCode);
}
private void addScanResult(ScanResult result) {
BluetoothDevice device = result.getDevice();
String deviceAddress = device.getAddress();
bluetoothScanResults.put(deviceAddress, device);
Log.d(LOG_TAG, "Found device " + deviceAddress);
}
};
// Use this to determine whether BLE is supported on the device.
if (!this.activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
throw new BluetoothLowEnergyNotSupported();
}
}
/**
* This method should be called when the activity is destroyed
*/
public void onDestroy(){
this.bondedBroadcaster.onDestroy();
this.pairingRequestBroadcaster.onDestroy();
this.disconnect();
}
/**
* This method is called when we finish pairing/bonding to the device
*/
public void onBluetoothBonded(){
// if we have the services already discovered, then we can
// send/receive data, to do so we call the bluetooth listener below
if (servicesDiscovered){
this.bluetoothListener.onBluetoothConnectionReady();
// if we know we have a connection established, then we can
// discover services
} else if (bluetoothConnected){
bluetoothGatt.discoverServices();
}
}
/**
* This method is called whenever a connection is established or a disconnection happens
*/
#Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
BluetoothDevice bluetoothDevice = gatt.getDevice();
// if these conditions == true, then we have a disconnect
if ( status == BluetoothGatt.GATT_FAILURE ||
status != BluetoothGatt.GATT_SUCCESS ||
newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.d(LOG_TAG, String.format(Locale.getDefault(),
"Disconnected from %s (%s) - status %d - state %d",
bluetoothDevice.getName(),
bluetoothDevice.getAddress(),
status,
newState
));
this.disconnect();
// if these conditions == true, then we have a successful connection
} else if (newState == BluetoothProfile.STATE_CONNECTED) {
bluetoothConnected = true;
Log.d(LOG_TAG, String.format(Locale.getDefault(),
"Connected to %s (%s) - status %d - state %d",
bluetoothDevice.getName(),
bluetoothDevice.getAddress(),
status,
newState
));
// this sleep is here to avoid TONS of problems in BLE, that occur whenever we start
// service discovery immediately after the connection is established
try {
Thread.sleep(600);
} catch (InterruptedException e) {
e.printStackTrace();
}
gatt.discoverServices();
}
}
#Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
if (status != BluetoothGatt.GATT_SUCCESS) {
return;
}
// BEGIN - find the service and characteristic that we want (defined as a static attribute
// of the BluetoothLowEnergy class)
Log.d(LOG_TAG, "Discovering services ...");
BluetoothGattService service = null;
for (BluetoothGattService serv: gatt.getServices()){
Log.d(LOG_TAG, "Found service " + serv.getUuid().toString());
if (serv.getUuid().toString().toUpperCase().contains(SERVICE_UUID)){
service = serv;
Log.d(LOG_TAG, "---> Selected service " + serv.getUuid().toString());
break;
}
}
if (service == null){
return;
}
for (BluetoothGattCharacteristic charac: service.getCharacteristics()){
Log.d(LOG_TAG, "Found characteristic " + charac.getUuid().toString());
if (charac.getUuid().toString().toUpperCase().contains(CHARACTERISTIC_UUID)){
this.characteristic = charac;
Log.d(LOG_TAG, "---> Selected characteristic " + charac.getUuid().toString());
break;
}
}
if (this.characteristic == null){
return;
}
Log.d(LOG_TAG, "Setting write and notification to the characteristic ...");
bluetoothAdapter.cancelDiscovery();
// END - find the service and characteristic
// set that we want to write to the selected characteristic and be notified if
// it changes (the remote GATT peripheral sends data to the Android's GATT Center)
this.characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
gatt.setCharacteristicNotification(this.characteristic, true);
// we finished service discovery
this.servicesDiscovered = true;
// if we have paired/bonded then we are ready to send/receive data
if (pairingRequestBroadcaster.getDevicePIN().isEmpty() || bondedBroadcaster.isDeviceBonded()) {
this.bluetoothListener.onBluetoothConnectionReady();
}
}
#Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic charac, int status) {
super.onCharacteristicRead(gatt, charac, status);
restartDisconnectTimeout();
if (status != BluetoothGatt.GATT_SUCCESS) {
return;
}
try {
String characValue = new String(charac.getValue(), CHARSET)
.replaceAll(DATA_FILTER_REGEX,"");
Log.i(LOG_TAG, String.format(Locale.getDefault(),
"Characteristic Read - %s",
characValue
));
if (charac.getUuid().equals(this.characteristic.getUuid())) {
this.bluetoothListener.onBluetoothReceiveMsg(characValue);
}
} catch (UnsupportedEncodingException e) {
Log.e(LOG_TAG, "Characteristic Read - Failed to convert message string to byte array");
}
}
#Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic charac, int status) {
super.onCharacteristicWrite(gatt, charac, status);
restartDisconnectTimeout();
try {
String characValue = new String(charac.getValue(), CHARSET);
Log.i(LOG_TAG, String.format(Locale.getDefault(),
"Characteristic Write - SUCCESS - %s",
characValue
));
bluetoothListener.onBluetoothSend( characValue, (status == BluetoothGatt.GATT_SUCCESS) );
} catch (UnsupportedEncodingException e) {
Log.e(LOG_TAG, "Characteristic Write - Failed to convert message string to byte array");
}
}
#Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic charac) {
super.onCharacteristicChanged(gatt, charac);
Log.d(LOG_TAG,"Characteristic Changed");
onCharacteristicRead(gatt, charac, BluetoothGatt.GATT_SUCCESS);
}
/**
* Remove pairing/bonding of the device
* #param device Device to remove bonding
*/
public static void removeBond(BluetoothDevice device){
try {
if (device == null){
throw new Exception();
}
Method method = device.getClass().getMethod("removeBond", (Class[]) null);
method.invoke(device, (Object[]) null);
Log.d(LOG_TAG, "removeBond() called");
Thread.sleep(600);
Log.d(LOG_TAG, "removeBond() - finished method");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Clears the GATT services cache, so that new services can be discovered
* #param bluetoothGatt GATT Client to clear service's discovery cache
*/
public static void refresh(BluetoothGatt bluetoothGatt){
try {
Method method = bluetoothGatt.getClass().getMethod("refresh", (Class[]) null);
method.invoke(bluetoothGatt, (Object[]) null);
} catch (Exception e){
e.printStackTrace();
}
}
/**
* Connect to the GATT Peripheral device
* #param uuid GATT Peripheral address / mac / uuid to connect to
* #param pin PIN to authenticate and pair to the device
*/
public void connect(String uuid, String pin) throws BluetoothNotEnabledException, BluetoothDeviceNotFound {
checkBluetooth();
// do not connect twice
if (this.isConnected()){
return;
}
// get device
BluetoothDevice device = this.bluetoothScanResults.get(uuid);
if (device == null){
throw new BluetoothDeviceNotFound();
}
this.deviceUUID = uuid;
pairingRequestBroadcaster.setDevicePIN(pin);
removeBond(device);
// create connection to the bluetooth device
bluetoothGatt = device.connectGatt(activity, false, this);
refresh(bluetoothGatt);
}
/**
* Disconnect from BLE device. This method should be called whenever we want to
* close the APP, or the BLE connection.
*/
public void disconnect() {
Log.d(LOG_TAG, "disconnect() - executed");
if (bluetoothGatt != null) {
if (characteristic != null) {
bluetoothGatt.setCharacteristicNotification(characteristic, false);
}
//remove device authorization/ bond/ pairing
removeBond(bluetoothGatt);
// disconnect now
bluetoothGatt.disconnect();
bluetoothGatt.close();
Log.d(LOG_TAG, "disconnect() - bluetoothGatt disconnect happened");
}
bluetoothGatt = null;
characteristic = null;
bluetoothConnected = false;
servicesDiscovered = false;
// set device as not bonded anymore
bondedBroadcaster.resetDeviceBonded();
}
/**
* bluetooth nearby devices scan is on
* #return true if scanning is on, false otherwise
*/
public boolean isScanning(){
return (this.bluetoothScanning);
}
/**
* Check bluetooth system state (on or off)
* #return true if system is on, false otherwise
*/
public boolean isEnabled(){
try {
checkBluetooth();
return bluetoothAdapter.isEnabled();
} catch (BluetoothNotEnabledException e) {
return false;
}
}
/**
* Check bluetooth connection
* #return true if connected, false otherwise
*/
public boolean isConnected(){
return (this.bluetoothConnected);
}
/**
* Start bluetooth scan for nearby devices
* #param filters Scan filters that define what devices to scan for
*/
public void startScan(List<ScanFilter> filters)
throws BluetoothNotEnabledException{
checkBluetooth();
// dont run two scans simultaneously
if (isScanning()) {
return;
}
// disconnect previously connected devices
if (isConnected()) {
this.disconnect();
return;
}
// setup bluetooth scanning settings
ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)
.build();
// start scanning
this.bluetoothScanning = true;
this.bluetoothScanResults.clear();
this.bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
// Stops scanning after a pre-defined scan period.
Handler bluetoothHandler = new Handler();
bluetoothHandler.postDelayed(new Runnable() {
#Override
public void run() {
stopScan();
}
}, SCAN_TIMEOUT);
// start scan with default scan callback
this.bluetoothLeScanner.startScan(filters, settings, bluetoothScanCallback);
// we have started successfully the BLE scanning
bluetoothListener.onBluetoothStartScan();
}
/**
* Stop bluetooth scan for nearby devices
*/
public void stopScan(){
if (!bluetoothScanning) {
return;
}
// set app scan state to false
bluetoothScanning = false;
if (bluetoothLeScanner != null) {
bluetoothLeScanner.stopScan(bluetoothScanCallback);
bluetoothLeScanner = null;
}
// we have stopped BLE scanning, call the user's callback
bluetoothListener.onBluetoothStopScan(bluetoothScanResults.values());
}
/**
* Send a message via bluetooth
* #param msg message to send
*/
public void send(String msg) {
if (!bluetoothConnected || characteristic == null){
bluetoothListener.onBluetoothNotConnected();
return;
}
try {
msg = msg.replaceAll(DATA_FILTER_REGEX, "") + TERMINATION_CHAR;
Log.d(LOG_TAG, String.format(Locale.getDefault(),
"Sending message: %s",
msg));
characteristic.setValue(msg.getBytes(CHARSET));
bluetoothGatt.writeCharacteristic(characteristic);
} catch (UnsupportedEncodingException e) {
Log.e(LOG_TAG,
"BluetoothLowEnergy.send: Failed to convert message string to byte array");
}
}
public String getDeviceUUID(){
return deviceUUID;
}
public Activity getActivity(){
return activity;
}
/**
* Check if bluetooth is enabled and working properly
*/
private void checkBluetooth() throws BluetoothNotEnabledException{
if (bluetoothAdapter == null) {
final BluetoothManager bluetoothManager =
(BluetoothManager) activity.getSystemService(Context.BLUETOOTH_SERVICE);
if (bluetoothManager == null){
throw new BluetoothNotEnabledException();
}
bluetoothAdapter = bluetoothManager.getAdapter();
}
// Ensures Bluetooth is available on the device and it is enabled. If not,
// displays a dialog requesting user permission to enable Bluetooth.
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
throw new BluetoothNotEnabledException();
}
}
}
The key methods and functions to avoid problems used above are:
Thread.sleep(600)
removeBond(device)
refresh(gatt)
gatt.disconnect()
gatt.close()
In my case I got this response from bluetooth stack because the device was already bonded with my phone. I removed it from my settings and the error 22 vanished.
in aosp (android source code). you can find any error in bluetooth source code, and know the meaning of status code.
the file path is system/bt/stack/include/gatt_api.h
Here's the link: https://android.googlesource.com/platform/system/bt/+/ea7ab70a711e642653dd5922b83aa04a53af9e0e/stack/include/gatt_api.h but it all display by hex.
for example:
hex
Decimal
reason
0x08
8
connection timeout
0x13
19
connection terminate by peer user
0x16
22
connectionterminated by local host
0x22
34
connection fail for LMP response tout
0x85
133
gatt_error

Android BLE LeScanCallback get when stopped

I have a dialog that scans for BLE devices for 10 seconds. When I start my scan I enable a spinner at the footer of the list. When the scan is completed I'd like to remove that spinner. I'm trying to get this to work with the deprecated mBluetoothAdapter.stopLeScan(callback) function instead of the new startScan/stopScan functions as if the device isn't running version 21 or higher, you have to fallback to this method.
stopLeScan requires the same callback as startLeScan but I dont think I see the callback being made. I was hoping that it was a simple check to see if the BluetoohDevice was null, then the callback was made because the scan was stopped, but this didn't work.
With the old version of the SDK, how do you get when the scan has been stopped (either due to the proper device being found or the scan time completed)? I could pass another handler to the my scanLeDevice function, but that just seems silly as I'm already passing a callback.
Bluetooth scanner
public class BleDevice {
private final static String TAG = BleDevice.class.getSimpleName();
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;
private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler;
public BleDevice() {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mHandler = new Handler();
}
public void scanLeDevice(final boolean enable, final BluetoothAdapter.LeScanCallback callback) {
if (enable == true && mScanning == false) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
// Turn off scanning
scanLeDevice(false, callback);
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(callback);
Log.d(TAG, "Starting Bluetooth LE scan");
} else if(enable == false && mScanning == true) {
mScanning = false;
mBluetoothAdapter.stopLeScan(callback);
Log.d(TAG, "Stopped Bluetooth LE scan");
}
}
}
Callback in Dialog Box:
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
#Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
Log.d(TAG, device.getAddress() + " " + device.getName() + "");
if(device == null) {
Log.d(TAG, "Device is null? stop?");
} else {
btAdapter.add(device);
}
}
};
In mBluetoothAdapter.stopLeScan(callback); the callback is just used to identify which scan is to be stopped and it's not supposed to trigger any method in the callback. It's a synchronous operation.
And the BluetoothAdapter.LeScanCallback class doesn't even have any methods beyond the onLeScan() which just receives the results.
So, you can define your own method to be triggered when the scan is stopped by you:
...
} else if(enable == false && mScanning == true) {
mScanning = false;
mBluetoothAdapter.stopLeScan(callback);
Log.d(TAG, "Stopped Bluetooth LE scan");
onScanStopped(); // <--- Remove the spinner here.
}
I'm not aware of any automatic timeout for startLeScan(), so as far as I know it should only stop by calling stopLeScan(). And onLeScan() being triggered doesn't stop the scan either.

BluetoothHeadset: why is it necessary to use a timer for calling startVoiceRecognition?

I wrote some codes for detecting the bluetooth headset connection and start audio through the headset. For API 11 and later, one can call startVoiceRecognition when the headset is connected. So a couple of use cases is as follow:
Headset was turned on before the application launches
The application should check for headset connected on start and establishes audio connection.
User turns on headset during lifetime of the application
The application should register for broadcast of headset connection state and start audio connection when receive connected state.
There is a problem with the second use case. When received the connected state, I call startVoiceRecognition, but it always return false. So I have to implement a timer and after about a second the call will return true. I guess the OS and the headset need sometime to have everything ready to work.
Does anybody know how to get headset audio connection without implement a timer. If it is not possible, should it be the OS that should take care of this situation (for example a READY_FOR_AUDIO_CONNECTION broadcast) instead of the application?
Below is the complete working code for API 11 or later.
Manifest permissions
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
Code
public class MainActivity extends Activity
{
protected TextView mInfoTextview;
protected BluetoothAdapter mBluetoothAdapter;
protected BluetoothHeadset mBluetoothHeadset;
protected BluetoothDevice mConnectedHeadset;
protected AudioManager mAudioManager;
private static final String TAG = "Bluetooth Headset"; //$NON-NLS-1$
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInfoTextview = (TextView) findViewById(R.id.main_textview);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter != null)
{
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (mAudioManager.isBluetoothScoAvailableOffCall())
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
mBluetoothAdapter.getProfileProxy(this, mHeadsetProfileListener, BluetoothProfile.HEADSET);
}
}
}
}
#Override
protected void onDestroy()
{
super.onDestroy();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
if (mBluetoothHeadset != null)
{
// Need to call stopVoiceRecognition here when the app
// change orientation or close with headset still turns on.
mBluetoothHeadset.stopVoiceRecognition(mConnectedHeadset);
unregisterReceiver(mHeadsetBroadcastReceiver);
mCountDown.cancel();
}
mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset);
}
Log.d(TAG, "onDestroy"); //$NON-NLS-1$
}
protected BluetoothProfile.ServiceListener mHeadsetProfileListener = new BluetoothProfile.ServiceListener()
{
/**
* This method is never called, even when we closeProfileProxy on onPause.
* When or will it ever be called???
*/
#Override
public void onServiceDisconnected(int profile)
{
Log.d(TAG, "Profile listener onServiceDisconnected"); //$NON-NLS-1$
mBluetoothHeadset.stopVoiceRecognition(mConnectedHeadset);
unregisterReceiver(mHeadsetBroadcastReceiver);
mBluetoothHeadset = null;
}
#Override
public void onServiceConnected(int profile, BluetoothProfile proxy)
{
Log.d(TAG, "Profile listener onServiceConnected"); //$NON-NLS-1$
// mBluetoothHeadset is just a head set profile,
// it does not represent a head set device.
mBluetoothHeadset = (BluetoothHeadset) proxy;
// If a head set is connected before this application starts,
// ACTION_CONNECTION_STATE_CHANGED will not be broadcast.
// So we need to check for already connected head set.
List<BluetoothDevice> devices = mBluetoothHeadset.getConnectedDevices();
if (devices.size() > 0)
{
// Only one head set can be connected at a time,
// so the connected head set is at index 0.
mConnectedHeadset = devices.get(0);
String log;
// The audio should not yet be connected at this stage.
// But just to make sure we check.
if (mBluetoothHeadset.isAudioConnected(mConnectedHeadset))
{
log = "Profile listener audio already connected"; //$NON-NLS-1$
}
else
{
// The if statement is just for debug. So far startVoiceRecognition always
// returns true here. What can we do if it returns false? Perhaps the only
// sensible thing is to inform the user.
// Well actually, it only returns true if a call to stopVoiceRecognition is
// call somewhere after a call to startVoiceRecognition. Otherwise, if
// stopVoiceRecognition is never called, then when the application is restarted
// startVoiceRecognition always returns false whenever it is called.
if (mBluetoothHeadset.startVoiceRecognition(mConnectedHeadset))
{
log = "Profile listener startVoiceRecognition returns true"; //$NON-NLS-1$
}
else
{
log = "Profile listener startVoiceRecognition returns false"; //$NON-NLS-1$
}
}
mInfoTextview.setText("Device name = " + mConnectedHeadset.getName() //$NON-NLS-1$
+ "\n\n" + log); //$NON-NLS-1$
Log.d(TAG, log);
}
// During the active life time of the app, a user may turn on and off the head set.
// So register for broadcast of connection states.
registerReceiver(mHeadsetBroadcastReceiver,
new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED));
// Calling startVoiceRecognition does not result in immediate audio connection.
// So register for broadcast of audio connection states. This broadcast will
// only be sent if startVoiceRecognition returns true.
registerReceiver(mHeadsetBroadcastReceiver,
new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED));
}
};
protected BroadcastReceiver mHeadsetBroadcastReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
int state;
int previousState = intent.getIntExtra(BluetoothHeadset.EXTRA_PREVIOUS_STATE, BluetoothHeadset.STATE_DISCONNECTED);
String log = ""; //$NON-NLS-1$
if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED))
{
state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_DISCONNECTED);
if (state == BluetoothHeadset.STATE_CONNECTED)
{
mConnectedHeadset = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mInfoTextview.append("\n\nDevice name = " + mConnectedHeadset.getName()); //$NON-NLS-1$
// Audio should not be connected yet but just to make sure.
if (mBluetoothHeadset.isAudioConnected(mConnectedHeadset))
{
log = "Headset connected audio already connected"; //$NON-NLS-1$
}
else
{
// Calling startVoiceRecognition always returns false here,
// that why a count down timer is implemented to call
// startVoiceRecognition in the onTick and onFinish.
if (mBluetoothHeadset.startVoiceRecognition(mConnectedHeadset))
{
log = "Headset connected startVoiceRecognition returns true"; //$NON-NLS-1$
}
else
{
log = "Headset connected startVoiceRecognition returns false"; //$NON-NLS-1$
mCountDown.start();
}
}
}
else if (state == BluetoothHeadset.STATE_DISCONNECTED)
{
// Calling stopVoiceRecognition always returns false here
// as it should since the headset is no longer connected.
mConnectedHeadset = null;
}
}
else // audio
{
state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_AUDIO_DISCONNECTED);
if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED)
{
log = "Head set audio connected, cancel countdown timer"; //$NON-NLS-1$
mCountDown.cancel();
}
else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED)
{
// The headset audio is disconnected, but calling
// stopVoiceRecognition always returns true here.
boolean returnValue = mBluetoothHeadset.stopVoiceRecognition(mConnectedHeadset);
log = "Audio disconnected stopVoiceRecognition return " + returnValue; //$NON-NLS-1$
}
}
log += "\nAction = " + action + "\nState = " + state //$NON-NLS-1$ //$NON-NLS-2$
+ " previous state = " + previousState; //$NON-NLS-1$
mInfoTextview.append("\n\n" + log); //$NON-NLS-1$
Log.d(TAG, log);
}
};
protected CountDownTimer mCountDown = new CountDownTimer(10000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
String log;
if (mBluetoothHeadset.isAudioConnected(mConnectedHeadset))
{
log = "\nonTick audio already connected"; //$NON-NLS-1$
}
else
{
// First stick calls always returns false. The second stick
// always returns true if the countDownInterval is set to 1000.
// It is somewhere in between 500 to a 1000.
if (mBluetoothHeadset.startVoiceRecognition(mConnectedHeadset))
{
log = "\nonTick startVoiceRecognition returns true"; //$NON-NLS-1$
}
else
{
log = "\nonTick startVoiceRecognition returns false"; //$NON-NLS-1$
}
}
mInfoTextview.append(log);
Log.d(TAG, log);
}
#Override
public void onFinish()
{
String log;
if (mBluetoothHeadset.isAudioConnected(mConnectedHeadset))
{
log = "\nonFinish audio already connected"; //$NON-NLS-1$
}
else
{
if (mBluetoothHeadset.startVoiceRecognition(mConnectedHeadset))
{
log = "\nonFinish startVoiceRecognition returns true"; //$NON-NLS-1$
}
else
{
log = "\nonFinish startVoiceRecognition returns false"; //$NON-NLS-1$
}
}
mInfoTextview.append(log);
Log.d(TAG, log);
}
};
}
Layout File
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/main_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="false" />
</ScrollView>

Recording Audio from a Bluetooth Audio Device in Android

How can I record the voice from a paired Bluetooth audio device (i.e. Moster Clarity Bluetooth Speaker) in Android.
I've paired with the device from within Android, and I'd like to record the voice from the microphone on the device (as opposed to using the phone's built-in microphone).
Here's the code I'm using for recording:
try {
isRecording = true;
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
if (file == null) {
File rootDir = Environment.getExternalStorageDirectory();
file = File.createTempFile(PREFIX, EXTENSION, rootDir);
}
recorder.setOutputFile(file.getAbsolutePath());
recorder.prepare();
recorder.start();
timDown = new RecordCountDown(10000, 1000);
timDown.start();
} catch (Exception e) {
Log.i("Error Message", "Error Message :" + e.getMessage());
}
How can I do this?
Try this code maybe helpful for you..
am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
Log.d(TAG, "Audio SCO state: " + state);
if (AudioManager.SCO_AUDIO_STATE_CONNECTED == state) {
/*
* Now the connection has been established to the bluetooth device.
* Record audio or whatever (on another thread).With AudioRecord you can record with an object created like this:
* new AudioRecord(MediaRecorder.AudioSource.MIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
* AudioFormat.ENCODING_PCM_16BIT, audioBufferSize);
*
* After finishing, don't forget to unregister this receiver and
* to stop the bluetooth connection with am.stopBluetoothSco();
*/
unregisterReceiver(this);
}
}
}, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));
Log.d(TAG, "starting bluetooth");
am.startBluetoothSco();
code to voice recording from bluetooth headset
public class Recording {
static int count = 0;
static String Shared;
static String bFlag;
public static int TIMEOUT = 5000;
public static int COUNTDOWN_INTERVAL = 1000;
static Context context;
public static void checkAndRecord(Context context,
OnBluetoothRecording BluetoothRecording, boolean resume) {
// Check bluetooth flag And Bluetooth is ON or OFF
if (getBluetoothFlag(context) && isBluetoothON()) {
// Check for bluetooth and Record
startBluetoothRecording(BluetoothRecording, resume, context);
} else {
// If Bluetooth is OFF Show Toast else Dont Show
if (getBluetoothFlag(context) && !isBluetoothON()) {
// false because recording not started
Toast.makeText(context,
"Bluetooth is OFF. Recording from Phone MIC.",
Toast.LENGTH_SHORT).show();
BluetoothRecording.onStartRecording(resume, false);
} else {
// false because recording not started
BluetoothRecording.onStartRecording(resume, false);
}
}
}
private static void startBluetoothRecording(
final OnBluetoothRecording BluetoothRecording,
final boolean resume, Context context) {
// TODO Auto-generated method stub
final int MAX_ATTEPTS_TO_CONNECT = 3;
final AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
final CountDownTimer timer = getTimer(BluetoothRecording, audioManager,
resume);
context.registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
int state = intent.getIntExtra(
AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
if (AudioManager.SCO_AUDIO_STATE_CONNECTED == state) {
// cancel Timer
timer.cancel();
context.unregisterReceiver(this);
// pass through and true because
// recording from bluetooth so set 8000kHz
BluetoothRecording.onStartRecording(resume, true);
} else if (AudioManager.SCO_AUDIO_STATE_DISCONNECTED == state) {
if (count > MAX_ATTEPTS_TO_CONNECT) {
context.unregisterReceiver(this);
// Stop BluetoothSCO
audioManager.stopBluetoothSco();
// reset Counter
count = 0;
// stop timer
timer.cancel();
// false because still recording not started
BluetoothRecording.onStartRecording(resume, false);
} else {
// Increment Disconnect state Count
count++;
}
}
}
}, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED));
// Start the timer
timer.start();
audioManager.startBluetoothSco();
}
// set the Timeout
private static CountDownTimer getTimer(
final OnBluetoothRecording BluetoothRecording,
final AudioManager audioManager, final boolean resume) {
// TODO Auto-generated method stub
return new CountDownTimer(TIMEOUT, COUNTDOWN_INTERVAL) {
#Override
public void onTick(long millisUntilFinished) {
// Do Nothing
}
#Override
public void onFinish() {
// stopBluetoothSCO() and start Normal Recording
audioManager.stopBluetoothSco();
// false because recording button is already clicked but still
// not recording.
BluetoothRecording.onStartRecording(resume, false);
}
};
}
// Return's the bluetooth state
private static boolean isBluetoothON() {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter
.getDefaultAdapter();
return bluetoothAdapter.isEnabled();
}
// Return's the bluetoothFlag state
private static boolean getBluetoothFlag(Context context) {
// shared pref
SharedPreferences sp = context.getSharedPreferences(Shared,
Context.MODE_PRIVATE);
return sp.getBoolean(bFlag, false);
}
}
Interface OnBluetoothRecording.java
public interface OnBluetoothRecording {
void onStartRecording(boolean state,boolean bluetoothFlag);
void onCancelRecording();
}
The key thing is to call audioManager.startBluetoothSco().
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.
This is an asynchronous operation, as such, you can register a BroadcastReceiver to be notified once audio will start being recorded through the bluetooth headset like so
private BroadcastReceiver mBluetoothScoReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
// Start recording audio
}
}
};
#Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
registerReceiver(mBluetoothScoReceiver, intentFilter);
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.startBluetoothSco();
}
The docs also mention something very important
the application can check the SCO audio state before calling startBluetoothSco() by reading the intent returned by the receiver registration. If the state is already CONNECTED, no state change will be received via the intent after calling startBluetoothSco().
And...
It is however useful to call startBluetoothSco() so that the connection stays active in case the current initiator stops the connection.
This means you do not have to wait for the receiver to be called if the SCO connection is already active. Here is the updated code snippet.
#Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
Intent intent = registerReceiver(mBluetoothScoReceiver, intentFilter);
if (intent == null) {
Log.e(TAG, "Failed to register bluetooth sco receiver...");
return;
}
int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
// Start recording
}
// Ensure the SCO audio connection stays active in case the
// current initiator stops it.
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.startBluetoothSco();
}
You should stop the SCO connection if you are not using it.
private void onPause() {
super.onPause();
unregisterReceiver(mBluetoothScoReceiver);
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.stopBluetoothSco();
}
You will also need the following permissions in your AndroidManifest.xml file
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY"/>

Categories

Resources