What is the lifespan of the connection object struct bt_conn?

A peripheral advertises to a central and a connection is established as the peripheral's callback function is executed with parameter struct bt_conn *conn.

  • Will this particular connection object remain the same through disconnects and reconnections of the central?
  • Can I save the address of this object as an identifier of this particular connection and compare it with the conn parameter passed to connect and disconnect callbacks?
  • What impact does a call to bt_conn_ref() or bt_conn_unref() have on how this works?

Thanks,

Ken

  • Hi,

    Will this particular connection object remain the same through disconnects and reconnections of the central?
    What impact does a call to bt_conn_ref() or bt_conn_unref() have on how this works?

    The connection object is valid as long as you called bt_conn_ref() and until bt_conn_unref() is called, even if the connection is terminated before. See Connection management. It cannot be used for much after the connection is temrinated though, but the information will still be valid.

    Can I save the address of this object as an identifier of this particular connection and compare it with the conn parameter passed to connect and disconnect callbacks?

    You can store the peer address and compare with the address in future connections. However, in some cases the same devicmay connect with a different address if using privacy (random resolvable address), which for instance all modern phones do.

    If you want to keep track of a device, it often makes sense to bond with it. If it use privacy, the IRK (identity resolution key) is exchagned so that you can identify it even as the address change.

  • Hi Einar,

    Thanks for the answer, but, my second question was not clear.  I was talking about the actual address of the bt_conn object within my program.  My testing observation is that with or without calling bt_conn_ref(), the bt_conn object passed as parameter conn in subsequent disconnect or (re) connect callbacks will not necessarily be at the same program address as the initial connection bt_conn object.  Is this correct?

  • Hi,

    Ah, I see. Yes, that is correct. When a new connection is established, a bt_conn object is created and you should not make any assumption on where this is, but use the pointer you get (and call bt_conn_ref() to make sure it stays valid until you unref it with bt_conn_unref() when it is no longer needed).

Related