How to connect to a bluetooth a2dp device?

2019-05-05 14:25发布

问题:

I am trying to pair an android device running android 4.1 with a a2dp capable audio receiver. 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. 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.

Any help on how to connect to an a2dp device would be greatly appreciated. Thanks.

回答1:

A2DP is not done over RFCOMM, so you can't use the createRfcommSocket APIs. The data is transported directly over L2CAP streams with specified protocol mux IDs (one for control, another for streaming data).