Search in sources :

Example 1 with Settings

use of nfc.sample.nfctransaction.Settings in project Samples-for-Java by blackberry.

the class Iso7816Command method execute.

public void execute(ReadOnlyCommandMetadata metadata, Object context) {
    MultiStateButtonField btn = (MultiStateButtonField) context;
    if (btn.getMsbState() == Constants.BTN_SELECT_OFF) {
        Utilities.log("XXXX " + Thread.currentThread().getName() + " ISO 7816-4 command exiting because button is in OFF state");
        return;
    }
    Settings settings = Settings.getInstance();
    NfcTransScreen screen = NfcTransScreen.getInstance();
    Utilities.log("XXXX " + Thread.currentThread().getName() + " selecting applet");
    APDUConnection apduConn = null;
    // this is an example and completely proprietary APDU included solely to demonstrate the basic use of JSR-177
    // Decode:
    // 0x80 : CLASS = proprietary
    // 0x01 : INS   = invented command! Interpretation will require the selected applet to understand this command
    // 0x00 : P1    = null
    // 0x00 : P1    = null
    byte[] command = settings.getAPDU();
    try {
        // Open an APDUConnection to our applet. This results in an ISO 7816-4 SELECT command being sent by the JSR-177 API
        String connection_string = Utilities.makeApduConnectionString(settings.getRegisteredAIDAsString());
        Utilities.log("XXXX " + Thread.currentThread().getName() + " ISO 7816-4 command: opening APDUConnection to " + connection_string);
        apduConn = (APDUConnection) Connector.open(connection_string);
        Utilities.log("XXXX " + Thread.currentThread().getName() + " ISO 7816-4 command: APDUConnection opened");
        screen.setUserMessage("Selected AID OK");
        // Send the APDU and wait for a response - expect an error here unless your applet supports the command that was sent
        Utilities.log("XXXX " + Thread.currentThread().getName() + " ISO 7816-4 command: sending APDU");
        byte[] ret = apduConn.exchangeAPDU(command);
        String resp_string = ByteArrayUtilities.byteArrayToHex(ret);
        Utilities.log("XXXX " + Thread.currentThread().getName() + " ISO 7816-4 command: response=" + resp_string);
        screen.setUserMessage("APDU response=" + resp_string);
        // Close the logical channel connection
        apduConn.close();
    } catch (Exception e) {
        Utilities.log("XXXX " + Thread.currentThread().getName() + " ISO 7816-4 command failed:" + e.getClass().getName() + ":" + e.getMessage());
        screen.setUserMessage("Error. Select failed: check log");
    }
}
Also used : NfcTransScreen(nfc.sample.nfctransaction.ui.NfcTransScreen) APDUConnection(javax.microedition.apdu.APDUConnection) MultiStateButtonField(nfc.sample.nfctransaction.ui.buttons.MultiStateButtonField) Settings(nfc.sample.nfctransaction.Settings)

Example 2 with Settings

use of nfc.sample.nfctransaction.Settings in project Samples-for-Java by blackberry.

the class CardEmulation method registerTransactionListener.

public static void registerTransactionListener(TransactionListener tl) {
    int pid = ApplicationManager.getApplicationManager().getProcessId(ApplicationDescriptor.currentApplicationDescriptor());
    Settings settings = Settings.getInstance();
    SecureElementManager sem = SecureElementManager.getInstance();
    if (sem == null) {
        Utilities.log("XXXX " + pid + ":" + Thread.currentThread().getName() + " SecureElementManager instance is null - exiting");
        System.exit(0);
    }
    SecureElement[] sec_elements = null;
    try {
        sec_elements = sem.getSecureElements();
    } catch (NFCException e2) {
        e2.printStackTrace();
    }
    if (sec_elements == null) {
        Utilities.log("XXXX " + pid + ":" + Thread.currentThread().getName() + " SecureElement[] is null - exiting");
        System.exit(0);
    }
    Utilities.log("XXXX " + pid + ":" + Thread.currentThread().getName() + " device has " + sec_elements.length + " SEs");
    Utilities.log("XXXX " + pid + ":" + Thread.currentThread().getName() + " getting SecureElement instance");
    SecureElement se = null;
    try {
        if (settings.isSimSeSelected()) {
            Utilities.log("XXXX " + pid + ":" + Thread.currentThread().getName() + " Obtaining SecureElement in SIM");
            se = sem.getSecureElement(SecureElement.SIM);
        } else {
            Utilities.log("XXXX " + pid + ":" + Thread.currentThread().getName() + " Obtaining embedded SecureElement");
            se = sem.getSecureElement(SecureElement.EMBEDDED);
        }
    } catch (NFCException e1) {
        Utilities.log("XXXX " + pid + ":" + Thread.currentThread().getName() + " exception when getting SE: " + e1.getClass().getName() + ":" + e1.getMessage());
    }
    if (se == null) {
        Utilities.log("XXXX " + pid + ":" + Thread.currentThread().getName() + " SecureElement is null - exiting");
        System.exit(0);
    }
    // register our app as a transaction listener
    try {
        Utilities.log("XXXX " + pid + ":" + Thread.currentThread().getName() + " adding transaction listener for AID:" + settings.getRegisteredAIDAsString());
        se.addTransactionListener(tl, new byte[][] { settings.getRegisteredAID() });
        _tl = tl;
    } catch (NFCException e) {
        if (!e.getMessage().startsWith("TransactionListener has already been added")) {
            Utilities.log("XXXX " + pid + ":" + Thread.currentThread().getName() + " exception when adding transaction listener: " + e.getClass().getName() + ":" + e.getMessage());
            System.exit(0);
        }
    }
}
Also used : NFCException(net.rim.device.api.io.nfc.NFCException) SecureElement(net.rim.device.api.io.nfc.se.SecureElement) SecureElementManager(net.rim.device.api.io.nfc.se.SecureElementManager) Settings(nfc.sample.nfctransaction.Settings)

Aggregations

Settings (nfc.sample.nfctransaction.Settings)2 APDUConnection (javax.microedition.apdu.APDUConnection)1 NFCException (net.rim.device.api.io.nfc.NFCException)1 SecureElement (net.rim.device.api.io.nfc.se.SecureElement)1 SecureElementManager (net.rim.device.api.io.nfc.se.SecureElementManager)1 NfcTransScreen (nfc.sample.nfctransaction.ui.NfcTransScreen)1 MultiStateButtonField (nfc.sample.nfctransaction.ui.buttons.MultiStateButtonField)1