Pairing in BLE

Developing a secure mobile application

This implementation uses the Qualcomm® QCA4020/4024 Platform as an example.

When the QCA402x board starts advertising, the Home Automation Android Application is able to scan and discover nearby devices, then list them. Clicking on a listed device initiates a BLE connection to that device. For authentication, the Home Automation Android Application launches the Passkey pop-up. The user enters the 6-digit number to pair and (optionally) bond the devices. This is the best way to keep both ends of the communication link secure.

Passkey should also be running on the QCA4020 Development Board Application, so that the passkey provided in the Home Automation Android Application is validated by the BLE stack; that establishes a secure channel for data communication. Without the correct passkey, no connection will be established for communication between the devices.

The following code snippets define pairing. You may modify the code to suit your needs.

Turn On Pairing request

This code allows a pairing request in the Home Automation Android Application:

  BluetoothDevice mBluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  if (mBluetoothDevice != null) {

    try {
        byte[] pin = (byte[]) BluetoothDevice.class.getMethod("convertPinToBytes", String.class).invoke(BluetoothDevice.class, passkey);
        Method m = mBluetoothDevice.getClass().getMethod("setPin", byte[].class);
        m.invoke(mBluetoothDevice, pin);
    } catch (Exception e) {

        e.printStackTrace();

    }
}

Check for pairing state

This code checks the pairing state. The resulting state message will be in integer format; the state message will change accordingly.

  • BOND_BONDING: The Home Automation Android Application is trying to pair with the QCA402x board.
  • BOND_BONDED: The Home Automation Android Application is paired with the QCA402x board.
  • BOND_NONE: The Home Automation Android Application is not paired with the QCA402x board.
  int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
  
                 if (state < 0) {
                     Toast.makeText(context, "Please try again!", Toast.LENGTH_LONG).show();
                     //we should never get here
                 } else if (state == BluetoothDevice.BOND_BONDING) {
                     //bonding process is still working
                     //essentially this means that the Confirmation Dialog is still visible
  
                     Logger.d(TAG, "Start Pairing...");
  
  
                 }
                 else if (state == BluetoothDevice.BOND_BONDED) {
                     Logger.d(TAG, "Paired");
                     getService();
  
                 }
                 else if (state == BluetoothDevice.BOND_NONE) {
                     Logger.d(TAG, "not Paired");
  
                 }
             }

Once the devices are paired, the Home Automation Android Application can communicate data securely.