Automatically pairing 2 BlueTooth devices

时间:2018-10-24 11:23:20

标签: android android-bluetooth pairing

I know this question has already been asked but It does not work for me. I want to automatically pair 2 devices without having a pairing notification on my device. I followed the instruction here: How to pair Bluetooth device programmatically Android but it does not work. Indeed, I still have a pairing notification. Here is my code:

IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
    final BroadcastReceiver mReceiver2 = new BroadcastReceiver() {
        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);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    int pin=intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY", 0);
                    byte[]  pinBytes = Integer.toString(pin).getBytes();
                    device.setPin(pinBytes);
                    boolean b = device.createBond();
                }
            }
        }
    };
    registerReceiver(mReceiver2, filter2);

0 个答案:

没有答案