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

Viewed 1416

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. When the BroadcastReciever is triggered, I set the passkey by using 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

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

Implementation of 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.

1 Answers

calling abortBroadcast(); after setPin() solved the problem for me.

Related