0

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

jlmg5564
  • 1,380
  • 13
  • 28
  • You can use my answer at http://stackoverflow.com/questions/14991158/using-the-android-recognizerintent-with-a-bluetooth-headset/14993590#14993590 – Hoan Nguyen Mar 15 '13 at 22:01
  • Oks, I can establish a SCO connection, but then, how do I get the audio? – jlmg5564 Mar 16 '13 at 13:13
  • I do not know if it is possible to rout audio from bluetooth headset to the speaker. Why don't you try SoundAbout to see if it does anything similar first. Link https://play.google.com/store/apps/details?id=com.woodslink.android.wiredheadphoneroutingfix – Hoan Nguyen Mar 17 '13 at 00:09

1 Answers1

0

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.

Yuan
  • 135
  • 1
  • 9