Android:如何以编程方式配对蓝牙设备? [英] Android: How to pair bluetooth devices programmatically?

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

问题描述

请任何人帮助我以编程方式将我的安卓手机与其他发现的手机配对?

Please any one help me with pairing my android phone with other discovered phone programmatically ?

推荐答案

使用反射找到了解决方案,我现在这样做如下,它对我有用:

Found the solution using reflection, I am doing this now as follows and it is working for me:

//For Pairing
private void pairDevice(BluetoothDevice device) {
    try {
        Log.d("pairDevice()", "Start Pairing...");
        Method m = device.getClass().getMethod("createBond", (Class[]) null);
        m.invoke(device, (Object[]) null);
        Log.d("pairDevice()", "Pairing finished.");
    } catch (Exception e) {
        Log.e("pairDevice()", e.getMessage());
    }
}


//For UnPairing
   private void unpairDevice(BluetoothDevice device) {
    try {
        Log.d("unpairDevice()", "Start Un-Pairing...");
        Method m = device.getClass().getMethod("removeBond", (Class[]) null);
        m.invoke(device, (Object[]) null);
        Log.d("unpairDevice()", "Un-Pairing finished.");
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
}

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

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