Robin Allen
Lurker
I'm building a custom bluetooth connect/disconnect app and so far I am able to connect my device using the a2dp profile, however when I try to trigger a disconnect, it runs through the disconnect method just fine but the device remains connected. I've tried everything I could find on the web and none of it has worked. The connection is using the a2dp profile. Below is my disconnect method:
Java:
public void disconnectFromDevice(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.disable();
bluetoothAdapter.enable();
}
};
BluetoothAdapter.getDefaultAdapter().getProfileProxy(context, serviceListener, BluetoothProfile.A2DP);
}
}