如何在不取消配对的情况下按照设置断开蓝牙连接

时间:2019-12-11 02:49:46

标签: android bluetooth

我需要一种取消蓝牙连接的方法。例如,单击设置以断开连接,但不要取消配对

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。我的蓝牙设备是输入设备,BluetoothUtil.INPUT_DEVICE_PROFILE = 4;

    private void disconnectToDevice() {
        if (mConnectedDevice != null) {
            mBluetoothAdapter.getProfileProxy(mContext, new BluetoothProfile.ServiceListener() {
                @Override
                public void onServiceConnected(int profile, BluetoothProfile proxy) {
                    LogUtil.d("getProfileProxy --> onServiceConnected profile:" + profile + (proxy == null));
                    if (proxy != null) {
                        LogUtil.d(proxy.getClass().getName());
                        BluetoothUtil.disconnectedDevice(mConnectedDevice, proxy);
                    }
                }

                @Override
                public void onServiceDisconnected(int profile) {
                }
            }, BluetoothUtil.INPUT_DEVICE_PROFILE);
        }
    }
    public static boolean disconnectedDevice(BluetoothDevice device, BluetoothProfile profile) {
        try {
            Method method = profile.getClass().getMethod("disconnect", BluetoothDevice.class);
            method.invoke(profile, device);
            return true;
        } catch (Exception e) {
            LogUtil.d(e.getMessage());
        }
        return false;
    }