Search in sources :

Example 1 with NfcTransScreen

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

the class NfcTransHandlerApp method main.

public static void main(String[] args) {
    NfcTransHandlerApp app = new NfcTransHandlerApp();
    Utilities.initLogging();
    Utilities.log("XXXX " + pid + ":" + Thread.currentThread().getName() + " NfcTransactionHandler V" + Constants.VERSION + " has registered with EventLogger");
    pid = ApplicationManager.getApplicationManager().getProcessId(ApplicationDescriptor.currentApplicationDescriptor());
    Utilities.log("XXXX " + pid + ":" + Thread.currentThread().getName() + " Launching with PID = " + pid);
    Settings settings = Settings.getInstance();
    Utilities.log("XXXX " + pid + ":" + Thread.currentThread().getName() + " Initial settings:" + settings.toString());
    if (args.length > 0) {
        dumpArgs(args);
        if (args[0].equals("auto")) {
            Utilities.log("XXXX " + pid + ":" + Thread.currentThread().getName() + " auto starting NfcTransactionHandler");
            Utilities.setUiNotRunningIndication();
            // switch on the backlight purely to ensure that the application permissions check/prompt associated with this API
            // occurs during automatic start up rather than the first time an NFC transaction is handled as this would disrupt the
            // user experience.
            Backlight.enable(true, 10);
            // Essential that the SIM is unlocked (PIN) and initialised before we can proceed to use JSR177
            waitForSimInitialisation();
            CardEmulation.registerTransactionListener(app);
            Utilities.log("XXXX " + pid + ":" + Thread.currentThread().getName() + " TransactionListener is exiting and will be restarted when a transaction occurs");
        } else {
            if (args[0].equals("seTransactionListener")) {
                Utilities.log("XXXX " + pid + ":" + Thread.currentThread().getName() + " launched by system to handle NFC transaction");
                transactionLaunch = true;
                app.enterEventDispatcher();
            }
        }
    } else {
        Utilities.log("XXXX " + pid + ":" + Thread.currentThread().getName() + " launched with no arguments");
        app.setRunning_in_background(false);
        NfcTransScreen screen = NfcTransScreen.getInstance();
        app.pushScreen(screen);
        screen.startRtsMonitorThread();
        screen.init();
        UiApplication.getApplication().requestForeground();
        Utilities.log("XXXX " + pid + ":" + Thread.currentThread().getName() + " entering event dispatcher");
        app.enterEventDispatcher();
    }
}
Also used : NfcTransScreen(nfc.sample.nfctransaction.ui.NfcTransScreen)

Example 2 with NfcTransScreen

use of nfc.sample.nfctransaction.ui.NfcTransScreen 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 3 with NfcTransScreen

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

the class ClearCommand method execute.

public void execute(ReadOnlyCommandMetadata metadata, Object context) {
    synchronized (UiApplication.getUiApplication().getEventLock()) {
        NfcTransScreen screen = NfcTransScreen.getInstance();
        screen.clearLastAIDs();
        Utilities.log("XXXX " + Thread.currentThread().getName() + " AID field cleared");
    }
}
Also used : NfcTransScreen(nfc.sample.nfctransaction.ui.NfcTransScreen)

Example 4 with NfcTransScreen

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

the class ToggleCeCommand method execute.

public void execute(ReadOnlyCommandMetadata metadata, Object context) {
    NfcTransScreen screen = NfcTransScreen.getInstance();
    Utilities.log("XXXX " + Thread.currentThread().getName() + " toggling card emulation");
    boolean nfc_enabled = false;
    try {
        nfc_enabled = nfc_service.isNfcEnabled();
        screen.setNfcLed(Utilities.ledBooleanToInt(nfc_enabled));
    } catch (NFCException e) {
        Utilities.log("XXXX " + Thread.currentThread().getName() + " exception when establishing NFC service status: " + e.getClass().getName() + ":" + e.getMessage());
        screen.setUserMessage("Failed to establish NFC service status");
        screen.setNfcLed(Constants.LED_ERR);
        return;
    }
    if (!nfc_enabled) {
        Utilities.log("XXXX " + Thread.currentThread().getName() + " NFC seems to be OFF so prompting user to enable it");
        screen.setUserMessage("NFC is off");
        try {
            nfc_service.promptToEnableNFC();
            screen.setCeButtonState(ce.isCeEnabled(SecureElement.BATTERY_ON_MODE));
        } catch (NFCException e) {
            Utilities.log("XXXX " + Thread.currentThread().getName() + " exception when prompting to enable NFC: " + e.getClass().getName() + ":" + e.getMessage());
            screen.setUserMessage("Failed when prompting to enable NFC");
        }
        return;
    }
    boolean ce_enabled = false;
    try {
        ce_enabled = ce.isCeEnabled(SecureElement.BATTERY_ON_MODE);
    } catch (NFCException e1) {
        Utilities.log("XXXX " + Thread.currentThread().getName() + " exception when checking CE status: " + e1.getClass().getName() + ":" + e1.getMessage());
        screen.setUserMessage("Error: could not establish CE status");
        screen.setCemLed(Constants.LED_ERR);
        return;
    }
    if (!ce_enabled) {
        try {
            if (nfc_service.isNfcEnabled()) {
                Utilities.log("XXXX " + Thread.currentThread().getName() + " NFC is ON so setting technology types");
                // Route ISO14443x to the Secure Element
                // for battery on, device on mode
                ce_enabled = ce.setRoutingOn(ce.getCurrentTechnologyTypes());
                if (ce_enabled) {
                    screen.setCeButtonState(true);
                    screen.setSelectButtonState(true);
                    screen.setUserMessage("Card emulation enabled OK");
                    screen.setCemLed(Constants.LED_ON);
                } else {
                    screen.setUserMessage("Card emulation not enabled");
                }
            } else {
                Utilities.log("XXXX " + Thread.currentThread().getName() + " NFC seems to be OFF so prompting user to enable it");
                screen.setUserMessage("NFC is off");
                nfc_service.promptToEnableNFC();
            }
        } catch (NFCException e) {
            Utilities.log("XXXX " + Thread.currentThread().getName() + " exception when setting SE routing: " + e.getClass().getName() + ":" + e.getMessage());
            screen.setUserMessage("Failed to set NFC routing");
            return;
        }
    } else {
        try {
            // Remove routing
            Utilities.log("XXXX " + Thread.currentThread().getName() + " NFC removing NFC SIM SE routing");
            ce_enabled = ce.setRoutingOff();
            if (!ce_enabled) {
                screen.setCeButtonState(false);
                screen.setSelectButtonState(false);
                screen.setUserMessage("Card emulation disabled OK");
                screen.setCemLed(Constants.LED_OFF);
            } else {
                screen.setUserMessage("Card emulation not disabled");
            }
        } catch (NFCException e) {
            Utilities.log("XXXX " + Thread.currentThread().getName() + " exception when setting SE routing: " + e.getClass().getName() + ":" + e.getMessage());
            screen.setUserMessage("Failed to set NFC routing");
            return;
        }
    }
}
Also used : NFCException(net.rim.device.api.io.nfc.NFCException) NfcTransScreen(nfc.sample.nfctransaction.ui.NfcTransScreen)

Aggregations

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