Exchanging Data with NFC Forum Type 4 Tags

This tutorial describes how to send data to and receive data from NFC Forum type 4 tags.

Context

The NFC Tag Extension API can be used to exchange data with type 4 tags using the CIso14443Connection::ExchangeData() method.

Prerequisites

Before you begin, refer to the following:

Steps

  1. Follow the steps in Discovering NFC Tags to detect the type 4 tag.

  2. Create a new instance of the CIso14443Connection class.

    For example,
    CIso14443Connection* iIso14443Connection;
    iIso14443Connection = CIso14443Connection::NewL(iNfcServer);
  3. Open the connection to the tag using MNfcTag::OpenConnection() and pass the instance of CIso14443Connection class.

    MNfcTag* iTag;	 
    iTag->OpenConnection(*iIso14443Connection);       
  4. Exchange data using the CIso14443Connection::ExchangeData() method. The ExchangeData() method exchanges Application Protocol Data Unit (APDU) commands with the target ISO 14443 tag.

    For example,
    void CNfcTagHandler::ExchangeData()
        {
        // Exchange data with the tag.
        iIso14443Connection->ExchangeData( iStatus, iTransmitData, iReceivedData );
        SetActive();
        }

Example

The following example illustrates how to exchange data with an ISO 14443 tag:

#include <e32base.h>
#include <iso14443connection.h>         // CIso14443Connection
#include <nfctag.h>                     // MNfcTag
#include <nfctagconnectionlistener.h>   // MNfcTagConnectionListener

class CNfcTagHandler : public CActive,                        
                       public MNfcTagConnectionListener                       
   {
public:
   ... 

public: // From MNfcTagConnectionListener
    
   // This method is called when tag is detected
   void TagDetected( MNfcTag* aNfcTag );        

   // This method is called when the tag is removed from near field.
   void TagLost();
   
public:
   void ExchangeData();
   
private:
   CIso14443Connection* iIso14443Connection;
   RNfcServer iNfcServer;
   MNfcTag* iTag;
   RBuf8 iTransmitData;
   RBuf8 iReceivedData;
   };

void CNfcTagHandler::TagDetected( MNfcTag* aTag )
    {    
    TInt error = KErrNone;
    iTag = aTag;
    // Create connection if not created yet.
    if ( !iIso14443Connection )
        {
       TRAP(err, iIso14443Connection = CIso14443Connection::NewL( iNfcServer ));       
        }
            
    // Checking the tag type.
    if ( aTag->HasConnectionMode( TNfcConnectionInfo::ENfc14443P4 ) )
        {
        // Open the connection to tag.
        error = aTag->OpenConnection( *iIso14443Connection );                
        }
    // Checking errors is omitted for clarity.        
    }

void CNfcTagHandler::TagLost()
    {
    // Deleting aTag has to be handled since its ownership is transferred to
    // the class implementing MNfcTagConnectionListener interface.     
    delete iTag;
    iTag = NULL;
    }

void CNfcTagHandler::ExchangeData()
    {
    //Exchange data with the tag.
    iIso14443Connection->ExchangeData( iStatus, iTransmitData, iReceivedData );
    SetActive();
    }

Related concepts

Related tasks