Skip to content Skip to sidebar Skip to footer

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

Solution 1:

Try this code maybe helpful for you..

am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

registerReceiver(newBroadcastReceiver() {

@OverridepublicvoidonReceive(Context context, Intent intent) {
    intstate= 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);
    }

    }
}, newIntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));

Log.d(TAG, "starting bluetooth");
am.startBluetoothSco();

Solution 2:

code to voice recording from bluetooth headset

publicclassRecording {

staticintcount=0;
static String Shared;
static String bFlag;
publicstaticintTIMEOUT=5000;
publicstaticintCOUNTDOWN_INTERVAL=1000;
static Context context;

publicstaticvoidcheckAndRecord(Context context,
        OnBluetoothRecording BluetoothRecording, boolean resume) {

    // Check bluetooth flag And Bluetooth is ON or OFFif (getBluetoothFlag(context) && isBluetoothON()) {

        // Check for bluetooth and Record
        startBluetoothRecording(BluetoothRecording, resume, context);

    } else {

        // If Bluetooth is OFF Show Toast else Dont Showif (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);
        }
    }

}

privatestaticvoidstartBluetoothRecording(
        final OnBluetoothRecording BluetoothRecording,
        finalboolean resume, Context context) {
    // TODO Auto-generated method stubfinalintMAX_ATTEPTS_TO_CONNECT=3;
    finalAudioManageraudioManager= (AudioManager) context
            .getSystemService(Context.AUDIO_SERVICE);

    finalCountDownTimertimer= getTimer(BluetoothRecording, audioManager,
            resume);

    context.registerReceiver(newBroadcastReceiver() {

        @OverridepublicvoidonReceive(Context context, Intent intent) {

            intstate= 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);

            } elseif (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++;

                }
            }

        }
    }, newIntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED));

    // Start the timer
    timer.start();
    audioManager.startBluetoothSco();

}

// set the Timeoutprivatestatic CountDownTimer getTimer(
        final OnBluetoothRecording BluetoothRecording,
        final AudioManager audioManager, finalboolean resume) {
    // TODO Auto-generated method stubreturnnewCountDownTimer(TIMEOUT, COUNTDOWN_INTERVAL) {

        @OverridepublicvoidonTick(long millisUntilFinished) {
            // Do Nothing

        }

        @OverridepublicvoidonFinish() {

            // 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 stateprivatestaticbooleanisBluetoothON() {

    BluetoothAdapterbluetoothAdapter= BluetoothAdapter
            .getDefaultAdapter();
    return bluetoothAdapter.isEnabled();
}

// Return's the bluetoothFlag stateprivatestaticbooleangetBluetoothFlag(Context context) {

    // shared prefSharedPreferencessp= context.getSharedPreferences(Shared,
            Context.MODE_PRIVATE);
    return sp.getBoolean(bFlag, false);

}

}

Interface OnBluetoothRecording.java

publicinterfaceOnBluetoothRecording {

voidonStartRecording(boolean state,boolean bluetoothFlag);
voidonCancelRecording();
}

Solution 3:

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

privateBroadcastReceivermBluetoothScoReceiver=newBroadcastReceiver() {
    @OverridepublicvoidonReceive(Context context, Intent intent) {
        intstate= intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);

        if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
            // Start recording audio
        }
    }
};


@OverrideprotectedvoidonResume() {
    super.onResume();

    IntentFilterintentFilter=newIntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
    registerReceiver(mBluetoothScoReceiver, intentFilter);

    AudioManageraudioManager= (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.

@OverrideprotectedvoidonResume() {
    super.onResume();

    IntentFilterintentFilter=newIntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
    Intentintent= registerReceiver(mBluetoothScoReceiver, intentFilter);
    if (intent == null) {
        Log.e(TAG, "Failed to register bluetooth sco receiver...");
        return;
    }

    intstate= 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.AudioManageraudioManager= (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    audioManager.startBluetoothSco();
}

You should stop the SCO connection if you are not using it.

privatevoidonPause() {
    super.onPause();

    unregisterReceiver(mBluetoothScoReceiver);

    AudioManageraudioManager= (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    audioManager.stopBluetoothSco();
}

You will also need the following permissions in your AndroidManifest.xml file

<uses-permissionandroid:name="android.permission.RECORD_AUDIO"/><uses-permissionandroid:name="android.permission.MODIFY_AUDIO_SETTINGS" /><uses-permissionandroid:name="android.permission.BROADCAST_STICKY"/>

Post a Comment for "Recording Audio From A Bluetooth Audio Device In Android"