We're attempting to get a Bluetooth data stream set up on our nRF52840. We've taken Zephyr's peripheral sample and pared it down somewhat to only allow indications from the board to a phone, a Samsung Galaxy S6 running nRF Connect in our case. The main components seem to work well, but there are some problems regarding pairing/bonding. What we're desiring is to have the phone bonded once and to have it receive data from the board whenever they come in close contact. In order to do this, we'd like to only send indications out if the phone we're connected to is paired, for security reasons (and this is with the understanding that the two units will be paired automatically without asking for a passcode if they're bonded just once). We've not been able to figure out how to do this quite right, unfortunately. The main issue is that the "pairing_complete" callback is never reached after a reconnection of the two bonded units; it only happens right after the passcode is entered, and of course that prompt never reoccurs with a bond present. We tried the "bt_foreach_bond" function, and although it seems to work at first, it keeps getting called even after the bond is deleted in nRF Connect, and even after an incorrect passcode is entered when the prompt inevitably comes up again on the phone. We also tried the "bt_conn_security" function, but its output doesn't seem to make any sense, as described below (we don't have "bt_conn_get_security" in our Zephyr 1.14.99). Is there a simple and reliable way to detect if the phone thinks the connection is secure, either through a recent pairing event or a previous non-deleted bonding event?
Upon trying to get all of this to work, we've come across two other issues. In the interest of space, we've condensed these into steps:
- Have bonding disabled with a static passcode
- Connect, type in passcode as immediately requested
- Device starts indicating as usual
- Device says pairing success and nRF Connect App says bonded
- Disconnect via nRF Connect App
- Device says disconnected (reason 19)
- Try to reconnect via nRF Connect App
- Device says connected, indication fail, then disconnected (reason 61)
- Phone says connected but eventually times out with error 8
- Bonding information is now automatically deleted in nRF Connect App
- Connect, type in passcode as immediately requested, back to beginning
In this first one, we're not sure why bonding succeeds when it's obviously disabled. The disable works the same via "bt_set_bondable(false)" and "CONFIG_BT_BONDABLE=n". This functionality isn't seen if bonding is enabled on our nRF52840. We're not sure if static passcodes are causing either of these issues, but that's just how we have it set currently.
- Have bonding enabled or disabled with a static passcode
- Connect, cancel passcode entry immediately when prompted
- While "Bonding...", close tab for device on nRF Connect App
- No message for pairing cancelled on device; indications keep being transmitted
- Device no longer shows up in list since advertising is no longer happening
- Soon, printed security level is raised to High and indications still flow out
- Have to reset device to make it connectable again
- If instead of closing tab, "Bonding..." is let to time out, printed security level rises but device disconnects properly when asked
This could be an issue with nRF Connect, but it basically bricks our nRF52840 until it's reset, and we can't have this happen for our project. The indications seem to be successful, and we're not sure who's acknowledging them, nor why the chip doesn't detect a disconnection event. This issue might be dependent on the nRF52840's data flash content (sorry, it's all a lot to test).
We're using the "v1.0.0" checkout version of "ncs", however it's termed (we had to revert due to the MQTT sample hanging). Our nRF Connect App is v4.22.3 according to the E-mail feedback subject line. We'd rather not use the SoftDevice as we like Zephyr for the most part, and it would just add more layers of complexity. Our relevant code can be seen below.