| nRF5 SDK v12.0.0 |

Choose documentation: | nRF5 SDK | S130 SoftDevice API | S132 SoftDevice API | S212 SoftDevice API | S332 SoftDevice API

All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
Programming a tag
This information applies to the nRF52 Series only.

To program a tag, use the precompiled Type 2 Tag library.

Complete the following steps:

1. Implement a callback function that handles events from the Type 2 Tag library and register it:
   uint32_t err_code;
   NfcRetval ret_val;
   /* Callback for NFC events */
   void nfc_callback(void *context, NfcEvent event, const char *data, size_t dataLength)
   {
   ...
   }
   /* Set up NFC and register application callback for NFC events. */
   ret_val = nfcSetup(nfc_callback, NULL);
   if (ret_val != NFC_RETVAL_OK)
   {
   APP_ERROR_CHECK((uint32_t) ret_val);
   }
   The NFCT interrupt handler runs at priority level APP_LOW.
2. Configure the data for the tag. You can provide the data as NDEF message (recommended, see NFC Data Exchange Format) or as a raw TLV structure (advanced usage, see Type 2 Tag data format).
   - Set an NDEF message:
     uint8_t ndef_msg_buf[] = ...; // Buffer with the user NDEF message
     uint32_t len = sizeof(ndef_msg_buf);
     /* Set created message as the NFC payload. */
     ret_val = nfcSetPayload( (char*)ndef_msg_buf, len);
     if (ret_val != NFC_RETVAL_OK)
     {
     APP_ERROR_CHECK((uint32_t) ret_val);
     }
   - Alternatively, set a TLV structure:
     uint8_t tlv_buf[] = ...; // Buffer with the user TLV structure
     uint32_t len = sizeof(tlv_buf);
     /* Set created message as the NFC payload. */
     ret_val = nfcSetPayloadRaw( (char*)tlv_buf, len);
     if (ret_val != NFC_RETVAL_OK)
     {
     APP_ERROR_CHECK((uint32_t) ret_val);
     }
3. Activate the NFC tag so that it starts sensing and reacts when an NFC field is detected:
   /* Start sensing NFC field. */
   ret_val = nfcStartEmulation();
   if (ret_val != NFC_RETVAL_OK)
   {
   APP_ERROR_CHECK((uint32_t) ret_val);
   }