Using SATSA-APDU

Creating an APDU connection

The SATSA-APDU package has been implemented as an extension of the Generic Connection Framework (GCF), which provides an API for data connections. With GCF, the application requests a connection from the factory class javax.microedition.io.Connector. The request returns an object that is can be used for exchanging data on the connection.

When creating an APDU connection, you need to pass a request to Connector’s open() method including a locator string representing a card application. This request returns an APDUConnection that can be used to communicate with the card application. An example locator string looks like the following:

"apdu:0;target=a0.00.00.00.62.03.01.0c.02.01"

The locator string always begins with "apdu". The locator string identifies the slot number (here 0) and the card application identifier (here a0.00.00.00.62.03.01.0c.02.01).

The open() method throws a ConnectionNotFoundException in case the defined slot cannot be found, there is no card in the slot, or if the card application is not available.

To find out a device’s available card slots at runtime, retrieve the value of the microedition.smartcardslots system property by using the getProperty() method in the java.lang.System class.

Exchanging APDU messages

To send a command to a card application and receive a response, use the exchangeAPDU() method.

The request and response messages are both in byte array format. To send a message to the card, pass a byte array containing a command APDU to exchangeAPDU(). The card will then send its response APDU as another byte array.

The following is an example APDU:

private final byte[] kCardAPDU = 
    // Select File EF_ID
    {(byte)0x00, (byte)0xA4, (byte)0x08, (byte)0x00,
     (byte)0x02, (byte)0x00, (byte)0x03 };

Closing an APDU connection

To close an APDUConnection, call the close() method on the connection as shown in the example below:

cardConnection0.close();

Note: If the connection is used by other threads for exchanging APDU message when you call close(), the connection is closed immediately and the exchangeAPDU() methods in other threads throw InterruptedException.