2

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

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

1 回答 1

2

最后我发现

调用 getProfileProxy 获取 a2dp 代理

adapter.getProfileProxy(c, listner, BluetoothProfile.A2DP);

监听器应该实现 onA2DPProxyReceived。

然后会调用 Callback onA2DPProxyReceived。

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();
    }
}

参考以下网站

于 2018-03-02T07:39:55.877 回答