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

Zephyr/NCS BLE Central accepting fixed passkey

Hi,

I have successfully created a BT Peripheral which uses a static/fixed passkey upon connection with my phone (nrf connect mobile) or using the nrf52840 dongle. When I connect with the peripheral, I am prompted for the passkey on my phone or desktop and succedes to connect after writing the same "123456" passkey every time.

What I am struggling with, however, is how to connect to the peripheral with a Zephyr/NCS Central using the fixed passkey. Could you please aid me in getting this to work?

My Peripherals auth connection callback struct looks like:

static struct bt_conn_auth_cb auth_cb = {
	.cancel = auth_cancel,
	.passkey_display = auth_passkey_display,
	.pairing_confirm = NULL,
	.pairing_complete = pairing_complete,
	.pairing_failed = pairing_failed,
};

and my Centrals auth connection callback struct looks like:

static struct bt_conn_auth_cb conn_auth_callbacks = {
    .cancel = auth_cancel,
    .pairing_confirm = NULL,
	.passkey_confirm = passkey_confirm,
    .pairing_complete = pairing_complete,
    .pairing_failed = pairing_failed};

I though perhaps the Central would accept the Peripherals fixed passkey through:

static void passkey_confirm(struct bt_conn *conn, unsigned int passkey) {
	char addr[BT_ADDR_LE_STR_LEN];

  bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));

  int err;
  unsigned int fixed_passkey = 123456;
  if (passkey == fixed_passkey)
  {
    err = bt_conn_auth_passkey_confirm(conn);
    if (err)
    {
      printk("failed to confirm passkey, err: %d\n", err);
    }
  }
}

But that doesn't work. Any suggestions?

Thanks a lot in advance!

Best regards,

Jonas

  • Hi Jonas,

    Your code snippets look correct to me. Can you explain in what way it does not work (adding some logging may be useful)? Is passkey_confirm() called at all?

    Einar

  • Sorry for the late reply, Einar!

    Following is what happens on the Peripheral side:

    And the Centrals side:

    It fails when trying to deal with this custom service I've made. This works when no encryption is enforced, however I've now set it to:

    BT_GATT_SERVICE_DEFINE(project_service,
      BT_GATT_PRIMARY_SERVICE(&bt_uuid_project_service),
      BT_GATT_CHARACTERISTIC(&bt_uuid_project_characteristic1.uuid, BT_GATT_CHRC_READ,
                            BT_GATT_PERM_READ_ENCRYPT, read_cb1, NULL, NULL),
      BT_GATT_CHARACTERISTIC(&bt_uuid_project_characteristic2.uuid, BT_GATT_CHRC_READ, 
                            BT_GATT_PERM_READ_ENCRYPT, read_cb2, NULL, NULL),
      BT_GATT_CHARACTERISTIC(&bt_uuid_project_characteristic3.uuid, BT_GATT_CHRC_READ,
                            BT_GATT_PERM_READ_ENCRYPT, read_cb3, NULL, NULL),
      BT_GATT_CHARACTERISTIC(&bt_uuid_project_characteristic4.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
                            BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT, read_cb4, write_cb4, NULL),
      BT_GATT_CHARACTERISTIC(&bt_uuid_project_characteristic5.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
                            BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT, read_cb5, write_cb5, NULL),
    );

    and the Peripheral breaks down after fulfilling connected():

    static void connected(struct bt_conn *conn, uint8_t err)
    {
    if (err) {
    printk("Connection failed (err 0x%02x)\n", err);
    } else {
    printk("Connected\n");
    }
    
    int sec = bt_conn_get_security(conn);
    printk("security level is: %d\n", sec); //sec = 1 upon connection
    
    }

    To answer your other question, passkey_confirm() doesnt get invoked on the Central either. Just let me know if you'd want me to paste the complete code, I'd just have to rename some variables like I did in the service register function Slight smile

  • There is an error on the peripheral side here, so we need to solve that crash first. Can you start by finding which file/line causes problems by using arm-none-eabi-addr2line -e <project_folder>/build/zephyr/zephyr.elf 0x51308 ? What code do you have on that line? Please share some code amount it as well to provide context, or even better your whole project.

  • Hi Jonas,

    How can you create a bond with fixed passkey on peripheral device and phone ?

    I'm trying but it is not worked

Related