Try changing the ADB connection timeout. I think it defaults that to 5000ms and I changed mine to 10000ms to get rid of that problem.
If you are in Eclipse, you can do this by going through
Window -> Preferences -> Android -> DDMS -> ADB Connection Timeout (ms)
Okay, since this was driving me crazy, I did some digging into the source code and I've found a 100% reliable (at least on my Nexus 4, Android 4.3) solution to connect to a paired A2DP device (such as a headset or Bluetooth audio device). I've published a fully working sample project (easily built with Android Studio) that you can find here on Github.
Essentially, what you need to do is:
- Get an instance of the
BluetoothAdapter
- Using this instance, get a profile proxy for A2DP:
adapter.getProfileProxy (context, listener, BluetoothProfile.A2DP);
where listener
is a ServiceListener
that will receive a BluetoothProfile
in its onServiceConnected()
callback (which can be cast to a BluetoothA2dp
instance)
- Use reflection to acquire the
connect(BluetoothDevice)
method on the proxy:
Method connect = BluetoothA2dp.class.getDeclaredMethod("connect", BluetoothDevice.class);
- Find your
BluetoothDevice
:
String deviceName = "My_Device_Name";
BluetoothDevice result = null;
Set<BluetoothDevice> devices = adapter.getBondedDevices();
if (devices != null) {
for (BluetoothDevice device : devices) {
if (deviceName.equals(device.getName())) {
result = device;
break;
}
}
}
- And invoke the
connect()
method:
connect.invoke(proxy, result);
Which, at least for me, caused an immediate connection of the device.
Best Answer
What is your Android device and Android version? If it's Android 4.2, they're now using Broadcom as I understood and so we're only able to create SDP connection.
I'm having the same problem while making a bluetooth connection between my Nexus 7 (Android 4.2.2 with CyanogenMod ROM 10) and a Wiimote. This is an HID device so I need to use L2CAP. Last versions of Android were able to create this connection (we can figure out just by looking at the market). If you search an application to handle this on the market, you'll see by looking at the description that all devices with Android version 4.0+ are not supported.
I just found this post few minutes ago which could help you: stackoverflow.com/a/7838587/1772805
Let me know if you solve this. I'll keep you in touch if I found anything.
EDIT #1: I tried the solution on the link above. I changed it to use a different constructor like this:
I succeeded to create the socket but when I call the method
connect()
, I get this error:bt l2cap socket type not supported, type:3
. This log is a very bad new for me because I found this thread which says Android 4.2 does not support L2CAP (or just disabled by Google..).Because my device is rooted with CyanogenMod 10, the feature will maybe come back on a new release. I hope..
EDIT #2: Here's a link pointing on the C file containing the reason of the problem: btif_sock.c. If anyone knows if it's possible re-write this file or how to add the L2CAP feature to Android with an external C library. I'm afraid it's not a simple task.