Bluetooth Pairing with Nrf UART is not working properly

后端 未结 1 947
花落未央
花落未央 2020-12-22 05:24

The Bluetooth pairing is not working properly. I am developing the Application based on Bluetooth pairing with UART. Here I have included my concept and Program.Help me out

相关标签:
1条回答
  • 2020-12-22 05:49

    You can bypass the native Bluetooth pairing process and pair with Bluetooth peripheral pro-grammatically. Try this:

    Register a receiver for BluetoothDevice.ACTION_PAIRING_REQUEST with the highest priority.

    private void notPaired(){
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
        filter.setPriority(SYSTEM_HIGH_PRIORITY-1);
        registerReceiver(mReceiver, filter);
        mDevice.createBound();// OR establish connection with the device and read characteristic for triggering the pairing process 
        getBoundState();
    }
    
    private final BroadcastReceiver mReceiver = new BroadcastReceiver()
    {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)){
                final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
    
                if(type == BluetoothDevice.PAIRING_VARIANT_PIN){
                    byte[] pin = "123456".getBytes();
                    device.setPin(pin);
                    Log.i("Pairing Process ", "Pairing Key Entered");
                    abortBroadcast();
                }else
                    Log.i("Pairing Process: ", "Unexected Pairing type");
            }
        }
    };
    

    To make sure that device is paired register a receiver for BluetoothDevice.ACTION_BOND_STATE_CHANGED

    private void getBoundState(){
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        registerReceiver(boundStateReciver, filter);
    }
    
    private final BroadcastReceiver boundStateReciver= new BroadcastReceiver()
    {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
                final int d = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,-1);
                switch(d){
                    case BluetoothDevice.BOND_BONDED:
                        Log.i("Pairing Process ", "Paired successfully");
                    break;
                }
            }
        }
    };
    

    In Manifests add this permission

    <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
    
    0 讨论(0)
提交回复
热议问题