NFC Reader
Last updated
Was this helpful?
Last updated
Was this helpful?
The can be thought of as a sensor, but it has some particularities that we need to address. In this article, we assume you have successfully initialized the devices with the , and you stored all connected devices on a global variable devices
. We also declared a global variable called nfcReader
.
Next, we need to know the , which 286:
Once we have a reference to the NFC reader, we can use the scan()
function to tell the reader to start scanning. When an NFC tag is placed on the reader, it will detect it and read the information that is stored on it:
When calling the scan()
function, you must provide two callback functions as arguments: The first is called when the NFC reader has found and read a tag successfully, the second if the reader has found a tag, but there was an error reading it.
In the function readingDone()
we have access to the data stored on the NFC tag via the parameter val
. In the current version of the Tinkerforge Device Manager, the tag's information must be a simple JSON object with two fields. For example, this could be stored on a NFC sticker:
We can obtain this JSON object from the val
parameter:
When a tag was read, whether that was successful or not, the NFC reader goes into an idle status. To keep the reader in a state where it can detect and read tags, we can take the following measurements:
In the callback function readingDone()
, we simply use nfcReader.scan()
directly after we processed the data from the current tag. This puts the NFC reader back to scan mode. We must do the same in the readingFailed()
function if we want to make sure the reader will always stay in scan mode.
In the example above, we put the NFC reader back to scan mode directly after we received the data from the current tag. As a consequence, we will see the same tag and its data many times because we can't remove the tag fast enough (and the reader is really fast). To prevent this issue, we can set a timeout of 1 second before we start scanning again:
The Tinkerforge Device Manager currently only support NFC Forum Typ 2 tags. These include the NFC stickers and the NFC cards that are available on the .