Is there a way, using the Android SDK, to programmatically connect to an already-paired Bluetooth device?
In other words: I can go into Settings -> Wireless & ne
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:
BluetoothAdapteradapter.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)
connect(BluetoothDevice) method on the proxy:Method connect = BluetoothA2dp.class.getDeclaredMethod("connect", BluetoothDevice.class);
BluetoothDevice:String deviceName = "My_Device_Name";
BluetoothDevice result = null;
Set devices = adapter.getBondedDevices();
if (devices != null) {
for (BluetoothDevice device : devices) {
if (deviceName.equals(device.getName())) {
result = device;
break;
}
}
}
connect() method:connect.invoke(proxy, result);
Which, at least for me, caused an immediate connection of the device.