2

I was trying to pair my android with a BLE device. The Problem is when I call for the pairing request activity, The dialog box appeared. But when I entered my password , it is not being paired or onActivityResult is not being called. So what to do for pairing successfully ?

   private void startBluetoothPairing(BluetoothDevice device) {
    String ACTION_PAIRING_REQUEST = "android.bluetooth.device.action.PAIRING_REQUEST";
    Intent intent = new Intent(ACTION_PAIRING_REQUEST);
    String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE";
    intent.putExtra(EXTRA_DEVICE, device);
    String EXTRA_PAIRING_VARIANT = "android.bluetooth.device.extra.PAIRING_VARIANT";
    int PAIRING_VARIANT_PIN = 0;
    intent.putExtra(EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    ((Activity) appContext).startActivityForResult(intent,1);
   }

OnActivityResult is not being called.

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.v("TAG","Bluetooth Device!!");
    if (requestCode == 1) {
        if (resultCode == Activity.RESULT_OK) {
            BluetoothDevice bluetoothDevice = data.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Parcel parcel = Parcel.obtain();
            data.getParcelableExtra(BluetoothDevice.EXTRA_PAIRING_KEY).writeToParcel(parcel, 0);
            byte[] bytes = parcel.marshall();
            parcel.recycle();
            bluetoothDevice.setPin(bytes);
            bluetoothDevice.createBond();
        }
    }
}

Problem solved : Updated code :

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 Answer 1

1

Here is what i was using and it worked well for me.

Pair with the device

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
                    BluetoothActivity.this.registerReceiver(mReceiver, filter);
                    device.createBond();

BroadcastReceiver to check if device paired

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String name = device.getName();
                String address = device.getAddress();
                if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
                    //Device Paired
                }
            }
        }
    };

Unregister receiver in onDestroy

BluetoothActivity.this.unregisterReceiver(mReceiver);
Sign up to request clarification or add additional context in comments.

4 Comments

Is it pairing with passkey ?
@mahbubcseju Yes
But using your method, No dialog box is appeared for password. I need pairing with passkey . No automated way is needed.
@mahbubcseju I just checked and it works for me. Dialog box is shown for devices that take standard passkey(0000 or 1234). Sometimes instead of a dialog box, a notification is shown(for pairing between devices).It didn't ask for any passkey when i tried to pair with my bluetooth headphone but it got paired. Maybe your BLE device is a newer one that can pair without a passkey?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.