1. Are you ready for the Galaxy S20? Here is everything we know so far!

Bluetooth disconnect (via reflection) does not disconnect device

Discussion in 'Android Development' started by Robin Allen, Nov 25, 2021.

  1. Robin Allen

    Robin Allen Lurker
    Thread Starter

    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:


    Code (Java):
    1.  
    2. public void disconnectFromDevice(BluetoothDevice device) {
    3.     BluetoothProfile.ServiceListener serviceListener =
    4.             new BluetoothProfile.ServiceListener() {
    5.                 @Override
    6.                 public void onServiceDisconnected(int profile) {
    7.  
    8.                 }
    9.  
    10.                 public void onServiceConnected(int profile, BluetoothProfile proxy) {
    11.                     Method disconnect;
    12.  
    13.                     try {
    14.                         disconnect = a2dp.getClass()
    15.                                 .getMethod("disconnect", BluetoothDevice.class);
    16.                         disconnect.setAccessible(true);
    17.                         disconnect.invoke(proxy, device);
    18.                     } catch (NoSuchMethodException e) {
    19.                         e.printStackTrace();
    20.                     } catch (IllegalAccessException e) {
    21.                         e.printStackTrace();
    22.                     } catch (InvocationTargetException e) {
    23.                         e.printStackTrace();
    24.                     }
    25.                     BluetoothAdapter.getDefaultAdapter().closeProfileProxy(profile, proxy);
    26.                     bluetoothAdapter.disable();
    27.                     bluetoothAdapter.enable();
    28.                 }
    29.             };
    30.     BluetoothAdapter.getDefaultAdapter().getProfileProxy(context, serviceListener, BluetoothProfile.A2DP);
    31. }
    32.     }
     


    Helna Gomez likes this.

    1. Download the Forums for Android™ app!


      Download

       
Loading...

Share This Page

Loading...