Android 如何断开A2DP配置文件蓝牙连接?

Android 如何断开A2DP配置文件蓝牙连接?,android,bluetooth,a2dp,Android,Bluetooth,A2dp,我正在使用耳机和其他蓝牙设备进行A2DP连接,但当我连接蓝牙设备时,却断开了连接,但它没有断开连接。我的代码是: try { Method connect = mA2dpService.getClass().getDeclaredMethod("disconnect", BluetoothDevice.class); connect.invoke(device); } catch (Exception e) { e.printStackTrace(); } 最

我正在使用耳机和其他蓝牙设备进行A2DP连接,但当我连接蓝牙设备时,却断开了连接,但它没有断开连接。我的代码是:

try {    
    Method connect = mA2dpService.getClass().getDeclaredMethod("disconnect", BluetoothDevice.class);
    connect.invoke(device);
} catch (Exception e) {
     e.printStackTrace();
}
最后我发现

调用getProfileProxy以获取a2dp代理

adapter.getProfileProxy(c, listner, BluetoothProfile.A2DP);
listenr应实现Ona2dproxyReceived

然后将调用OnA2DProxyReceived的回调

public void onA2DPProxyReceived (BluetoothA2dp proxy) {
    Method disconnect = null;
    try {
        disconnect = BluetoothA2dp.class.getDeclaredMethod("disconnect", BluetoothDevice.class);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
    BluetoothDevice device = findBondedDeviceByName(mBtAdapter, myDevice);
    disconnect.setAccessible(true);
    try {
        int result = disconnect.invoke(proxy,device);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
}
请参阅下面的网站

  • getDeclaredMethod:
  • 援引:

您解决了这个问题吗?