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

nfc change url

Hi

Regarding the NFC url example there is any way to change the URL dynamically. My idea is to, at a certain point, be able to modify the URL configured during the setup.

I tried different approaches:

  • Simply calling "nfc_uri_msg_encode" with the new url (it changes the content of ndef_msg_buf)
  • Call "nfc_uri_msg_encode" and then register the ndef_msg_buf by calling "nfcSetPayload"
  • Call "nfcStopEmulation" -> "nfc_uri_msg_encode" -> "nfcSetPayload" -> "nfcStartEmulation"

However none of these approaches worked in my case, when I call the "nfcSetPayload" it returns an error and when I change only the encoded message ("nfc_uri_msg_encode") I am able to get the tag with a smartphone but it return an empty tag.

Any one knows a way to dynamically change the url of a nfc tag?

Thanks in advance

  • What is the error code is returned from your call to nfcSetPayload()?

  • Hi Einar, thanks for your reply, the returned error code is 2

  • The last option you have suggested should work. I just verified it now by modifying the URI Message Example. You can try my modified main.c (quick and dirty).

    The main thing, as suggested is to do the following steps:

    1. nfcStopEmulation
    2. nfc_uri_msg_encode
    3. nfcSetPayload
    4. nfcStartEmulation

    Remember to use correct size of the new data in the calls to nfc_uri_msg_encode() and nfcSetPayload().

  • Hi

    Based on the code of Einar I was able to find the solution. It is necessary only to change a simple detail in the code to make it work. We need to pass the reference to the first element of the ndef_msg_buf. I left the code of a function that allows to change dynamically the nfc tag.

    void change_nfc_tag()
    {
    
        uint8_t ret_val, err_code;
    
        ret_val = nfcStopEmulation();
        if (ret_val != NFC_RETVAL_OK)
        {
            APP_ERROR_CHECK((uint32_t) ret_val);
        }
        
        
        /* The new URL */
        char url2[50];
        uint32_t len2 = sizeof(ndef_msg_buf2);
        memset(url2,'0',sizeof(url3));
        sprintf(url2,"nordicsemi.com"); //you can use format to construct the string. Take care with the size limit (50 in my case)
    
        /* Encode URI message into buffer */
        err_code = nfc_uri_msg_encode( NFC_URI_HTTP_WWW,
                                       (uint8_t *) url2,
                                       sizeof(url2),
                                       &ndef_msg_buf2[0],
                                       &len2);
                                       
        APP_ERROR_CHECK(err_code);
        /** @snippet [NFC URI usage_1] */
    
        /* Set created message as the NFC payload */
        ret_val = nfcSetPayload( (char*)ndef_msg_buf2, len2);
        if (ret_val != NFC_RETVAL_OK)
        {
            APP_ERROR_CHECK((uint32_t) ret_val);
        }
        
        /* Start sensing NFC field */
        ret_val = nfcStartEmulation();
        if (ret_val != NFC_RETVAL_OK)
        {
            APP_ERROR_CHECK((uint32_t) ret_val);
        }
    }