在我们的应用程序中,我想使用Android v4.2或更高版本连接到之前配对的A2DP蓝牙扬声器并直接播放音频 .
我可以使用此代码成功创建A2DP配置文件对象以启动该过程:
/* Manifest permissions */
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
// Get the default adapter
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// Establish connection to the proxy.
mBluetoothAdapter.getProfileProxy(this, mProfileListener, BluetoothProfile.A2DP)
并且以下监听器响应连接:
private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == BluetoothProfile.A2DP) {
mBluetoothSpeaker = (BluetoothA2dp) proxy;
// no devices are connected
List<BluetoothDevice> connectedDevices = mBluetoothSpeaker.getConnectedDevices();
//the one paired (and disconnected) speaker is returned here
int[] statesToCheck = {BluetoothA2dp.STATE_DISCONNECTED};
List<BluetoothDevice> disconnectedDevices = mBluetoothSpeaker.getDevicesMatchingConnectionStates(statesToCheck);
BluetoothDevice btSpeaker = disconnectedDevices.get(0);
//WHAT NOW?
}
}
public void onServiceDisconnected(int profile) {
if (profile == BluetoothProfile.A2DP) {
mBluetoothSpeaker = null;
}
}
};
我已尝试使用以下代码连接到设备as detailed in the Android docs,但最终 BluetoothSpeaker.getConnectedDevices()
调用不返回任何已连接的设备 .
BluetoothSocket tmp = null;
UUID MY_UUID = UUID.fromString("00001108-0000-1000-8000-00805f9b34fb");
try {
tmp = btSpeaker.createInsecureRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e1) {
// TODO Auto-generated catch block
Log.d("createRfcommSocketToServiceRecord ERROR", e1.getMessage());
}
mmSocket = tmp;
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
Log.d("connectException", connectException.getMessage());
mmSocket.close();
} catch (IOException closeException) { }
return;
}
connectedDevices = mBluetoothSpeaker.getConnectedDevices();
代码似乎确实以某种方式连接到设备,因为当我停止执行时,蓝牙扬声器宣布它已准备好配对(就像它从音频源断开时一样) .
旧版本的 BluetoothA2dp
似乎有一个 connect(BluetoothDevice device)
方法,但现在已被删除(截至4.2),我很难找到任何明确的例子,说明如何以编程方式连接到A2DP设备,并将音频输出定向到它 . 我们将非常感激地提供任何关于如何接近任何一方的帮助 .
任何有关如何处理此问题的建议都将受到极大的赞赏 .
3 回答
这个对我有用 . 收到BluetoothA2dp.STATE_CONNECTED后,您可以正常播放音乐 .
}
特德
当我尝试使用BluetoothHeadset时,我遇到了和你一样的问题 . 我想我的工作可能适用于A2DP . 由于我的耳机仅支持免提配置文件 . 我只用BlueHeadset测试 .
无需 Build RFComm Channels .
为了我 . 连接到BluetoothHeadsetService后,1 . 检查音频是否已连接
2.如果没有, Build 音频连接
3.为BluetoothHeadset注册BroadcastReceiver.ACTION_CONNECTION_STATE_CHANGED和BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED
有同样的问题,但发现老帖子:
Programmatically connect to paired Bluetooth device
简而言之,为了连接到配对的a2dp设备,你只需要调用BluetoothA2dp.connect(myPairedA2dpDevice),但是现在该方法对公共API是隐藏的,这是没有用的 . 所以你通过Java反射访问它 . 这是一种黑客行为,但谷歌的方式,现在似乎没有一个干净的解决方案 .