• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Bluetooth disconnect (via reflection) does not disconnect device

Robin Allen

Lurker
Nov 25, 2021
1
1
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);
}
    }
 
  • Like
Reactions: Helna Gomez

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones