Show TOC Start of Content Area

This graphic is explained in the accompanying textWrite  Locate the document in its SAP Library structure

In this example the RFID reader writes data to a tag. The program does the following:

...

       1.      Gets the RFID parameters from the first driver and, using them, opens the connection to the RFID reader.

       2.      Checks if the driver supports the "identify all", the "write single", and the "read single" attributes.

       3.      Looks for the first tag with writable areas.

       4.      Creates a byte array, with random values, of the size of each of the writable area.

       5.      Writes the data above to the tag and reads the data back for corroboration.

       6.      Checks, byte per byte, if the written and read data are the same.

 

import java.util.*;

 

import com.sap.ip.me.api.pios.PIOSException;

import com.sap.ip.me.api.pios.connection.*;

import com.sap.ip.me.api.pios.rfid.*;

 

public class ExampleWriteTag {

 

   public static void main(String[] args) {

 

       RfidConnection rfidConnection = null;

 

       try {

          //----------------------(1)--------------------

          Connector connector = Connector.getInstance();

 

          DriverInfo[] rfidDrivers =

              connector.listDrivers(ConnectionType.RFID);

 

          if (rfidDrivers.length > 0) {

 

              RfidParameters rfidParams = new RfidParameters(rfidDrivers[0]);

 

              rfidConnection = (RfidConnection) connector.open(rfidParams);

 

              //----------------------(2)--------------------

              if (rfidDrivers[0]

                 .isAttributeSupported(

                     RfidConnection.Attributes.IDENTIFY_ALL)

                 && rfidDrivers[0].isAttributeSupported(

                     RfidConnection.Attributes.WRITE_SINGLE)

                 && rfidDrivers[0].isAttributeSupported(

                     RfidConnection.Attributes.READ_SINGLE)) {

 

                 RfidTag[] tagList = rfidConnection.identify();

 

                 if (tagList.length > 0) {

 

                     boolean writableAreas = false;

 

                     RfidTag sampleTag = null;

 

                     //----------------------(3)--------------------

                     for (int i = 0;

                        (i < tagList.length) && !writableAreas;

                        i++) {

 

                        RfidTagUserArea[] areas =

                           tagList[i].getTagType().getUserWritableAreas();

 

                        if (areas.length > 0) {

 

                           writableAreas = true;

 

                           sampleTag = tagList[i];

 

                        }

                     }

                     if (sampleTag != null) {

 

                        RfidTagUserArea[] areas =

                           sampleTag.getTagType().getUserWritableAreas();

 

                        //----------------------(4)--------------------

                        for (int j = 0; j < areas.length; j++) {

 

                           int writableSize =

                               areas[j].getEndPos()

                                  - areas[j].getStartPos()

                                  + 1;

 

                           byte[] dataBytes = new byte[writableSize];

 

                           new Random().nextBytes(dataBytes);

 

                           //----------------------(5)--------------------

                           rfidConnection.write(

                               sampleTag,

                               areas[j].getStartPos(),

                               dataBytes);

 

                           RfidTagData data =

                               rfidConnection.read(

                                  sampleTag,

                                  areas[j].getStartPos(),

                                  writableSize);

 

                           //----------------------(6)--------------------

                           if (data != null) {

 

                               System.out.println(

                                  "Check tag data #" + (j + 1) + ": ");

 

                               if (isEqual(data.getTagData(),

                                  dataBytes)) {

 

                                  System.out.println(

                                      "The written and read data are the same.");

 

                               } else {

 

                                  System.out.println(

                                      "The written and read data are different.");

 

                               }

                           }

                        }

                     } else {

 

                        System.out.println("No writable tags in range.");

 

                     }

                 } else {

 

                     System.out.println("No tags in range.");

 

                 }

              } else {

                 System.out.println(

                     "The required attributes are not supported by this driver.");

              }

          } else {

 

              System.out.println("There are no RFID drivers.");

 

          }

       } catch (Exception ex) {

 

          ex.printStackTrace();

 

       } finally {

          try {

 

              rfidConnection.close();

 

          } catch (PIOSException pex) {

 

              pex.printStackTrace();

 

          }

       }

 

   }

 

   public static boolean isEqual(byte[] a, byte[] b) {

       if (a.length != b.length) {

          return false;

       }

 

       for (int i = 0; i < a.length; i++) {

          if (a[i] != b[i]) {

              return false;

          }

       }

 

       return true;

   }

 

}

 

 

End of Content Area