首页 文章

如何将音频从Android设备重定向到蓝牙speeker

提问于
浏览
1

我正在尝试将我在应用程序中播放的所有音频重定向到蓝牙音箱 . 首先我配对蓝牙设备,然后我试着向audioManager'说',我播放的所有音频都应该发送到蓝牙设备:

private final BluetoothAdapter _bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

public void pairBluetoothDevice(BluetoothDevice bluetoothDevice)
{
    BluetoothSocket socket= bluetoothDevice.createInsecureRfcommSocketToServiceRecord( UUID.fromString( "0000111E-0000-1000-8000-00805F9B34FB" ) );
    socket.connect();

    _bluetoothAdapter.getProfileProxy( _appContext, _profileListener, BluetoothProfile.HEADSET );
}

private BluetoothProfile.ServiceListener _profileListener = new BluetoothProfile.ServiceListener()
{
  public void onServiceConnected( int profile, BluetoothProfile proxy )
  {
     if ( profile == BluetoothProfile.HEADSET )
     {
        _bluetoothHeadset = (BluetoothHeadset) proxy;
        _bluetoothHeadset.startVoiceRecognition(_device);

        AudioManager audioManager = (AudioManager) _appContext.getSystemService(Context.AUDIO_SERVICE);
        audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
        audioManager.startBluetoothSco();
        audioManager.setBluetoothScoOn(true);
        audioManager.setSpeakerphoneOn(false); 
     }
  }

  public void onServiceDisconnected( int profile )
  {
     if ( profile == BluetoothProfile.HEADSET )
     {
        AudioManager audioManager= (AudioManager) _appContext.getSystemService(Context.AUDIO_SERVICE);
        audioManager.setBluetoothScoOn(false);
        audioManager.stopBluetoothSco();
        audioManager.setMode(AudioManager.MODE_NORMAL);
        audioManager.setSpeakerphoneOn(true); 

        _bluetoothHeadset.stopVoiceRecognition(_device);
        _bluetoothHeadset= null;
     }
  }
};

当我播放音频时......

_soundPool.play( _soundPoolMap.get( index ), streamVolume, streamVolume, 1, 0, speed );

......我什么都没听到 .

谢谢你的提示:-)

1 回答

  • 1

    我找到了以下解决方法:我打开默认的蓝牙设置,我可以配对蓝牙扬声器,而音频将自动发送到扬声器 .

    startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS));
    

相关问题