[英]Android Connect Bluetooth device automatically after pairing programmatically
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,并且一直等到我输入引脚并与设备配对后,所以我使用不正确:
isBonded = createBond(bdDevice);
if(isBonded) {...}
So the question is How can I paired with device and when it is paired connect to it? 因此,问题是如何与设备配对以及何时配对连接到设备?
PD My code is based in the first answer of the following thread: Android + Pair devices via bluetooth programmatically PD我的代码基于以下线程的第一个答案: Android +以编程方式通过蓝牙配对设备
I found the solution. 我找到了解决方案。
First I need a BroadcastReceiver
like: 首先,我需要一个
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_BOUNDED
and if it is I connect with device. 通过这种方式,接收
ACTION_FOUND
在监听ACTION_FOUND
(发现新设备)和ACTION_BOND_STATE_CHANGED
(设备更改其绑定状态),然后检查新状态是否为BOND_BOUNDED
以及是否与设备连接。
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. 现在,当我调用
createBond
方法(在问题中描述)并输入引脚时, ACTION_BOND_STATE_CHANGED
将触发,并且device.getBondState() == BluetoothDevice.BOND_BONDED
将为True
并进行连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.