【问题标题】:closeProfileProxy is not disconnecting my A2DP bluetooth connection, not sure whycloseProfileProxy 没有断开我的 A2DP 蓝牙连接,不知道为什么
【发布时间】:2022-01-03 15:30:32
【问题描述】:

我使用 a2dp 配置文件连接到蓝牙设备。该部分一切正常,但是当需要断开所有预期代码时,可以访问但设备没有断开连接,我不确定我做错了什么。这是我的断开连接代码:

public void disconnectFromOther(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.getDefaultAdapter().getProfileProxy(context, serviceListener, BluetoothProfile.A2DP);
}

编辑:我现在也尝试了 BluetoothGatt,但仍然无法断开我的设备。

【问题讨论】:

    标签: java android bluetooth android-bluetooth a2dp


    【解决方案1】:

    原来耳机的第二个配置文件 (BluetoothProfile.HEADSET) 也被打开了,我在 logcat 输出中发现了它。关闭该配置文件以及我的 a2dp 配置文件会关闭连接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-22
      • 2017-11-23
      • 1970-01-01
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      • 2012-10-09
      相关资源
      最近更新 更多