NFC - Access the content of NFC Tag


Preparation
  • Ameba x 1
  • Smartphone with NFC feature x 1


Example

Android phones with NFC feature are able to write data to the NFC tag of Ameba. The data can be stored to the flash memory, so as to preserve the data even if Ameba is turned off.
There are many NFC application in Google Play, for example:
https://play.google.com/store/apps/details?id=com.wakdev.wdnfc
https://play.google.com/store/apps/details?id=com.nxp.taginfolite
These applications can read the NDEF content of NFC Tags, as well as write NDEF content to NFC Tags. In this example, we will show you how to read and write the NFC Tag.

Open the sample code in “File” -> “Examples” -> “AmebaNFC” -> “StoreTagContent”. Compile and upload to Ameba, then press the reset button.

Here we use NFC Tools as example.
Open the application, select the “READ” tab at the top of the page. Then bring the phone near to Ameba. If the NFC connection is established, the data of the NFC Tag would be displayed in the App. Scroll down the page, you can see a “Hello World” message.
1

Next, select the “WRITE” tab at the top of the page. Click “Add a record” -> “Text”, and enter “new msg”, click “OK”, “Write”. Then bring the phone near the NFC Tag of Ameba.
2

If the write is successful, you can see a completed message.
3

Switch the tab back to “READ” to check the message is written to Ameba.
4

The “text/plain” content is modified to “new msg”. Note that the content would preserve even if Ameba is turned off.


Code Reference

In this example, we use the Flash Memory API and NFC API. First we read from the Flash Memory and check if the content is correct by calculating the checksum of NFC UID.(Note: if you are using the NFC component for the first time, the checksum would be incorrect.)
We use NfcTag.appendRtdText("Hello World!") to add plain text NDEF content, then call NfcTag.begin() to enable the NFC Tag.
In the sample code, we copy the buffer of the NFC Tag to the buffer of flash memory every time the NFC Tag is modified, and call FlashMemory.update() to update to flash memory.
Next time the Ameba is turned on, we copy the buffer of flash memory to the buffer of NFC Tag.
Please confirm that QQ communication software is installed