简体   繁体   中英

Understanding Bluetooth Headset connection with Android

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

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

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

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?
      }}

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?

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

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

and call

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM