Check if connection is bonded/paired

This is my environment:

  • IDE: VSC
  • SDK: NCS v2.2.0
  • iPhone 14 running iOS 16.6 as Central
  • nRF52-DK as Peripheral

When the Central connects to the Peripheral, I want the Peripheral to check if it has pairing/bonding info stored for that connection, and if so, set a flag.  What's the best was with NCS to check if a connection is a paired/bonded connection?

Best regards,

Mike

Parents
  • Hi Mike

    Usually you have a pairing or bonding complete event of some kindwhen a bonding/pairing is done on the peripheral side, so you could just set a flag whenever this event is triggered, as that is the easiest way I can think of going about this.

    Best regards,

    Simon

  • Hi Simon,

    Thanks for your reply :-)

    I've got something working, but not sure if its the most robust approach.

    In my application, the Peripheral goes into Hibernate mode at the end of a connection, so I effectively lose any variables I may have set up when the connection was active.  Plus, I'm not sure how I would associate a flag to each different Central (I can have any number of Centrals connect to my Peripheral) that the Peripheral is bonded to.

    The sequence of events I'm trying to deal with is:

    1. Central connects to Peripheral for first time.  Connection isn't encrypted, but if it tries to write/read to any characteristics it triggers a pairing/bonding request, as they all have ENCRYPT permissions set

    2. Any subsequent connections, the pairing/bonding is remembered for that Central, and it can read/write to characteristics as required.

    I also need the Peripheral to grab the CTS from the Central, which in iOS it can only do if paired/bonded.  That's why I need to check the pairing/bonding status, so I can flag to my "get CTS code" that things are OK to proceed

    The way I am doing it at the moment is, once the connection has been made, I attempt to discover the CTS on the Central.  Once this is completed, I check for:

    if (bt_conn_get_security(cts_c.conn) >= BT_SECURITY_L2) {
    	bonded_to_client = true;
    }

    I'm only doing L2 security on my devices as neither have any input/display functionality.  If bonded_to_client is set to TRUE, I can then run my "get CTS code".

    Not sure if there is a better way to achieve my goal?

    Cheers,

    Mike

Reply
  • Hi Simon,

    Thanks for your reply :-)

    I've got something working, but not sure if its the most robust approach.

    In my application, the Peripheral goes into Hibernate mode at the end of a connection, so I effectively lose any variables I may have set up when the connection was active.  Plus, I'm not sure how I would associate a flag to each different Central (I can have any number of Centrals connect to my Peripheral) that the Peripheral is bonded to.

    The sequence of events I'm trying to deal with is:

    1. Central connects to Peripheral for first time.  Connection isn't encrypted, but if it tries to write/read to any characteristics it triggers a pairing/bonding request, as they all have ENCRYPT permissions set

    2. Any subsequent connections, the pairing/bonding is remembered for that Central, and it can read/write to characteristics as required.

    I also need the Peripheral to grab the CTS from the Central, which in iOS it can only do if paired/bonded.  That's why I need to check the pairing/bonding status, so I can flag to my "get CTS code" that things are OK to proceed

    The way I am doing it at the moment is, once the connection has been made, I attempt to discover the CTS on the Central.  Once this is completed, I check for:

    if (bt_conn_get_security(cts_c.conn) >= BT_SECURITY_L2) {
    	bonded_to_client = true;
    }

    I'm only doing L2 security on my devices as neither have any input/display functionality.  If bonded_to_client is set to TRUE, I can then run my "get CTS code".

    Not sure if there is a better way to achieve my goal?

    Cheers,

    Mike

Children
No Data
Related