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

时间:2017-11-17 06:50:56

标签: android bluetooth a2dp

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

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

1 个答案:

答案 0 :(得分:1)

最后我找到了

调用getProfileProxy以获取a2dp代理

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

listenr应该实现onA2DPProxyReceived。

然后将调用回调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();
    }
}

请参阅以下网站