Announce two new product: SmartCard Reader Rockey200 and USB2.0 Extension Cable

ROCKEY 200 is a handy and portable USB Smart Card reader that can perform read/write operations on any ISO 7816-1/2/3/4 smart cards which are compatible with protocol T=0 and T=1.

USB2.0 Extension Cable With high speed USB 2.0 devices such as USB token ,USB Card Reader and so on.

Come to try it now!

The Communication Sample Code

Applets Development Guide

Moderator: product

User avatar
Posts: 259
Joined: Thu Apr 30, 2015 12:00 pm
Points: 2267

The Communication Sample Code

Postby JavaCardOS » Thu Jun 16, 2016 5:26 am

The following code is the Communication Sample Code in JavaCard API Specification.

You can copy and use it directly or you can download the JCIDE project from the attachment and build it.

Code: Select all

 * @file
 * @brief Communication Sample Code in JavaCard API Specification
 * @comment The purpose of this example is only used to show the usage of API functions and there is no practical significance.
 * @copyright Copyright(C) 2016 JavaCardOS Technologies Co., Ltd. All rights reserved.
package JavaCardOS.Sample.Communication;

import javacard.framework.*;
import javacardx.apdu.ExtendedLength;

public class Communication extends Applet implements ExtendedLength
   public static final byte INS_SEND_RECV_APDU_1     = 0x01;
   public static final byte INS_SEND_RECV_APDU_2     = 0x02;
   public static final byte INS_RECV_EXTEND_APDU     = 0x03;
   public static final byte INS_SEND_EXTEND_APDU     = 0x04;
   private byte[] tmp_memory;
   public static final short MAX_LENGTH = (short)(0x7FFF);
   public static void install(byte[] bArray, short bOffset, byte bLength)
      new Communication().register(bArray, (short) (bOffset + 1), bArray[bOffset]);

   public void process(APDU apdu)
      if (selectingApplet())
      //Get the apdu buffer datas
      byte[] buf = apdu.getBuffer();
      byte cla= buf[ISO7816.OFFSET_CLA];
      byte ins = buf[ISO7816.OFFSET_INS];
      byte p1 = buf[ISO7816.OFFSET_P1];
      byte p2 = buf[ISO7816.OFFSET_P2];
      short p1p2 = Util.makeShort(p1, p2);
      if (cla != (byte)0x80)
      if (p1p2 != 0x00)
      //Calling this method indicates that this APDU has incoming data.
      short recvLen = apdu.setIncomingAndReceive();
      //Get the incoming data length(Lc).
      short lc = apdu.getIncomingLength();
      if (lc == 0x00)
      switch (ins)
      case (byte)INS_SEND_RECV_APDU_1:
         //Define a byte string
         byte sendStr[] = {'A','P','D','U', ',', 'C','l','a','s','s', ',', 'D','e','m','o'};
         short len = (short) sendStr.length;
         //Copy character to APDU Buffer.
         Util.arrayCopyNonAtomic(sendStr, (short)0, buf, (short)0, (short)len);
         //Send the 'sendStr' string, the hex of JCRE sending data is the ASCII of sendStr.
         apdu.setOutgoingAndSend((short)0, (short)len);
      case (byte)INS_SEND_RECV_APDU_2:
         //Set the length of received bytes into the 'buf' array,  the offset is 0.
         Util.setShort(buf, (short)0, (short)lc);
         //Set the data transfer direction to outbound.
         short le = apdu.setOutgoing();
         //Set the expected(le) length of response data.
         //Sends the data of APDU buffer 'buf', the length is 'le' bytes,  the offset is 0.
         apdu.sendBytes((short) 0, (short)le);
      case (byte) INS_RECV_EXTEND_APDU: //
         short pointer = 0;
         tmp_memory = new byte[MAX_LENGTH];
         short offData = apdu.getOffsetCdata();
         // recvieve data and put them in a byte array 'tmp_memory'
         while (recvLen > (short) 0)
            Util.arrayCopy(buf, offData, tmp_memory, pointer, recvLen);
            pointer += recvLen;
            //Gets as many data bytes as will fit without APDU buffer overflow
            recvLen = apdu.receiveBytes(offData);

         // send the lc length
         Util.setShort(buf, (short)0, lc);
         apdu.sendBytesLong(buf, (short) 0, (short)2);
      case (byte) INS_SEND_EXTEND_APDU: //
         if(buf[ISO7816.OFFSET_LC] != 0x02)
         short sendLen = Util.getShort(buf, ISO7816.OFFSET_CDATA);
         // send the specified length data in byte array 'tmp_memory'
         apdu.sendBytesLong(tmp_memory, (short)0, sendLen);
         //request Object Deletion
         tmp_memory = null;


The purpose of this example is only used to show the usage of API functions and there is no practical significance.
You do not have the required permissions to view the files attached to this post. Please login first.

Return to “Applets Development Guide”

Who is online

Users browsing this forum: No registered users and 1 guest

JavaCard OS : Disclaimer