编程粘接在Android设备BLE [英] Programmatically bonding to BLE device on Android
问题描述
我写一个Android应用程序中,我想以编程方式结合到一个自定义的BLE装置。我有手工焊接工作中,用户输入使用标准的Android蓝牙配对对话框PIN码,但我一直没能找到有关如何自动编程债券A BLE设备的任何信息,无需用户干预。那可能吗?如果是这样,有什么程序?
I'm writing an Android application in which I'd like to programmatically bond to a custom BLE device. I have the manual bonding working in which the user enters the PIN using the standard Android Bluetooth pairing dialog, but I have not been able to find any information on how to automatically bond a BLE device programatically, without user intervention. Is that possible? If so, what's the process?
推荐答案
我可以通过注册一个BroadcastReceiver接收BluetoothDevice.ACTION_BOND_STATE_CHANGED意图,然后在接收后BluetoothDevice类调用BluetoothDevice.setPin,使这项工作的大部分时间。 BOND_BONDING消息。如同在Android中最BLE的东西的情况下,这似乎行事略有不同取决于设备和Android版本上。不幸的是,我似乎无法从又要接受蓝牙意图阻止Android的,所以在完成粘合前的PIN码输入屏幕仍然会弹出一秒钟。
I was able to make this work MOST OF THE TIME by registering a BroadcastReceiver to receive the BluetoothDevice.ACTION_BOND_STATE_CHANGED intent and then calling BluetoothDevice.setPin after receiving the BluetoothDevice.BOND_BONDING message. As is the case with most BLE things in Android, this seems to act slightly differently depending on the device and Android version. Unfortunately, I can't seem to stop Android from also receiving the bluetooth intent, so the PIN entry screen still pops up for a second before the bonding is completed.
private final BroadcastReceiver mReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
final String action = intent.getAction();
Logger("Broadcast Receiver:" + action);
if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED))
{
final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
if(state == BluetoothDevice.BOND_BONDING)
{
Logger("Bonding...");
if (mDevice != null) {
mDevice.setPin(BONDING_CODE.getBytes());
Logger("Setting bonding code = " + BONDING_CODE);
}
}
else if(state == BluetoothDevice.BOND_BONDED)
{
Logger("Bonded!!!");
mOwner.unregisterReceiver(mReceiver);
}
else if(state == BluetoothDevice.BOND_NONE)
{
Logger("Not Bonded");
}
}
}
};
这篇关于编程粘接在Android设备BLE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!