Home Android - Recording audio from bluetooth headset sometimes records from the mic
Reply: 0

Android - Recording audio from bluetooth headset sometimes records from the mic

Roni
1#
Roni Published in 2015-11-17 12:54:50Z

I am trying to record audio and analyse it from a Bluetooth headset, to do this I am using the object: android.media.AudioRecord, My problem is that for some devices in combination with what their Android versions, I get the recording from the phone mic instead of the bluetooth headset.

I can solve this issue in some cases - if when I create this object I pass to it's constructor a different audio source type.

For example I saw that in order to record from the Bluetooth headset in Nexus 6 with Android 5.0, I need to construct the AudioRecord like this:

audioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, 44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, 4410);

But If I try to use the same code for a phone called "THL T6S" that also has Android 5.0, the recording comes from phone mic. If I want it to be from the headset in this phone I need to write:

audioRecord = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION, 44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, 4410);

But off course I can't cover in my code all the possible combinations of phone types and OS versions.

How can I solve it?

Attached all the relevant code.

Thanks.

This is my Bluetooth Sco class

public class BluetoothSco {

private static BluetoothSco instance = null;

// Context
private Context mContext;
private Service service;

// Logcat TAG
private final String TAG = BluetoothSco.class.getSimpleName();

// AudioMode
public static final int MODE_IN_CALL = 1;
public static final int MODE_IN_COMMUNICATION = 2;
public static final int MODE_NORMAL = 3;

// BluetoothAdapter
private final BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

protected BluetoothSco() { }

public static BluetoothSco getInstance(Context context) {
    if(instance == null) {
        instance = new BluetoothSco();
        instance.mContext = context;
        instance.service = (Service) context;
    }

    return instance;
}

// Start Sco
public synchronized void startSco(final int audioMode) {

    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            if (btAdapter == null) {
                Log.i(TAG, "This device does not support Bluetooth");
                return;
            }
            else if (!btAdapter.isEnabled()) {
                Log.i(TAG, "Bluetooth headset is disconnected. Would you like to monitor manually?");
                return;
            }

            AudioManager audioManager = (AudioManager) service.getSystemService(Context.AUDIO_SERVICE);
            if (!audioManager.isBluetoothScoAvailableOffCall()) {
                Log.i(TAG, "Off-call Bluetooth audio not supported on this device.");
                return;
            }

            if (audioMode == BluetoothSco.MODE_IN_CALL) {
                audioManager.setMode(AudioManager.MODE_IN_CALL);
            }
            else if (audioMode == BluetoothSco.MODE_IN_COMMUNICATION) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                    audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
                }
                else {
                    audioManager.setMode(AudioManager.MODE_NORMAL);
                }
            }
            else if (audioMode == BluetoothSco.MODE_NORMAL) {
                audioManager.setMode(AudioManager.MODE_NORMAL);
            }


            //Roni - temp code/////////
            //audioManager.setMode(AudioManager.MODE_IN_CALL);
            ////////////////////

            audioManager.setBluetoothScoOn(true);

            try{
                audioManager.startBluetoothSco();
                Log.v(TAG, "Bluetooth SCO Started");
            }
            catch(NullPointerException ex){
                Log.i(TAG, "Bluetooth headset is disconnected. Would you like to monitor manually?");
            }
        }
    };
    runnable.run();
}

public synchronized void stopSco() {
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            AudioManager audioManager = (AudioManager) service.getSystemService(Context.AUDIO_SERVICE);

            try {
                audioManager.setBluetoothScoOn(false);
                audioManager.stopBluetoothSco();
                audioManager.setMode(AudioManager.MODE_NORMAL);

                Log.v(TAG, "Bluetooth SCO Stopped");
            }
            catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }
    };
    runnable.run();
}

}

And here is how I initialize the AudioRecord and Bluetooth Sco

audioRecord = new AudioRecord(Options.audioSource, Options.sampleRate, Options.channelConfig,
                Options.audioEncoding, Options.numBuffers * Math.max(minBufSize,  bufferSizeBytes));
mBluetoothSco.startSco(BluetoothSco.MODE_NORMAL);
audioRecord.startRecording();
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.307456 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO