I am connected to a bluetooth device using the a2dp profile. That part is all working fine, but when it comes time to disconnect all the expected code is accessed but the device does not disconnect and I'm not sure what I'm doing wrong. This is my disconnect code:
public void disconnectFromOther(BluetoothDevice device) {
BluetoothProfile.ServiceListener serviceListener =
new BluetoothProfile.ServiceListener() {
@Override
public void onServiceDisconnected(int profile) {
}
public void onServiceConnected(int profile, BluetoothProfile proxy) {
Method disconnect;
try {
disconnect = a2dp.getClass()
.getMethod("disconnect", BluetoothDevice.class);
disconnect.setAccessible(true);
disconnect.invoke(proxy, device);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
BluetoothAdapter.getDefaultAdapter().closeProfileProxy(profile, proxy);
}
};
BluetoothAdapter.getDefaultAdapter().getProfileProxy(context, serviceListener, BluetoothProfile.A2DP);
}
Edit: I have now also tried BluetoothGatt and I still can't disconnect my device.
Turns out that a second profile for Headset (BluetoothProfile.HEADSET) was also being opened and I spotted it in the logcat output. Closing that profile along with my a2dp profile closed the connection.