To create an Android Java app that sends APDU requests and receives responses from a contact smart card using the ACS contact smart card reader, follow these steps:
Note: This tutorial assumes you have added the required ACS libraries to your project. You can find the ACS Android library (aacs-1.1.5.jar) in the ACS Android SDK. Download the zip file and extract the necessary JAR file.
- Inside the project, create a new Java class file called
SmartCardReader.java. This class will handle all the communication with the smart card reader.
- Implement the Singleton Design Pattern in
SmartCardReader.java to ensure only one instance is created. Here's a basic implementation:
import com.acs.smartcard.Reader;
import com.acs.smartcard.ReaderException;
import com.acs.smartcard.Features;
public class SmartCardReader {
private static SmartCardReader instance;
private Reader reader;
private SmartCardReader() {
// Implement any initialization logic here
reader = new Reader();
}
public static SmartCardReader getInstance() {
if (instance == null) {
instance = new SmartCardReader();
}
return instance;
}
// Implement smart card reader communication methods below
}
- Implement the communication methods and other necessary functionalities in the
SmartCardReader.java class:
public void attach(Context context, UsbManager usbManager, UsbDevice device)
throws ReaderException {
reader.attach(usbManager, device);
}
public void detach() {
if(reader != null)
reader.detach();
}
public int[] getSlots() {
return reader.getSlots();
}
public byte[] power(int slot) throws ReaderException {
Features features = new Features();
byte[] atr = features.getAtr(slot);
return atr;
}
- Add more methods for sending APDU commands and receiving responses. Here's a simple example of sending an APDU command:
public byte[] transmit(int slot, byte[] apdu) throws ReaderException {
Features features = new Features();
byte[] response = new byte[255];
int responseLength = features.transmit(slot, apdu, apdu.length, response, response.length);
return Arrays.copyOf(response, responseLength);
}
- Implement other necessary methods to open connections and close connections to the smart card reader in your
SmartCardReader.java class.
Once you have the SmartCardReader class set up, use it in your MainActivity:
import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private SmartCardReader smartCardReader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
smartCardReader = SmartCardReader.getInstance();
}
// Implement necessary methods to discover USB devices, connect and disconnect smart card reader
private void attachSmartCardReader() {
UsbDevice device; // This should be your discovered ACS smartcard reader
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
try {
smartCardReader.attach(getApplicationContext(), manager, device);
} catch (ReaderException e) {
e.printStackTrace();
}
}
private void detachSmartCardReader() {
smartCardReader.detach();
}
}
Ensure that you have the necessary permissions and handle the USB devices properly in your MainActivity by referring to the official ACS Android documentation.
With this implementation, you should be able to send APDU requests and receive responses from a contact smart card using an ACS contact smart card reader. Always refer to the device's documentation and SDK for any additional methods or features needed for your specific use case.