How to pair Bluetooth device programmatically Android How to pair Bluetooth device programmatically Android android android

How to pair Bluetooth device programmatically Android


How can I avoid or dismiss Android's Bluetooth pairing notification when I am doing programmatic pairing?

This seems to give you the answer, with the pin entering and all. It involves sending .setPin() whenever you get the message.


So, I had this question, if someone needs the answer to this working in android 4.4.2.

 IntentFilter filter = new IntentFilter(                "android.bluetooth.device.action.PAIRING_REQUEST");        /*         * Registering a new BTBroadcast receiver from the Main Activity context         * with pairing request event         */        registerReceiver(                new PairingRequest(), filter);

And the code for the Receiver.

  public static class PairingRequest extends BroadcastReceiver {        public PairingRequest() {            super();        }        @Override        public void onReceive(Context context, Intent intent) {            if (intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")) {                try {                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);                    int pin=intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY", 0);                    //the pin in case you need to accept for an specific pin                    Log.d("PIN", " " + intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY",0));                    //maybe you look for a name or address                    Log.d("Bonded", device.getName());                    byte[] pinBytes;                    pinBytes = (""+pin).getBytes("UTF-8");                    device.setPin(pinBytes);                    //setPairing confirmation if neeeded                    device.setPairingConfirmation(true);                } catch (Exception e) {                    e.printStackTrace();                }            }        }    }

And in the manifest file.

<uses-permission android:name="android.permission.BLUETOOTH" /><uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

And the broadcastReceiver.

 <receiver android:name=".MainActivity$PairingRequest">                <intent-filter>                    <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />                    <action android:name="android.bluetooth.device.action.PAIRING_CANCEL" />                </intent-filter></receiver>


How to set the pin code has been answered above (and that helped me). Yet, I share my simple code below which works with Android 6:

BluetoothAdapter mBTA = BluetoothAdapter.getDefaultAdapter();if (mBTA.isDiscovering()) mBTA.cancelDiscovery();mBTA.startDiscovery();.../** In a broadcast receiver: */if (BluetoothDevice.ACTION_FOUND.equals(action)) { // One device found.    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);    Log.d(TAG, "Start Pairing... with: " + device.getName());    device.createBond();}// If you want to auto-input the pin#:else if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)){                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);                    device.setPin("1234".getBytes());}