3
votes

I'm recording live audio from a bluetooth headset's mic and playing it onto the phone's speaker.

Streaming code:

 int bufferSize = AudioRecord.getMinBufferSize(samplingRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
        arec = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION, samplingRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);
        //STREAM_MUSIC - no sound
        //STREAM_ALARM - beep from phone's speaker
        //STREAM_RING - no sound
        //STREAM_NOTIFICATION - no sound
        //STREAM_SYSTEM - no sound
        //STREAM_VOICE_CALL - plays back on bt headset
        //STREAM_DTMF - sound from loud speaker, not very clear but works for now
        atrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL, samplingRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);
        atrack.setPlaybackRate(samplingRate);
        byte[] buffer = new byte[bufferSize];
        arec.startRecording();
        atrack.play();
        while(isRecording) {
            arec.read(buffer, 0, bufferSize);
            atrack.write(buffer, 0, buffer.length);
        }

Configuration of bluetooth headset mic code:

audioManager.setBluetoothScoOn(true);
    audioManager.startBluetoothSco();
    //works for MODE_IN_COMMUNICATION - however there's a lot of echo when on speaker phone
    //works fine for MODE_IN_CALL
    audioManager.setMode(AudioManager.MODE_IN_CALL);
    //turning speakerphone on causes a lot of disturbance
    //audioManager.setSpeakerphoneOn(true);
    //samplingRate = AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_VOICE_CALL);
    samplingRate = 16000;

However, even when I have setSpeakerphoneOn(false) and insert a earphones into the audio jack, the audio is played from the phone itself and not the headset. How do I overcome this? Turning the loudspeaker on causes a lot of noise.

I'm using a sampling rate of 16000 because it can be heard without headphones. The moment I use the native sampling rate, I obviously just hear really high frequency noise. How can I get this sound to play on the headset rather than the phone's speaker?

1

1 Answers

1
votes

Unfortunately for you, with the deprecation of explicit routing there is no supported combination of bluetooth and speakerphone settings which will enable you to use the bluetooth headset's microphone but the handset's speakers (earpiece/headset or speakerphone). Given the many possible nonsensical combinations, I suspect that's the rationale for moving to the simpler api.

You could try calling the deprecated setRouting() function, however this may have unexpected side effects and/or not even work depending upon what the hardware is capable of and what cases are correctly managed in the underlying subsystems/drivers.