了解了Android蓝牙耳机连接

了解了Android蓝牙耳机连接

本文介绍了了解了Android蓝牙耳机连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个蓝牙耳机(BTC6White)。我想对着麦克风讲话,并在Android设备播放声音。

I have a Bluetooth headset (BTC6White). I want to speak into the microphone and an Android device play the sound.

那么,如何才能做到这一点?首先,我可以建立一个socket连接

So, How I can do this? First, I can establish a socket connection

socket = mDevice.createRfcommSocketToServiceRecord(UUID.fromString("0000111e-0000-1000-8000-00805f9b34fb"));
socket.connect();

那么,如何得到我的声音?有什么用此方法:startBuetoothSco?把音频扬声器,我应该使用Auditrack?

Then, how I get the audio? What is the use this method: startBuetoothSco? To put the audio in the speaker, Should I use Auditrack?

int buffersize = AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_8BIT);
soundData = new byte [buffersize*5];
audioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
   8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_8BIT,
   soundData.length,AudioTrack.MODE_STREAM);

不过,那么,我应该填充缓冲区soundData?怎么样?使用插座(成线)?

But, then, Should I fill the buffer soundData? How? Using the socket (into a Thread)?

  mmInStream = socket.getInputStream();
  public void run() {
  byte[] buffer = new byte[8000*2];
      while (true) {
           bytes = mmInStream.read(buffer);
            audioTrack.write(buffer, 0, bytes); //write directly?
      }}

和startBuetoothSco()是为了什么?要知道,上海合作组织国家? SCO_AUDIO_STATE_CONNECTED ...或者发送/接收数据?我不明白怎么去从耳机得到德音频数据以及如何分流到扬声器。建立SCO连接(与AudioManager)来获得蓝牙耳机的数据这是必要的吗?

And startBuetoothSco() for what? To know sco states? SCO_AUDIO_STATE_CONNECTED... Or to send/receive data? I don't understand how get de audio data from de headset and then how to streaming to the speaker. It's necessary establish a SCO connection (with AudioManager) to get the data of the bluetooth headset?

这是关于这个问题,而Android文档很难找得到的信息是非常差的(这个话题)。

It's very difficult find information about this problem and the android documentation is very poor (this topic).

推荐答案

,你可以设置流源。举例来说,如果你想记录通过蓝牙耳机的声音,你可以初始化AudioRecord和AudioManager如下:

for retrieving audio data, you could set the stream source. for instance, if you want to record sound via bluetooth headset, you could initialize AudioRecord and AudioManager as following:

AudioRecord ar = new AudioRecord(MediaRecorder.AudioSource.MIC,
                    RECORDER_SAMPLERATE,
                    RECORDER_OUT_CHANNELS,
                    RECORDER_AUDIO_ENCODING,
                    bufferSize);
ar.startRecording();

和电话

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

您可以再定义一个线程读取的音频数据在其中写上如下code:

you can then define a thread for reading audio data in which you write the following code:

private Runnable run_record = new Runnable(){

    @Override
    public void run() {
        // TODO Auto-generated method stub
        short dataBuf[] = new short[bufferSize];
        FileOutputStream os = null;
        try {
            os = new FileOutputStream(file_path);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        while(isRecording){
            ar.read(dataBuf, 0, bufferSize);
            Log.d(LOG_TAG, "writing "+bufferSize+" bytes of data to file "+file_path);
            byte data[] = short2byte(dataBuf);
            try {
                os.write(data, 0, bufferSize);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        try {
            os.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }};

我希望我解释清楚了。

i wish i explained it clearly.

这篇关于了解了Android蓝牙耳机连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 12:32