Android的 - 功能以路由音频智能手机和蓝牙模块之间蓝牙、智能手机、路由、模块

2023-09-05 00:55:18 作者:风尘五

这是我在#1的第一篇文章。 我尝试将音频路由的Andr​​oid智能手机,在过去几天,但我并没有发现功能,这样做正确。

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.

有用于使蓝牙Babyphone

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...

我与该项目的音频方面支持A2DP和HFP(免提)配置文件进行通信。

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.

我搜索的 http://developer.android.com/index.html 的功能将音频信号。

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

在AudioManager类有一些功能,将音频信号像setRouting或setParameters,但我有什么结果呢。 http://developer.android.com/reference/android/media/AudioManager.html

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

您可以看到下面的code我用它来得到babyphone侧(蓝牙模块侧)音频:

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()

如果我不能将音频路由到智能手机的扬声器,并使用在同一时间回到音频从麦克风上的babyphone方上海合作组织的连接,我需要尽快知道它改变了我的项目的方向。

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.

在智能手机端的babyphone程序需要的所有最新的智能手机如果可能的话,执行

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.

推荐答案

A2DP是媒体播放,并且是单向的。合组织,用于语音呼叫/语音/记录过BT和是双向但以较低的质量。

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.

会发生什么(在大多数/我曾经使用过的所有设备至少),当您启用BluetoothSco和模式设置为 IN_CALL IN_COMMUNICATION STREAM_VOICE_CALL 将被路由到所述BT_SCO输出装置,和输入装置将根据由于所述模式的输出装置上(被选择)这意味着BT_SCO输入装置

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.

我的建议 - 虽然我不能保证它会工作在每个设备上 - 是为不可以将模式设置为 IN_COMMUNICATION 。该 setBluetoothScoOn(真)电话将在内部设置了强制使用设置录制BT_SCO,并结合与使用默认 VOICE_RECOGNITION AudioSource应选择BT_SCO麦克风为录音设备。 然而,从BT_SCO和同时内置麦克风记录将可能证明不可能做在一个可移植的方式。 对于输出,你可以使用 RING ALARM 音乐流类型,它应该被路由至内部扬声器而不是BT_SCO在不进行语音呼叫。

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.