以编程方式配对后,Android 会自动连接蓝牙设备

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21776712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:03:16  来源:igfitidea点击:

Android Connect Bluetooth device automatically after pairing programmatically

androidbluetooth

提问by RdlP

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;
}

And I use it as the following way:

我按以下方式使用它:

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.

它向我显示配对设备的对话框并输入引脚。

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:

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

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

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

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

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

PD 我的代码基于以下线程的第一个答案:Android + Pair devices via bluetooth programmatically

采纳答案by RdlP

I found the solution.

我找到了解决方案。

First I need a BroadcastReceiverlike:

首先我需要一个BroadcastReceiver喜欢:

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);

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_BOUNDEDand if it is I connect with device.

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

Now when I call createBondMethod (described in the question) and enter the pin, ACTION_BOND_STATE_CHANGEDwill fire and device.getBondState() == BluetoothDevice.BOND_BONDEDwill be Trueand it will connect.

现在,当我打电话createBond的方法(在问题中所述),并输入PIN码,ACTION_BOND_STATE_CHANGED将火和device.getBondState() == BluetoothDevice.BOND_BONDEDTrue,它会连接。