Skip to content

Working with card readers (smart‐card‐reader‐lib)

Kristel Merilain edited this page Jun 28, 2021 · 3 revisions

High level API for communicating with supported smart card readers in Android.

Currently supported smart card readers:

Basic usage

Context context = ...;
UsbManager usbManager = ...;

ImmutableList<SmartCardReader> readers = ImmutableList.of(
        new AcsSmartCardReader(usbManager),
        new IdentivSmartCardReader(context, usbManager));
SmartCardReaderManager smartCardReaderManager =
        new SmartCardReaderManager(context, usbManager, readers);

// get the status of the smart card
// prompts the user for permissions and tries to connect to the card reader
// when reader is connected, tries to connect to the card in the reader
smartCardReaderManager.status()
        .doOnNext(status -> {
            switch (status) {
                case IDLE:
                    // no reader found
                    break;
                case READER_DETECTED:
                    // reader found, user should insert card into the reader
                    break;
                case CARD_DETECTED:
                    // card found in the reader, ready to communicate with the card
                    break;
            }
        });

// check whether the device is supported by readers in the manager
UsbDevice usbDevice = ...;
boolean supports = smartCardReaderManager.supports(usbDevice);

Example usage with id-card-lib showing personal data from the connected card

SmartCardReaderManager smartCardReaderManager = null;

Observable<Optional<PersonalData>> personalDataObservable = smartCardReaderManager.status()
        .observeOn(Schedulers.io())
        .map(status -> {
            if (status.equals(SmartCardReaderStatus.CARD_DETECTED)) {
                return Optional.fromNullable(smartCardReaderManager.connectedReader());
            } else {
                return Optional.<SmartCardReader>absent();
            }
        })
        .map(smartCardReader -> {
            if (smartCardReader.isPresent()) {
                return Optional.of(Token.create(smartCardReader.get()));
            } else {
                return Optional.<Token>absent();
            }
        })
        .map(token -> {
            if (token.isPresent()) {
                return Optional.of(token.get().personalData());
            } else {
                return Optional.absent();
            }
        });

Disposable personalDataDisposable = personalDataObservable
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(personalData -> {
            if (personalData.isPresent()) {
                // show personal data
            } else {
                // show info how to connect reader and card
            }
        });