Bonded Pairing disconnect reason 19 (0x13) after reset

I have an existing production application and I am trying to enable "just works" bonded pairing. I am using nRF Connect version 2.3.0.

The steps I have taken so far:

1. I have updated my main service (the one also in the advertising packet) to have encrypted characteristics like so: 

BT_GATT_SERVICE_DEFINE(history_service,
                       BT_GATT_PRIMARY_SERVICE(BT_UUID_HISTORY_SERVICE),
                       BT_GATT_CHARACTERISTIC(BT_UUID_HISTORY_SERVICE_DATAPOINT,
                                              BT_GATT_CHRC_WRITE | BT_GATT_CHRC_NOTIFY | BT_GATT_CHRC_READ,
                                              BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT,
                                              read_history_cb_fn, on_receive_datapoint, history_datapoint_buf),
                       BT_GATT_CCC(on_cccd_changed,
                                   BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT),

2. I added CONFIG_BT_SMP=y to my prj.conf file

3. I included the security_changed parameter to my bt_conn_cb struct:

static void security_changed(struct bt_conn *conn, bt_security_t level,
							 enum bt_security_err err)
{
	char addr[BT_ADDR_LE_STR_LEN];

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

	if (!err)
	{
		printk("Security changed: level %u\n", level);
	}
	else
	{
		printk("Security failed: level %u err %d\n", level, err);
	}
}

static struct bt_conn_cb conn_callbacks = {
	.connected = connected,
	.disconnected = disconnected,
	.security_changed = security_changed,
	.le_param_req = le_param_req,
	.le_param_updated = le_param_updated,
};

4. I configured the following additional callbacks (I am going for "just works" bonding and do not have any interface on the sensor itself so I cannot verify a passkey or press a button):

static void auth_cancel(struct bt_conn *conn)
{
	char addr[BT_ADDR_LE_STR_LEN];

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

	printk("Pairing cancelled\n");
}

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

static void pairing_complete(struct bt_conn *conn, bool bonded)
{
	char addr[BT_ADDR_LE_STR_LEN];

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

	printk("Pairing completed: %s, bonded: %d\n", addr, bonded);
}

static void pairing_failed(struct bt_conn *conn, enum bt_security_err reason)
{
	char addr[BT_ADDR_LE_STR_LEN];

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

	printk("Pairing failed conn: %s, reason %d\n", addr, reason);
}

static struct bt_conn_auth_info_cb conn_auth_info_callbacks = {
	.pairing_complete = pairing_complete,
	.pairing_failed = pairing_failed};

My bluetooth setup function is as follows:

	bt_conn_cb_register(&conn_callbacks);
	err = bt_conn_auth_cb_register(&conn_auth_callbacks);
	err = bt_conn_auth_info_cb_register(&conn_auth_info_callbacks);

	if (err)
	{
		printk("Failed to register authorization callbacks.\n");
		return;
	}

After all of these steps, when I first connect to the sensor, my phone (iOS) asks me if I would like to pair. I choose "pair". But then if I reset my sensor and try to connect to the sensor again with my phone, it disconnects right away with reason 19:

Connection established!
                Connected to: 74:8F:43:4A:8B:D3 (random)
                Role: 1
                Connection interval: 24
                Slave latency: 0
                Connection supervisory timeout: 72
Disconnected: 19

What am I missing? What does disconnect reason 19 mean?

  • Hi

    Disconnect reason 19 means that the peer device (device on the other end) initiated the disconnection. So you should also see a more specific disconnect reason on the other side. If you only see this after the devices are paired and one of them are "reset", that's likely because the bonding information is erased in one device while the other tries to reconnect with the "old" bonding information. If that's the case, you need to erase bonding information on the iOS side as well before reconnecting and then pair again.

    Best regards,

    Simon

  • Thanks Simon, however I'm connecting using the bluetooth menu in the iOS settings. I'm testing without any 3rd party apps, just using the system bluetooth connection. So is it possible the sensor is losing the bonding information? 

    I have a bootloader, and NVS user settings configured, does enabling bonded pairing need any special memory offset or something for the bond information to be stored in the flash on the sensor?

  • Hi Eric

    But then if I reset my sensor and try to connect to the sensor again

    If this "reset" means that you delete bonding information then that is possible, yes. Depending on the sample you've used for development, it could be that the pairing information is either deleted upon a disconnect or upon start up after a reset. This is usually done with the bt_unpair() function in the nRF Connect SDK, so you can check if it is called at all in your application.

    Best regards,

    Simon

  • I can confirm it is not called in my application. Is it possible that I need to configure something specific for the pairing information to be stored in non-volatile memory?

  • Bonding information will be stored in the flash (non-volatile) memory by default. Can you explain what you mean by "I reset my sensor" in your initial question? If "resetting" is to reprogram it or erase flash data, then that's likely why you get this error, as that will also erase the bonding information.

    Are you not able to see any kind of error message on the iPhone side? If you use the nRF Connect app you should be able to see some logging information from the phone side as well, which is the side that initiates the disconnection.

    Best regards,

    Simon

Related