在最新API(Lollipop)上使用PIN与BLE设备绑定时避免用户对话

Kix*_*ixX 10 android bluetooth-lowenergy android-4.4-kitkat android-5.0-lollipop

我正在通过Android应用程序与蓝牙低功耗设备绑定/配对.启动绑定时,会显示"输入PIN" - 对话框.当我从代码设置引脚时它会自动消失.对话框出现并消失得足够慢以使用户感到困惑和烦恼.我怎么能避免这个对话?

我在Android BLE指南中找不到任何关于绑定的内容https://developer.android.com/guide/topics/connectivity/bluetooth-le.html

我得到了另一个问题的帮助,但解决方案并没有删除对话框. 在进行程序化配对时,如何避免或取消Android的蓝牙配对通知?

这个问题建议删除SDK中的接收器. Android防止蓝牙配对对话框

在API中真的没办法解决这个问题吗?关于这个问题的大多数问题已经有几年了,最近Android蓝牙API已经发生了很多变化.我目前正在使用API​​ 19(4.4 Kitkat)但我会使用API​​ 22(5.1 Lollipop),如果这会有所帮助.

这就是我如何进行粘合:

myBluetoothDevice.createBond();
Run Code Online (Sandbox Code Playgroud)

然后,我听取配对意图,以便能够在合适的时刻提供引脚.我也听取了绑定意图.

//Register before calling createBond
context.registerReceiver(broadcastReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED));
context.registerReceiver(broadcastReceiver, new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST));

//The recievers
final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();        

    if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action))
    {
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        byte[] pin = convertPinToBytes("111111"); //convertPinToBytes for some reason not available in API, so I made a copy
        device.setPin(pin);
    }

    if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
        if (BluetoothDevice.BOND_BONDED == device.getBondState())
        {
            //Success!
        }
    }
}
Run Code Online (Sandbox Code Playgroud)