Захват аудио через гарнитуру Bluetooth в сочетании с устройством Android

Я пытаюсь захватить звук с гарнитуры Bluetooth в сочетании с устройством Android.

Ниже приведен соответствующий код:

Intent in=null;
final int bufferSize=BufferElements2Rec*BytesPerElement;

final BroadcastReceiver brr=new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context,Intent intent)
    {
        int state=intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE,-1);
        Log.d(labelApp,"Audio SCO State = "+state);
        if(AudioManager.SCO_AUDIO_STATE_CONNECTED==state)
        {
            Log.d(labelApp,"Entered and Starting Recording");
            //recorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT,
            //        RECORDER_SAMPLERATE, RECORDER_CHANNELS,
            //        RECORDER_AUDIO_ENCODING, bufferSize);
            recorder = new AudioRecord(android.media.MediaRecorder.AudioSource.MIC,
                RECORDER_SAMPLERATE, RECORDER_CHANNELS,
                RECORDER_AUDIO_ENCODING, bufferSize);
            if(recorder==null)
            {
                Log.d(labelApp,"null");
            }
            else
            {
                Log.d(labelApp,"not null");
            }
            recorder.startRecording();
            recordingThread=new Thread(new Runnable()
            {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    writeAudioDataToFile();
                }

            },"AudioRecorder Thread");
            recordingThread.start();

            Log.d(labelApp,"Launched Recording Thread");
        }
    }
};

try
{
    Log.d(labelApp,"Initializing BT");
    am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    //am.setMode(AudioManager.MODE_IN_CALL);

    //in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));
    //in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));
    Log.d(labelApp,"Starting Bluetooth");
    am.setStreamSolo(AudioManager.MODE_IN_CALL, true);
    am.setBluetoothScoOn(true);
    am.setMode(AudioManager.MODE_IN_CALL);
    am.startBluetoothSco();
    Log.d(labelApp,"Can BT record from mic? "+am.isBluetoothScoAvailableOffCall());
    //in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));
    in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED));
    // The following line makes the audio go to hell
    //am.setMode(AudioManager.MODE_IN_CALL);


    //am.setStreamSolo(AudioManager.MODE_IN_CALL, true);
    Log.d(labelApp,"Everything initializated");
    Log.d(labelApp,"Recorder is...");
}
catch(Exception e)
{
    Log.e(labelApp,"exception",e);
    writeStack(e);
}

try
{
        Log.d(labelApp,"Initializing BT");
        am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        //am.setMode(AudioManager.MODE_IN_CALL);

        //in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));
        //in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));
        Log.d(labelApp,"Starting Bluetooth");
        am.setStreamSolo(AudioManager.MODE_IN_CALL, true);
        am.setBluetoothScoOn(true);
        am.setMode(AudioManager.MODE_IN_CALL);
        am.startBluetoothSco();
        Log.d(labelApp,"Can BT record from mic? "+am.isBluetoothScoAvailableOffCall());
        //in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));
        in=registerReceiver(brr,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED));
        // The following line makes the audio go to hell
        //am.setMode(AudioManager.MODE_IN_CALL);


        //am.setStreamSolo(AudioManager.MODE_IN_CALL, true);
        Log.d(labelApp,"Everything initializated");
        Log.d(labelApp,"Recorder is...");
}
catch(Exception e)
{
        Log.e(labelApp,"exception",e);
        writeStack(e);
}

Манифест просит разрешения для:

  • WRITE_EXTERNAL_STORAGE
  • ЗАПИСЬ АУДИО
  • ИНТЕРНЕТ
  • MODIFY_AUDIO_SETTINGS
  • BROADCAST_STICKY
  • БЛЮТУЗ
  • BLUETOOTH_ADMIN

Типичный выход Filtered LogCat для приложения:

  1. Инициализация BT
  2. Запуск Bluetooth
  3. Может ли BT записывать с микрофона? правда
  4. Все инициализировано
  5. Рекордер это...
  6. Аудио SCO State = 2
  7. Аудио SCO State = 1
  8. Введена и начинается запись
  9. ненулевой
  10. Запущенная запись темы

Когда am.startBluetoothSco(); вызывается, я слышу короткий шум на устройстве BT, но затем приложение просто получает звук с микрофона Android-устройства вместо микрофона BT.

Любой намек на то, что я пропускаю / делаю неправильно?

Заранее спасибо за внимание

2 ответа

Самое главное, вам нужно установить
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

в файле манифеста. Без него не будет сообщения об ошибке, но состояние B/T откажется изменить на подключенное.

Другие соответствующие разрешения включают в себя:

<uses-permission android:name="android.permission.RECORD_AUDIO"/> 
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/> 
<uses-permission android:name="android.permission.BROADCAST_STICKY"/>

Изменить: В дополнение к комментариям, вот пример кода, который я использовал ранее:

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

brr(new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
        Log.d(TAG, "Audio SCO state: " + state);

        if (AudioManager.SCO_AUDIO_STATE_CONNECTED == state) { 
            /* 
             * Now the connection has been established to the bluetooth device. 
             * Record audio or whatever (on another thread).With AudioRecord you can record with an object created like this:
             * new AudioRecord(MediaRecorder.AudioSource.MIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
             * AudioFormat.ENCODING_PCM_16BIT, audioBufferSize);
             *
             * After finishing, don't forget to unregister this receiver and
             * to stop the bluetooth connection with am.stopBluetoothSco();
             */
            unregisterReceiver(this);
        }

    }
}, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));

Log.d(TAG, "starting bluetooth");
am.startBluetoothSco();

Кредиты по этому коду идут пользователю Stephan

Код для записи голоса с Bluetooth-гарнитуры

public class Recording {

static int count = 0;
static String Shared;
static String bFlag;
public static int TIMEOUT = 5000;
public static int COUNTDOWN_INTERVAL = 1000;
static Context context;

public static void checkAndRecord(Context context,
        OnBluetoothRecording BluetoothRecording, boolean resume) {

    // Check bluetooth flag And Bluetooth is ON or OFF
    if (getBluetoothFlag(context) && isBluetoothON()) {

        // Check for bluetooth and Record
        startBluetoothRecording(BluetoothRecording, resume, context);

    } else {

        // If Bluetooth is OFF Show Toast else Dont Show
        if (getBluetoothFlag(context) && !isBluetoothON()) {
            // false because recording not started
            Toast.makeText(context,
                    "Bluetooth is OFF. Recording from Phone MIC.",
                    Toast.LENGTH_SHORT).show();
            BluetoothRecording.onStartRecording(resume, false);
        } else {
            // false because recording not started
            BluetoothRecording.onStartRecording(resume, false);
        }
    }

}

private static void startBluetoothRecording(
        final OnBluetoothRecording BluetoothRecording,
        final boolean resume, Context context) {
    // TODO Auto-generated method stub

    final int MAX_ATTEPTS_TO_CONNECT = 3;
    final AudioManager audioManager = (AudioManager) context
            .getSystemService(Context.AUDIO_SERVICE);

    final CountDownTimer timer = getTimer(BluetoothRecording, audioManager,
            resume);

    context.registerReceiver(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

            int state = intent.getIntExtra(
                    AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
            if (AudioManager.SCO_AUDIO_STATE_CONNECTED == state) {
                // cancel Timer
                timer.cancel();
                context.unregisterReceiver(this);

                // pass through and true because
                // recording from bluetooth so set 8000kHz

                BluetoothRecording.onStartRecording(resume, true);

            } else if (AudioManager.SCO_AUDIO_STATE_DISCONNECTED == state) {
                if (count > MAX_ATTEPTS_TO_CONNECT) {
                    context.unregisterReceiver(this);
                    // Stop BluetoothSCO
                    audioManager.stopBluetoothSco();
                    // reset Counter
                    count = 0;
                    // stop timer
                    timer.cancel();
                    // false because still recording not started
                    BluetoothRecording.onStartRecording(resume, false);
                } else {
                    // Increment Disconnect state Count
                    count++;

                }
            }

        }
    }, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED));

    // Start the timer
    timer.start();
    audioManager.startBluetoothSco();

}

// set the Timeout
private static CountDownTimer getTimer(
        final OnBluetoothRecording BluetoothRecording,
        final AudioManager audioManager, final boolean resume) {
    // TODO Auto-generated method stub
    return new CountDownTimer(TIMEOUT, COUNTDOWN_INTERVAL) {

        @Override
        public void onTick(long millisUntilFinished) {
            // Do Nothing

        }

        @Override
        public void onFinish() {

            // stopBluetoothSCO() and start Normal Recording
            audioManager.stopBluetoothSco();

            // false because recording button is already clicked but still
            // not recording.
            BluetoothRecording.onStartRecording(resume, false);
        }
    };
}

// Return's the bluetooth state
private static boolean isBluetoothON() {

    BluetoothAdapter bluetoothAdapter = BluetoothAdapter
            .getDefaultAdapter();
    return bluetoothAdapter.isEnabled();
}

// Return's the bluetoothFlag state
private static boolean getBluetoothFlag(Context context) {

    // shared pref
    SharedPreferences sp = context.getSharedPreferences(Shared,
            Context.MODE_PRIVATE);
    return sp.getBoolean(bFlag, false);

}

}
Другие вопросы по тегам