1

I have a problem with android wear's microphone.

Bluetooth headset works with android wear. My app release VoIP application. When I play voice (from network) in my app with paired bluetooth headset, headset play this voice. But when I try record voice from the microphone.. turns on the microphone of the android wear, not the headset.

How can I implement voice reading from the Bluetooth headset microphone??

Attach Player.class

public class Player {
private static final String TAG = Player.class.getName();
private AudioTrack audioTrack;
private boolean isWorking;

public Player() {
    try {
        audioTrack = new AudioTrack(
                new AudioAttributes.Builder()
                        .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
                        .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                        .setLegacyStreamType(AudioManager.STREAM_MUSIC)
                        .build(),

                new AudioFormat.Builder()
                        .setChannelMask(AudioFormat.CHANNEL_OUT_MONO)
                        .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
                        .setSampleRate(AudioConsts.SAMPLERATE)
                        .build(),

                AudioConsts.GetPlayerBufferSize(),
                AudioTrack.MODE_STREAM,
                AudioManager.AUDIO_SESSION_ID_GENERATE);

    } catch (Exception e) {
        Log.e(TAG, e.toString());
    }

}

public void play() {
    audioTrack.play();
}

public void stopReading() {
    if (!isWorking)
        return;

    audioTrack.release();
    isWorking = false;
}

public void appendForPlayback(byte[] audioMessage, int size) {
    new Executor().doInBackground(audioMessage);
}

private class Executor extends AsyncTask<byte[], Void, Void> {
    @Override
    protected Void doInBackground(byte[]... bytes) {
        if (bytes != null) {
            if (bytes.length > 0) {
                byte[] audioMessage = bytes[0];
                if (audioMessage.length != 0) {
                    int written = audioTrack.write(audioMessage, 0, audioMessage.length);
                    if (written != audioMessage.length) {
                        Log.d(TAG, "WTF");
                    }
                }
            }
        }

        return null;
    }
}}

Attach Recorder.class

public class Recorder {
private static final String TAG = Recorder.class.getName();

private boolean isAlive;
private Thread recordThread;
private IRecorderBytesListener listener;
private AudioRecord audioRecord;

public Recorder() {
    isAlive = true;

    audioRecord = new AudioRecord.Builder()
            .setAudioSource(MediaRecorder.AudioSource.MIC)
            .setAudioFormat(new AudioFormat.Builder()
                    .setSampleRate(AudioConsts.SAMPLERATE)
                    .setEncoding(AudioConsts.ENCODING_PCM_16BIT)
                    .build())
            .setBufferSizeInBytes(AudioConsts.GetRecorderBufferSize())
            .build();

    //audioRecord.setPreferredDevice(audioDeviceInfo);

    recordThread = new Thread(() -> {
        ByteBuffer buffer = ByteBuffer.allocateDirect(AudioConsts.GetRecorderBufferSize());
        byte[] audioMsg = new byte[AudioConsts.FRAME_SIZE * AudioConsts.ENCODING_PCM_16BIT];

        while (isAlive) {
            if (audioRecord.getRecordingState() == 1) {
                try {
                    Thread.sleep(50);
                } catch (Exception e) {
                    Log.d(TAG, "hz");
                }
                continue;
            }

            buffer = (ByteBuffer) buffer.rewind();
            int len = audioRecord.read(buffer, AudioConsts.GetRecorderBufferSize());
            if (len != AudioConsts.GetRecorderBufferSize())
                Log.d(TAG, "WTF LEN");

            len -= AudioConsts.OFFSET_AUDIO_RECORDER;

            if (len > 0) {
                try {
                    System.arraycopy(buffer.array(), AudioConsts.OFFSET_AUDIO_RECORDER,
                            audioMsg, 0, len);

                    if (listener != null)
                        listener.bytesReceived(audioMsg, len);
                } catch (Exception e) {
                    Log.e(TAG, e.toString());
                }
            } else {
                Log.d(TAG, "WTF");
            }
        }

        audioRecord.stop();
    });

    recordThread.start();
}

public void startRecording() {
    audioRecord.startRecording();
}

public void stopRecording() {
    audioRecord.stop();
}

public void setListener(IRecorderBytesListener listener) {
    this.listener = listener;
}

public void dispose() {
    isAlive = false;
}}

Sorry for my English.

  • You may want to check out [`AudioManager.startBluetoothSco()`](https://developer.android.com/reference/android/media/AudioManager#startBluetoothSco()) method. I think this [answer](https://stackoverflow.com/a/36073000) also explains the usage of the method pretty well. – Jacque Nov 16 '18 at 08:08
  • Android wear also does not use headset mic with SCO – Vlad Doroshko Nov 19 '18 at 08:19

0 Answers0