Use the Custom NDEF messages and Custom NDEF records modules to create standardized messages and records that are not covered by other modules, or to create your own custom messages.
To generate a custom NDEF message, start by creating the records that make up the message. Next, generate an empty message of the required length and add the records to the message. Finally, encode and create the message.
You can choose the tag's platform with NFC_NDEF_MSG_TAG_TYPE in sdk_config.h
. In case of Type 2 Tag platform, an NDEF message is encoded according to the NDEF message and record format. In case of Type 4 Tag an additional field is added in front of an NDEF message. See NDEF file format for more details.
You can also encapsulate a message as payload for a record. For example, a Handover Request message contains several Alternative Carrier records. These records themselves are full messages that contain information about alternative carriers that can be used for further communication.
You can provide the payload of the record in binary format or in a different format that is then converted into binary format. The content of the record is defined in a record descriptor.
Use the NFC_NDEF_RECORD_BIN_DATA_DEF macro for binary data or the NFC_NDEF_GENERIC_RECORD_DESC_DEF macro for other data to create the record descriptor. You must provide header information for the record (for example, ID and type) and the payload data to the macro. For generic data that is not in binary format, you must also provide a payload constructor. This constructor converts the payload data to the needed format. For binary data, the module provides this constructor.
The following code example shows how to generate a record descriptor:
A message consists of one or more records. Accordingly, a message descriptor (that holds the content of the message) contains an array of pointers to record descriptors.
Use the NFC_NDEF_MSG_DEF macro to create the message descriptor and the array of pointers. When creating the message descriptor, you specify how many records the message will contain, but you do not specify the actual records yet.
To add the records, use the nfc_ndef_msg_record_add function.
After adding all records, call nfc_ndef_msg_encode to actually create the message from the message descriptor. nfc_ndef_msg_encode internally calls nfc_ndef_record_encode to encode each record. The NDEF records are always encoded in long format. If no ID field is specified, a record without ID field is generated.
The following code example shows how to create two messages:
To encapsulate a message in a record so that it can be added to another message, use the NFC_NDEF_NESTED_NDEF_MSG_RECORD_DEF macro to create the record descriptor. This record descriptor uses nfc_ndef_msg_encode as payload constructor. You can then add this record descriptor to a message like any other record descriptor.
The following code example shows how to encapsulate a message as payload for a record: