This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Bluetooth Pairing with NrfUART in android

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 to fix the problem.

My Expected Result is If the user is press the Connect button. It should be pair without user input and Confirmation Screen for Pair Request and PIN. Finally The Device is Respond back to Connected.

My Actual Result is The Confirmation Screen and User Input Popup will open .After that the Device is Paired. Finally the Device is not responded back to I am connected.

I am Stuck in that Problem More than 2 days. Help me out of this Problem.

1. Register the PAIRING in onstart() method

          IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
         this.registerReceiver(mPairingRequestReceiver, filter);

2. BroadcastReceiver for Receive the PairingRequest.

  private BroadcastReceiver mPairingRequestReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
            try {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                int pin = intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY", 123456);
                //the pin in case you need to accept for an specific pin
                byte[] pinBytes;
                pinBytes = ("" + pin).getBytes("UTF-8");
                device.setPin(pinBytes);
    
         
        } catch (Exception e) {
                Log.e(TAG, "Error occurs when trying to auto pair");
                e.printStackTrace();
            }
        }
    }
};

After devices is connected I am creating the Bond

     @Override
     public void onDeviceConnected(BluetoothDevice device) {

        device.createBond();

      }
  • Hi,

    So there are two things:

    1. I would recommend you to base your project on BLE library, which handles or common patterns and problems. You'll find if here: github.com/.../Android-Ble-library In the Readme you'll find links to two projects that use this lib: very simple nRF Blinky and more complex nRF Toolbox.

    2. But still even when you'll use this lib the system popup to enter pin will be shown. The reason is that the broadcast that you want to receive (BluetoothDevice.ACTION_PAIRING_REQUEST) is sent by the system whenever a device wants to pair with pin. Every registered app may respond to it. Yours will provide the pin, but the system Bluetooth app will display a popup. Unfortunately you can't do anything about it, in my opinion, I wish I was mistaken.

    After you provide the pin with setPin(..) the device will bond and you should get ACTION_BOND_STATE_CHANGED broadcast with State bonded. You'll not get any connected event. When you'll use the BLE library, you'll get onBonded (or similar) in the Callbacks.

    Hope that helped, Aleksander

Related