如何在 android 上以编程方式取消配对或删除配对的蓝牙设备? [英] How to unpair or delete paired bluetooth device programmatically on android?

查看:68
本文介绍了如何在 android 上以编程方式取消配对或删除配对的蓝牙设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

项目是用我的安卓手机连接我的arduino设备.但是我怎样才能取消配对的配对.我看到配对列表似乎存储在蓝牙适配器可以随时检索的位置.

The project is to use my android phone to connect with my arduino devices. but how can I unpair the paired ones. I see it seems the paired list is stored where bluetoothadapter could retrieve anytime.

附注:第一,我知道长按配对设备会取消配对.
但这里的问题是如何以编程方式实现这一点?

PS: 1st, I know long press paired device will unpair it.
but the question here is how can I make this happen programmatically?

2nd,我检查了 bluetoothdevice 和 bluetoothAdapter 类,没有实现这个的函数.

2nd, I have checked bluetoothdevice and bluetoothAdapter class, there is no function to implement this.

谢谢.

推荐答案

此代码适用于我.

private void pairDevice(BluetoothDevice device) {
    try {
        if (D)
            Log.d(TAG, "Start Pairing...");

        waitingForBonding = true;

        Method m = device.getClass()
            .getMethod("createBond", (Class[]) null);
        m.invoke(device, (Object[]) null);

        if (D)
            Log.d(TAG, "Pairing finished.");
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
}

private void unpairDevice(BluetoothDevice device) {
    try {
        Method m = device.getClass()
            .getMethod("removeBond", (Class[]) null);
        m.invoke(device, (Object[]) null);
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
}

这篇关于如何在 android 上以编程方式取消配对或删除配对的蓝牙设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆