Bluetooth: NFC pairing

The NFC pairing sample demonstrates Bluetooth® LE out-of-band pairing using an NFC tag and the NFC TNEP protocol. You can use it to test the touch-to-pair feature between Nordic Semiconductor’s devices and an NFC polling device with Bluetooth LE support, for example, a mobile phone.

The sample shows the usage of NFC NDEF Connection Handover messages and records and TNEP for tag device with the Connection Handover service. It provides minimal Bluetooth functionality in Peripheral role and on GATT level it implements only the Device Information Service.

The sample supports pairing in one of the following modes:

  • LE Secure Connections Just Works pairing

  • LE Secure Connections OOB pairing

  • Legacy OOB pairing

  • Legacy Just Works pairing

  • Negotiated Handover (TNEP protocol) - two-way OOB pairing

Requirements

The sample supports the following development kits:

Hardware platforms

PCA

Board name

Build target

nRF5340 DK

PCA10095

nrf5340dk_nrf5340

nrf5340dk_nrf5340_cpuapp_ns nrf5340dk_nrf5340_cpuapp

nRF52 DK

PCA10040

nrf52dk_nrf52832

nrf52dk_nrf52832

nRF52840 DK

PCA10056

nrf52840dk_nrf52840

nrf52840dk_nrf52840

When built for an _ns build target, the sample is configured to compile and run as a non-secure application with Cortex-M Security Extensions enabled. Therefore, it automatically includes Trusted Firmware-M that prepares the required peripherals and secure services to be available for the application.

The sample has the following additional requirements:

  • NFC polling device (for example, a smartphone or a tablet with NFC support).

  • NFC polling device with TNEP support (when testing the two-way OOB pairing). You can use the corresponding Bluetooth: Central NFC pairing as an NFC Polling Device.

Overview

When the application starts, it initializes and starts the NFCT peripheral that is used for pairing. The application does not start advertising immediately, but only when the NFC tag is read by an NFC polling device, for example a smartphone or a tablet with NFC support, or when the exchange of data over the TNEP Connection Handover Service has completed. The message that the tag sends to the NFC device contains the data required to initiate pairing. To start the NFC data transfer, the NFC device must touch the NFC antenna that is connected to the development kit.

After reading the tag or exchanging data over TNEP, the initiator can connect and pair with the device that is advertising. The connection state of the device is signaled by the LEDs. When the connection is lost due to a timeout, the library automatically triggers direct advertising.

NFC data exchange

This sample can exchange OOB data with an NFC Forum Polling device with TNEP support or can be read as a Static Tag by NFC Forum Polling Devices that do not support TNEP.

Static Handover

A tag in the Static Handover mode, contains a Handover Select Message with carrier information NDEF records or a single Carrier Configuration Record.

Static Handover

Negotiated Handover

In Negotiated Handover, two NFC Forum Devices identify a matching set of alternative carriers by exchanging lists of potential alternative carriers. In case of the OOB data for Bluetooth LE, the NFC devices can exchange OOB data in a two-way manner. Handover messages are exchanged using the TNEP Single Response communication mode. The NFC Poller Device can have the Handover Requester or Handover Selector Device role. The role is assumed based on the first NDEF message from the NFC Tag.

You can configure this sample to take the Connection Handover Selector role or the Connection Requester role. The Connection Handover Selector role is default for this sample. You can change the default role by choosing CONFIG_NFC_TAG_CH_SELECTOR or CONFIG_NFC_TAG_CH_REQUESTER.

Negotiated Handover

User interface

Button 4:

Removes all bonded devices and terminates current connections.

LED 1:

Indicates that a Bluetooth connection is established.

LED 2:

Indicates that an NFC field is present.

Building and running

This sample can be found under samples/bluetooth/peripheral_nfc_pairing in the nRF Connect SDK folder structure.

When built as firmware image for the _ns build target, the sample has Cortex-M Security Extensions (CMSE) enabled and separates the firmware between Non-Secure Processing Environment (NSPE) and Secure Processing Environment (SPE). Because of this, it automatically includes the Trusted Firmware-M (TF-M). To read more about CMSE, see Processing environments.

To build the sample with Visual Studio Code, follow the steps listed on the How to build an application page in the nRF Connect for VS Code extension documentation. See Building and programming an application for other building and programming scenarios and Testing and debugging an application for general information about testing and debugging in the nRF Connect SDK.

Testing

After programming the sample to your development kit, complete the following steps to test it:

Testing with NFC Poller Device

  1. Touch the NFC antenna with the smartphone or tablet and observe that LED 2 is lit.

  2. Confirm pairing with Nordic_NFC_pairing in a pop-up window on the smartphone or tablet and observe that LED 1 lights up.

  3. Move the smartphone or tablet away from the NFC antenna and observe that LED 2 turns off.

Testing with NFC TNEP Poller Device

  1. Touch the NFC antenna with the NFC Poller Device, for example Bluetooth: Central NFC pairing and observe that LED 2 is lit.

  2. Observe the output log. The content of the exchanged NDEF messages is printed there.

  3. Check the security level of the paring on the terminal.

  4. Move the NFC antenna away from the NFC Poller Device.

Dependencies

This sample uses the following nRF Connect SDK libraries:

In addition, it uses the Type 4 Tag library from nrfxlib:

It uses the following Zephyr libraries:

The sample also uses the following secure firmware component: