简体   繁体   English

如何连接到蓝牙a2dp设备?

[英]How to connect to a bluetooth a2dp device?

I am trying to pair an android device running android 4.1 with a a2dp capable audio receiver. 我正在尝试将运行android 4.1的android设备与具有a2dp功能的音频接收器配对。 I can do that without problems from the bluetooth settings screen but i am struggeling to do it in code. 我可以在蓝牙设置屏幕上做到这一点而没有问题,但是我正在努力用代码来做到这一点。

Basically i am able to discover the device but i can not connect to it via the socket. 基本上,我能够找到该设备,但无法通过套接字连接到该设备。 Maybe i am using the wrong UUID or maybe i should use the predefined android.bluetooth.BluetoothA2dp classes. 也许我使用了错误的UUID,或者我应该使用预定义的android.bluetooth.BluetoothA2dp类。 Here is what i am doing: 这是我在做什么:

UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
socket = device.createInsecureRfcommSocketToServiceRecord(uuid);
socket.connect();

But i get an exception stating that it can not connect. 但我得到一个例外,指出它无法连接。

java.io.IOException: Service discovery failed
at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:403)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:213)

I have also tried to connect using the UUIDs the device provides via device.getUuids() but those did not help either to connect to the a2dp device. 我也尝试过使用设备通过device.getUuids()提供的UUID进行连接,但这些都无助于连接到a2dp设备。

Any help on how to connect to an a2dp device would be greatly appreciated. 任何有关如何连接到a2dp设备的帮助将不胜感激。 Thanks. 谢谢。

A2DP is not done over RFCOMM, so you can't use the createRfcommSocket APIs. A2DP不能通过RFCOMM完成,因此您不能使用createRfcommSocket API。 The data is transported directly over L2CAP streams with specified protocol mux IDs (one for control, another for streaming data). 数据直接通过具有指定协议多路复用ID的L2CAP流进行传输(一个用于控制,另一个用于流式传输数据)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM