从我的Android应用程序,我想将音频流式传输到A2DP扬声器,docking扬声器或car扬声器等任何可用的扬声器.
为此,我想使用蓝牙连接并通过蓝牙连接流...
我看到以下链接
setBluetoothA2dpOn(boolean on)
Android 2.3:如何从SCO切换到A2DP以获得功能强大的蓝牙扬声器?
但到处都是我发现的相同..即
public void setBluetoothA2dpOn (boolean on)
This method is deprecated.
Do not use.
Run Code Online (Sandbox Code Playgroud)
请指导我如何做到这一点..或建议是否可以使用其他方式完成..任何帮助/建议表示赞赏.
编辑:
清单中的许可:
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
Run Code Online (Sandbox Code Playgroud)
代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (RelativeLayout) findViewById(R.id.layout);
text = (TextView) findViewById(R.id.editText1);
scoSwitch = (ToggleButton) findViewById(R.id.switch1);
try {
mp1 = MediaPlayer.create(this, R.raw.jc_cm);
mp2 = MediaPlayer.create(this, R.raw.rp);
amanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//amanager.setBluetoothA2dpOn(true);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block …Run Code Online (Sandbox Code Playgroud) 我想获得蓝牙连接设备列表......而不仅仅是配对设备.
我BluetoothHeadset在API级别11中找到了API,它提供getConnectedDevices()了获取连接蓝牙设备列表的方法.
如何使用此API获取蓝牙连接设备列表?
我需要使用具有语音识别功能的蓝牙耳机,这在S3,S4和Samsung Grand等设备上几乎可以正常使用.但是,当我在Nexus 7上尝试相同时,我得到了
BluetoothHeadsetServiceJni : Failed to start voice recognition, status: 6
Run Code Online (Sandbox Code Playgroud)
我在调用startVoiceRecognition()方法时出错.我正在使用此SO页面中的代码.
仅在某些设备中出现此问题的可能原因是什么?有什么方法可以解决这个问题吗?
我正在尝试从蓝牙耳机录制音频,startBluetoothSco()在不同版本的android中工作不同,在android 4.2,4.4和5.0上录制来自蓝牙耳机的音频.使用" 诺基亚BH-310和9xxPlantronics "蓝牙耳机.
SAMPLE_RATE = 8000;
AudioSource.DEFAULT
mRecorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, SAMPLE_RATE,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,
bufferSize);
Run Code Online (Sandbox Code Playgroud)
AudioSource.DEFAULT
mRecorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, SAMPLE_RATE,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,
bufferSize);
Run Code Online (Sandbox Code Playgroud)
要么
AudioSource.MIC
mRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,
bufferSize);
Run Code Online (Sandbox Code Playgroud)
AudioSource.VOICE_COMMUNICATION
mRecorder = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION, SAMPLE_RATE,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,
bufferSize);
//with other AudioSource types (MIC, DEFAULT) sco returns connected state but record from phone mic
Run Code Online (Sandbox Code Playgroud)
LOG for Android 5.0设备连接状态
D/inside onRcv? state=0
D/State=? conn -> 0
D/inside onRcv? state=2 …Run Code Online (Sandbox Code Playgroud) 我写了一些用于检测蓝牙耳机连接并通过耳机启动音频的代码.对于API 11及更高版本,可以在连接耳机时调用startVoiceRecognition.所以有几个用例如下:
应用程序启动前耳机已打开应用
程序应检查启动时连接的耳机并建立音频连接.
用户在应用程序
的生命周期内打开耳机应用程序应注册耳机连接状态的广播并在接收连接状态时启动音频连接.
第二个用例存在问题.当收到连接状态时,我调用startVoiceRecognition,但它总是返回false.所以我必须实现一个计时器,大约一秒后,调用将返回true.我想操作系统和耳机需要一段时间才能让一切准备就绪.有没有人知道如何在没有实现计时器的情况下获得耳机音频连接.如果不可能,应该是应该处理这种情况的操作系统(例如READY_FOR_AUDIO_CONNECTION广播)而不是应用程序吗?
以下是API 11或更高版本的完整工作代码.
清单权限
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
Run Code Online (Sandbox Code Playgroud)
码
public class MainActivity extends Activity
{
protected TextView mInfoTextview;
protected BluetoothAdapter mBluetoothAdapter;
protected BluetoothHeadset mBluetoothHeadset;
protected BluetoothDevice mConnectedHeadset;
protected AudioManager mAudioManager;
private static final String TAG = "Bluetooth Headset"; //$NON-NLS-1$
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInfoTextview = (TextView) findViewById(R.id.main_textview);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter != null)
{
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (mAudioManager.isBluetoothScoAvailableOffCall())
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) …Run Code Online (Sandbox Code Playgroud) 有没有办法在免提配置文件(HFP)模式下以编程方式配对和连接到远程设备?
到目前为止,我只能通过代码以正常方式连接到远程设备.我需要在HFP配置文件模式下连接到远程设备.
我试图通过SCO发送应用程序的所有音频.
我能够成功发送音频,
但是当有来电时,我需要断开SCO表格,以便应用音频不会干扰通话,
问题是,当我尝试在通话后将音频重新路由到SCO时,它不起作用.
这是我用来将音频发送到SCO的代码:
public class BluetoothManager {
// For Bluetooth connectvity
private static String TAG = "BluetoothManager";
private static BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private static AudioManager aM;
/**
* Set the audio manager of the device.
* @param c: The context this method is called from
*/
public static void setAudioManager(Context c) {
aM = (android.media.AudioManager)c.getSystemService(Context.AUDIO_SERVICE);
}
/**
* Check if a Bluetooth headset is connected. If so, route audio to Bluetooth SCO.
*/
private static void initializeAudioMode(Context …Run Code Online (Sandbox Code Playgroud) 我正在开发一个应用程序,可以通过蓝牙 SCO 播放 TextToSpeech 音频。连接到目标蓝牙设备(汽车立体声),然后指示 TextToSpeech 引擎说话后,即使我在通过 SCO 连接和播放 TextToSpeech 之间等待了几秒钟,也需要大约 15 秒才能开始播放音频。
这是我用来通过 SCO 连接的代码:
AudioManager audioM = (AudioManager) getApplicationContext().getSystemService(getApplicationContext().AUDIO_SERVICE);
audioM.setMode(audioM.MODE_IN_COMMUNICATION);
audioM.setBluetoothScoOn(true);
audioM.startBluetoothSco();
audioM.setSpeakerphoneOn(false);
Run Code Online (Sandbox Code Playgroud)
这是我用来播放 TextToSpeech 的代码:
String text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.";
HashMap<String, String> ttsParams = new HashMap<String, String>();
ttsParams.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_VOICE_CALL));
mTts.speak(text, TextToSpeech.QUEUE_FLUSH, ttsParams);
Run Code Online (Sandbox Code Playgroud)
其他 Android 应用程序(包括 VOIP 和内置电话应用程序)不会受到此延迟的影响。我创建的一个等效的 iOS 应用程序没有延迟。所以我知道问题不在于立体声。
任何帮助将不胜感激,谢谢