I am working with a voice recognition app. After a bluetooth headset is successfully connected to my android device, I want to freely switch audio input between the inbuilt mic and headset mic, how to do it?
1 Answers
0
votes
You can use the following code to switch bluetooth headset mic.
AudioManager audiomanager= (AudioManager)mContext.getSystemService(Context
.AUDIO_SERVICE);
IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
mContext.registerReceiver(mBluetoothScoReceiver, intentFilter);
audiomanager.startBluetoothSco();
and listen for the broadcast receiver.
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
log("audio connected");
startRecording();
} else if(state == AudioManager.SCO_AUDIO_STATE_DISCONNECTED){
log("audio disconnected");
}
}
};
and for switching(to disconnect bluetooth headset mic) you can use this code.
mAudioManager.stopBluetoothSco();