This tutorial describes how to write data to NFC Forum Type 1, Type 2 and Type 3 tags.
The NFC
Tag Extension API provides access to different types of tags. An instance
of a connection class is created and is passed to MNfcTag
object which activates the connection. On activating the connection,
the tags can be accessed for writing data.
Before you begin, refer to the following:
nfcconnection.h, MNfcConnection
nfctype1connection.h, CNfcType1Connection
nfctype2connection.h, CNfcType2Connection
nfctype3connection.h, CNfcType3Connection
nfctag.h, MNfcTag
Follow the steps in Discovering NFC Tags to detect the type 1 tag.
Create a new instance of the specific tag class.
CNfcType1Connection
class. For example, CNfcType1Connection* Type1Connection; Type1Connection = CNfcType1Connection::NewL(iNfcServer);
CNfcType2Connection
class. For example, CNfcType2Connection* Type2Connection; Type2Connection = CNfcType2Connection::NewL(iNfcServer);
CNfcType3Connection
class. For example, CNfcType3Connection* Type3Connection; Type3Connection = CNfcType3Connection::NewL(iNfcServer);
Open the
connection to the tag using MNfcTag::OpenConnection()
and pass the instance of the tag class.
For example for the Type 1 tag:
MNfcTag* iTag; iTag->OpenConnection(*Type1Connection);
Write data to the tags using the specific tag methods .
Note: Only one write command can be requested at a time for a specific type of tag.
Type 1 Tag: Write data to Type 1 tag using CNfcType1Connection::Write()
.
void CMyTagInitializer::Type1WriteL() { _LIT8(KSendText,"class1TagData"); iWriteBuffer.ReAlloc(KSendText().Size()); //Specify the block address TInt block = 0; //Specify the byte address TInt byte = 6; TNfcType1Address address(byte, block); iWriteBuffer = KSendText; //Write data to the target NFC Forum Type 1 tag. iNfcType1Connection->Write( iWriteWait->iStatus, iWriteBuffer, address ); iWriteWait->SetActive(); }
Type 2 Tag: Write data to Type 2 tag using CNfcType2Connection::Write()
.
void CMyTagInitializer::Type2WriteL() { _LIT8(KSendText,"class2TagData"); iWriteBuffer.ReAlloc(KSendText().Size()); //Specify the block address TInt block = 0; //Specify the byte address TInt byte = 6; TNfcType2Address address(byte, block); iWriteBuffer = KSendText; //Write data to the target NFC Forum Type 2 tag. iNfcType2Connection->Write( iWriteWait->iStatus, iWriteBuffer, address ); iWriteWait->SetActive(); }
Type 3 Tag: Write data to Type 3 tag using CNfcType3Connection::Update()
.
void CMyTagInitializer::UpdateClass3Tag() { _LIT8(KSendText,"class2TagData"); iWriteBuffer.ReAlloc(KSendText().Size()); //Specify the block address TInt block = 0; //Specify the byte address TInt byte = 6; //Specify the servicecode address. TInt service = 11; TNfcType2Address address(byte, block, service); iWriteBuffer = KSendText; //Write data to the target NFC Forum Type 2 tag. iNfcType3Connection->Update( iWriteWait->iStatus, iWriteBuffer, address ); iWriteWait->SetActive(); }
Note: The TNfcType1Address
, TNfcType2Address
and TNfcType3Address
are helper classes which
provides access to manage NFC Forum Type 1, Type 2 and Type 3 addresses
respectively.
The sequence diagram below illustrates how writing to a tag works: