Accessing MIFARE Classic Tag

This tutorial describes how to access the MIFARE Classic tag.

Context

The MIFARE Classic tag is a NXP-specific platform type of tag. For detailed information about the MIFARE Classic tag, refer to NXP MIFARE Classic.

The NFC Tag Extension API provides access to the MIFARE Classic tags. An instance of a connection class is created and is passed to a MNfcTag object which activates the connection. On activating the connection, the tag can be accessed.

Prerequisites

Before you begin, refer to the following:

Steps

  1. Follow the steps in Discovering NFC Tags to detect the MIFARE Classic tag.

  2. Create a new instance of the MIFARE Classic tag class CMifareClassicConnection.

    For example,

    //RNfcServer is a client side connection to NFC server.
    RNfcServer iNfcServer;
    
    CMifareClassicConnection* iMifareClassicConnection;
    iMifareClassicConnection = CMifareClassicConnection::NewL(iNfcServer);
  3. Open the connection to the tag using MNfcTag::OpenConnection() and pass the instance of the tag class.

    For example,

    MNfcTag* iTag; 
    iTag->OpenConnection(*iMifareClassicConnection);       
  4. Perform any of the following operations on the MIFARE Classic tag:

      Read data from a sector of the MIFARE Classic tag using iMifareClassicConnection::ReadSector(TRequestStatus &aStatus, CMifareClassicSector &aMifareClassicSector, TUint8 aSector). For example,

       void  CMyTagInitializer::ReadFromMifareTag()
         { 
      		//Key A for sector
      		RBuf8 keyA;
          CleanupClosePushL( keyA );
          keyA.CreateL(100);
        	 
      		//Key B for sector
      		RBuf8 keyB;
          keyB.CreateL(100);
          CleanupClosePushL( keyB );
           
          TInt sect = 1;
          if(iMifareClassicDataSector)
               {
               delete iMifareClassicDataSector;
               iMifareClassicDataSector = NULL;
               }
      		//Creates a new instance of the CMifareClassicSector class.
       		iMifareClassicDataSector = CMifareClassicSector::NewL(
                   CMifareClassicSector::E1KSector);
      		
      		//Reads one sector from the target Mifare Classic tag.
        	iMifareClassicConnection->ReadSector( 
               iReadWait->iStatus, 
               *iMifareClassicDataSector, 
               sect,
               keyA,
               keyB);
           
          iReadWait->SetActive();
      
          CleanupStack::PopAndDestroy(); // keyA
          CleanupStack::PopAndDestroy(); // keyB
          }

      OR

      Write data into a sector of the MIFARE Classic tag using iMifareClassicConnection::WriteSector(TRequestStatus &aStatus, const CMifareClassicSector &aMifareClassicSector, TUint8 aSector). For example,

      void  CMyTagInitializer::WriteToMifareTag()
          {
      		//Key A for sector
      		RBuf8 keyA;
          CleanupClosePushL( keyA );
          keyA.CreateL(100);
          
          //Key B for sector
          RBuf8 keyB;
          keyB.CreateL(100);
          CleanupClosePushL( keyB );
          
          TInt sect = 1;
          if(iMifareClassicDataSector)
              {
              delete iMifareClassicDataSector;
              iMifareClassicDataSector = NULL;
              }
         	//Creates a new instance of the CMifareClassicSector class.
          iMifareClassicDataSector = CMifareClassicSector::NewL(
                  CMifareClassicSector::E1KSector);
         
          //Writes data into a sector of the MIFARE Classic tag.
          iMifareClassicConnection->WriteSector( 
                  iWriteWait->iStatus, 
                  *iMifareClassicDataSector, 
                  sect);
          
          iWriteWait->SetActive();
          
          CleanupStack::PopAndDestroy(); // keyA
          CleanupStack::PopAndDestroy(); // keyB
          }

      OR

      Control the accessibility of data blocks on a MIFARE Classic tag using MifareClassicAccessBits::SetAccessConditionForDataBlock( TUint8 aBlockNumber, MifareClassicCommon::TAccessCondition aAccessCondition). Refer the MIFARE standard card data sheet for detailed description on access conditions and bit combinations.