配对编程后,Android的蓝牙连接设备自动 [英] 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
}
}
和它告诉我的对话框配对设备,并输入PIN码。
And it show me the dialog to pairing devices and enter the pin.
问题是,createBond函数始终返回true,它DOEN的等待,直到我进入引脚和与设备配对,所以不正确使用:
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我的code是总部设在以下螺纹的第一个答案: Android设备蓝牙配对
P.D My code is based in the first answer of the following thread: Android Device Bluetooth pairing
推荐答案
我找到了解决办法。
首先,我需要一个广播接收器
这样的:
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
方法(在问题中所述),并输入引脚, ACTION_BOND_STATE_CHANGED
将开火, device.getBondState()== BluetoothDevice.BOND_BONDED
将真
键,它会连接。
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的蓝牙连接设备自动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!