This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Read NDEF after tag write

Hi,

I am having trouble reading the contents of a record after the NFC tag in the microcontroller has been written by a smartphone.

I'm using the t4t emulator. When the tag has been written the callback gets called. In the callback I use the ndef_record_parser() to parse the data, and I use the ndef_record_printout() to print the payload.

The problem is that the payload contains unreadable data. It does not contain the text that has been written to the tag.

What is the correct way of reading the contents that are written to the tag?

Thanks, Jan

  • FormerMember
    0 FormerMember

    I don't know what your code looks like, so I will just tell how it works.

    When writing to the NFC tag from another device, the initial data will be over written, and stored in the set buffer in nfc_t4t_ndef_rwpayload_set() during the initialization. In the example writable_ndef_msg, the buffer is m_ndef_msg_buf.

    Since m_ndef_msg_buf is overwritten upon a write, that is where the data to be parsed is located. (Note: the data field in the event "NFC_T4T_EVENT_NDEF_UPDATED" doesn't contain valid data. The only valid data for that event is dataLength.)

    For a Tag Type 4, the written data can be printed out the following way:

    /**
    * @brief Callback function for handling NFC events.
    */
    static void nfc_callback(void          * context,
                         nfc_t4t_event_t event,
                         const uint8_t * data,
                         size_t          dataLength,
                         uint32_t        flags)
    {
        (void)context;
    	ret_code_t ret_val;
    
        uint8_t  read_nfc_buffer[NFC_NDEF_PARSER_REQIRED_MEMO_SIZE_CALC(10)];
        uint32_t read_nfc_buffer_length = sizeof(read_nfc_buffer);		
    
        switch (event)
        {
            ....
    
            case NFC_T4T_EVENT_NDEF_UPDATED:
    				
    			NRF_LOG_INFO("NFC_T4T_EVENT_NDEF_UPDATED \r\n");
    		
                if (dataLength == 0)
                {
                    m_update_state = true;
    	            NRF_LOG_INFO("DataLength:  0 \r\n");
                }
                else if (m_update_state == true)
                {
    	            ret_val = ndef_msg_parser(read_nfc_buffer, &read_nfc_buffer_length, m_ndef_msg_buf+2, &dataLength);
    		        //APP_ERROR_CHECK(ret_val);
    			    if (ret_val != NRF_SUCCESS)
    			    {
    			        NRF_LOG_INFO("Error code: 0x%x \r\n", ret_val);
    				    NRF_LOG_INFO("Error during parsing a NDEF message.\r\n");
    			    }
    			
    			    ndef_msg_printout((nfc_ndef_msg_desc_t *) read_nfc_buffer);
                    m_update_state = false;
                    bsp_board_led_on(BSP_BOARD_LED_1);
                }
                break;
    
        default:
            break;
        }
    }
    
  • Hi Kristin, what includes are necessaries to be able to use the functions NFC_NDEF_PARSER_REQIRED_MEMO_SIZE_CALC and ndef_msg_printout?

    Thanks

  • FormerMember
    0 FormerMember in reply to FormerMember

    You would need to include nfc_ndef_msg_parser.c/.h and nfc_ndef_msg_parser_local.c/.h.

    In addition, the NFC NDEF message parser module has to be enabled in sdk_config.h: (see the sdk_config.h file in this folder: \nRF5_SDK_13.0.0_04a0bfd\config )

    // <e> NFC_NDEF_MSG_PARSER_ENABLED - nfc_ndef_msg_parser - NFC NDEF message parser module
    //==========================================================
    #ifndef NFC_NDEF_MSG_PARSER_ENABLED
    #define NFC_NDEF_MSG_PARSER_ENABLED 0
    #endif
    #if  NFC_NDEF_MSG_PARSER_ENABLED
    // <e> NFC_NDEF_MSG_PARSER_LOG_ENABLED - Enables logging in the module.
    //==========================================================
    #ifndef NFC_NDEF_MSG_PARSER_LOG_ENABLED
    #define NFC_NDEF_MSG_PARSER_LOG_ENABLED 0
    #endif
    #if  NFC_NDEF_MSG_PARSER_LOG_ENABLED
    // <o> NFC_NDEF_MSG_PARSER_LOG_LEVEL  - Default Severity level
     
    // <0=> Off 
    // <1=> Error 
    // <2=> Warning 
    // <3=> Info 
    // <4=> Debug 
    
    #ifndef NFC_NDEF_MSG_PARSER_LOG_LEVEL
    #define NFC_NDEF_MSG_PARSER_LOG_LEVEL 3
    #endif
    
    // <o> NFC_NDEF_MSG_PARSER_INFO_COLOR  - ANSI escape code prefix.
     
    // <0=> Default 
    // <1=> Black 
    // <2=> Red 
    // <3=> Green 
    // <4=> Yellow 
    // <5=> Blue 
    // <6=> Magenta 
    // <7=> Cyan 
    // <8=> White 
    
    #ifndef NFC_NDEF_MSG_PARSER_INFO_COLOR
    #define NFC_NDEF_MSG_PARSER_INFO_COLOR 0
    #endif
    
    #endif //NFC_NDEF_MSG_PARSER_LOG_ENABLED
    // </e>
    
    #endif //NFC_NDEF_MSG_PARSER_ENABLED
    // </e>
    
  • Thanks also for posting the example code. I would have never found that I had to skip the first 2 bytes of the buffer.

Related