Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Function to route audio between smartphone and bluetooth module

This is my first post on Stackoverflow. I try to route audio on android smartphone for the past few days but I don't find function to do that properly.

It is for making a Bluetooth Babyphone.

I need to make a routing between smartphone and Bluetooth module. The Bluetooth module is initially used to make Handset, cars Handfree, wireless speaker...

I communicate with A2DP and HFP(Handfree) profiles for the audio side of the project.

I can establish a “sco” connection(connection used with Audio Bluetooth exchanges ) between devices and get back audio from Bluetooth module. But when a connection “sco” is working, I can no more use the speaker and microphone on my smartphone.

I hope to find a solution to use audio on my smartphone and in the same time the audio on my Bluetooth module.

I searched on http://developer.android.com/index.html for a function to route audio.

The AudioManager class has some functions to route audio like setRouting or setParameters but I have any result yet. http://developer.android.com/reference/android/media/AudioManager.html

You can see below the code I use to get audio from babyphone side(Bluetooth module side):

boolean isRecording=true;
int buffersize = 8000;
byte[] buffer = new byte[buffersize];

//audio configuration and SCO Bluetooth connection. 
AudioManager aManager = (AudioManager) getSystemService(AUDIO_SERVICE);
android.os.Process.setThreadPriority(
            android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
aManager.startBluetoothSco();
aManager.setBluetoothScoOn(true);
aManager.setMode(AudioManager.MODE_IN_COMMUNICATION);

//AudioRecord configuation for recording audio from babyphone.
AudioRecord  arec = new AudioRecord(
            MediaRecorder.AudioSource.VOICE_COMMUNICATION,
            8000,
            AudioFormat.CHANNEL_IN_MONO,
            AudioFormat.ENCODING_PCM_16BIT,
            buffersize); 

//AudioTrack configuation for sending audio to smartphone speaker.                
AudioTrack  atrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
                8000,
                AudioFormat.CHANNEL_OUT_MONO,
                AudioFormat.ENCODING_PCM_16BIT,
                buffersize,
                AudioTrack.MODE_STREAM);

atrack.setPlaybackRate(8000);

//start audio recording and playing.
arec.startRecording();
atrack.play();

while(isRecording) {
    arec.read(buffer, 0, buffersize);

    atrack.write(buffer, 0, buffer.length);
}

arec.stop();
atrack.stop()

If I can not route audio to smartphone speaker and use “sco” connection in the same time to get back audio from microphone on the babyphone side, I need to know it as soon as possible for changing the direction of my project.

The babyphone program on the smartphone side needs to run on all recent smartphones if possible.

English is not my first language so some sentence may not be correct.

I’m open to any solution.

like image 907
user2235813 Avatar asked Nov 12 '22 08:11

user2235813


1 Answers

A2DP is for media playback, and is one-way. SCO is used for voice calls / VoIP / recording over BT and is two-way but at a lower quality.

What will happen (on most/all devices I've worked with at least) when you enable BluetoothSco and set the mode to IN_CALL or IN_COMMUNICATION is that STREAM_VOICE_CALL will be routed to the BT_SCO output device, and the input device will be selected based on the output device (because of the mode) which means the BT_SCO input device.

My suggestion - although I can't guarantee that it will work on every device - is to not set the mode to IN_COMMUNICATION. The setBluetoothScoOn(true) call will internally set the "force use" setting for recording to BT_SCO, and combining that with using the DEFAULT or VOICE_RECOGNITION AudioSource should select the BT_SCO mic as the recording device.
However, recording from BT_SCO and the internal microphone at the same time will probably prove impossible to do in a portable manner.
For the output you could use the RING, ALARM or MUSIC stream types, which should be routed to the internal loudspeaker rather than to BT_SCO when not in a voice call.

like image 111
Michael Avatar answered Nov 14 '22 22:11

Michael