我试图通过我的android应用程序以编程方式配对BLE设备。所以首先我为PAIRING_REQUEST注册一个BroadcastReceiver。当调用device.createBond()时,BroadcastReciever被触发。当 BroadcastReciever 被触发时,我使用 setpin() 设置 key 。但问题是有时会出现配对请求对话框,有时不会出现配对框,配对是自动完成的。我希望它永远不会显示任何对话框,但它应该以编程方式与 key 配对。
有什么办法解决吗?
或者有什么办法可以满足我的期望吗? 提前致谢。
在应用程序启动期间注册broadCasterReciever
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
appContext.getApplicationContext().registerReceiver(broadCastReceiver,intentFilter);
广播接收器的实现。
private String BLE_PIN= "000012";
private BroadcastReceiver broadCastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action))
{
BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
bluetoothDevice.setPin(BLE_PIN.getBytes());
Log.e("TAG","Auto-entering pin: " + BLE_PIN);
}
}
};
发现设备后,我调用了device.createBond()。
最佳答案
在setPin()
之后调用abortBroadcast();
为我解决了这个问题。
关于android - 如何隐藏配对对话框以在 Android 上以编程方式配对 BLE 设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56626964/