简体   繁体   English

如何隐藏配对对话框以编程方式在Android上配对BLE设备?

[英]How to hide the pairing dialog box to programmatically pair a BLE device on Android?

I was trying to pair a BLE device programmatically from my android app.So at first I register a BroadcastReceiver for PAIRING_REQUEST.When device.createBond() is called ,the BroadcastReciever is triggered. 我试图通过Android应用程序以编程方式配对BLE设备。因此首先我为PAIRING_REQUEST注册了BroadcastReceiver。当device.createBond()被调用时,会触发BroadcastReciever。 When the BroadcastReciever is triggered, I set the passkey by using setpin(). 触发BroadcastReciever时,我使用setpin()设置了密码。 But the problem is pairing request dialog box appeared sometimes and sometimes without appearing pairing box pairing is done automatically . 但是问题是配对请求对话框有时会出现,有时没有出现,配对框会自动完成。 I want that it will never show any dialog box but it should be paired with the passkey programmatically. 我希望它永远不会显示任何对话框,但应以编程方式将其与密码配对。

Any solution of it ? 有什么解决办法吗?

Or is there any way to fulfill my expectation? 还是有什么办法可以满足我的期望? Thanks in advance. 提前致谢。

Registered broadCasterReciever during application launching 在应用程序启动期间注册了broadCasterReciever

    IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
    intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
    appContext.getApplicationContext().registerReceiver(broadCastReceiver,intentFilter);

Implementation of broadcastReciever. 实现broadcastReciever。

    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);

                }
           }
      };

And I called device.createBond() after discovering the device. 发现设备后,我调用了device.createBond()

calling abortBroadcast(); 调用abortBroadcast(); after setPin() solved the problem for me. setPin()为我解决了问题之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM