会自动连接蓝牙设备

会自动连接蓝牙设备

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

问题描述

在我的应用中,我需要配对蓝牙设备并立即与其连接.

In my app I need pairing bluetooth device and immediately connect with it.

我有以下功能来配对设备:

I have the following function in order to pairing devices:

public boolean createBond(BluetoothDevice btDevice)
{
    try {
        Log.d("pairDevice()", "Start Pairing...");
        Method m = btDevice.getClass().getMethod("createBond", (Class[]) null);
        Boolean returnValue = (Boolean) m.invoke(btDevice, (Object[]) null);
        Log.d("pairDevice()", "Pairing finished.");
        return returnValue;

    } catch (Exception e) {
        Log.e("pairDevice()", e.getMessage());
    }
    return false;
}

我使用它的方式如下:

Boolean isBonded = false;
try {
    isBonded = createBond(bdDevice);
    if(isBonded)
    {
         //Connect with device
    }
}

它向我展示了配对设备和输入密码的对话框.

And it show me the dialog to pairing devices and enter the pin.

问题是 createBond 函数总是返回 true,它确实等到我输入 pin 并与设备配对,所以我没有正确使用:

The problem is that createBond functions always return true, and it doen's wait until I enter the pin and paired with device, so I don't use correctly:

isBonded = createBond(bdDevice);
if(isBonded) {...}

所以问题是我如何与设备配对以及何时配对连接到它?

So the question is How can I paired with device and when it is paired connect to it?

PD 我的代码基于以下线程的第一个答案:Android + 以编程方式通过蓝牙配对设备

P.D My code is based in the first answer of the following thread: Android + Pair devices via bluetooth programmatically

推荐答案

我找到了解决方案.

首先我需要一个 BroadcastReceiver 像:

First I need a BroadcastReceiver like:

private BroadcastReceiver myReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
                // CONNECT
            }
        } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // Discover new device
        }
    }
};

然后我需要按如下方式注册接收器:

And then I need register the receiver as follow:

IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
context.registerReceiver(myReceiver, intentFilter);

通过这种方式,接收器正在侦听 ACTION_FOUND(发现新设备)和 ACTION_BOND_STATE_CHANGED(设备更改其绑定状态),然后我检查新状态是否为 BOND_BOUNDED,如果是我与设备连接.

In this way the receiver is listening for ACTION_FOUND (Discover new device) and ACTION_BOND_STATE_CHANGED (Device change its bond state), then I check if the new state is BOND_BOUNDED and if it is I connect with device.

现在,当我调用 createBond 方法(在问题中描述)并输入 pin 时,ACTION_BOND_STATE_CHANGED 将触发并且 device.getBondState() == BluetoothDevice.BOND_BONDED 将是 True 并且它会连接.

Now when I call createBond Method (described in the question) and enter the pin, ACTION_BOND_STATE_CHANGED will fire and device.getBondState() == BluetoothDevice.BOND_BONDED will be True and it will connect.

这篇关于以编程方式配对后,Android 会自动连接蓝牙设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 12:42