Search in sources :

Example 1 with SecureElementManager

use of net.rim.device.api.io.nfc.se.SecureElementManager in project Samples-for-Java by blackberry.

the class NfcMidlet2 method removeTransactionListener.

public void removeTransactionListener() {
    SecureElementManager sem = SecureElementManager.getInstance();
    if (sem == null) {
        Utilities.log("XXXX " + Thread.currentThread().getName() + " removeTransactionListener instance is null - exiting");
        return;
    }
    Utilities.log("XXXX " + Thread.currentThread().getName() + " removeTransactionListener SecureElement instance of type SIM");
    SecureElement se = null;
    try {
        se = sem.getSecureElement(SecureElement.SIM);
    } catch (Exception e1) {
        Utilities.log("XXXX " + Thread.currentThread().getName() + " removeTransactionListener when getting SE: " + e1.getClass().getName() + ":" + e1.getMessage());
    }
    if (se == null) {
        Utilities.log("XXXX " + Thread.currentThread().getName() + " removeTransactionListener(SIM) is null - exiting");
        return;
    }
    try {
        myListener = getTransactionListener();
        Utilities.log("XXXX " + Thread.currentThread().getName() + " TransactionListener=" + myListener);
        if (myListener != null) {
            se.removeTransactionListener(myListener);
            Utilities.log("XXXX " + Thread.currentThread().getName() + " removeTransactionListener listener OK");
        }
    } catch (Exception e) {
        Utilities.log("XXXX " + Thread.currentThread().getName() + " removeTransactionListener when removing TransactionListener:" + e.getClass().getName() + ":" + e.getMessage());
    }
    Utilities.log("XXXX " + Thread.currentThread().getName() + " removed TransactionListener from RuntimeStore");
    runtimeStore.remove(TRANSACTION_LISTENER);
}
Also used : SecureElement(net.rim.device.api.io.nfc.se.SecureElement) SecureElementManager(net.rim.device.api.io.nfc.se.SecureElementManager) IOException(java.io.IOException) NFCException(net.rim.device.api.io.nfc.NFCException)

Example 2 with SecureElementManager

use of net.rim.device.api.io.nfc.se.SecureElementManager 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)

Example 3 with SecureElementManager

use of net.rim.device.api.io.nfc.se.SecureElementManager in project Samples-for-Java by blackberry.

the class NfcMidlet2 method addTransactionListener.

public void addTransactionListener() {
    SecureElementManager sem = SecureElementManager.getInstance();
    if (sem == null) {
        Utilities.log("XXXX " + Thread.currentThread().getName() + " addTransactionListener:SecureElementManager instance is null - exiting");
        return;
    }
    Utilities.log("XXXX " + Thread.currentThread().getName() + " addTransactionListener:getting SecureElement instance of type SIM");
    SecureElement se = null;
    try {
        se = sem.getSecureElement(SecureElement.SIM);
    } catch (Exception e1) {
        Utilities.log("XXXX " + Thread.currentThread().getName() + " addTransactionListener:exception when getting SE: " + e1.getClass().getName() + ":" + e1.getMessage());
    }
    if (se == null) {
        Utilities.log("XXXX " + Thread.currentThread().getName() + " addTransactionListener:SecureElement(SIM) is null - exiting");
        return;
    }
    myListener = getTransactionListener();
    try {
        Utilities.log("XXXX " + Thread.currentThread().getName() + " addTransactionListener:adding transaction listener");
        try {
            se.addTransactionListener(myListener, MY_AID);
        } catch (NFCException e) {
            if (e.getMessage().startsWith("TransactionListener has already been added") || e.getMessage().startsWith("AID has already been registered")) {
                Utilities.log("XXXX " + Thread.currentThread().getName() + " addTransactionListener:already registered - ignoring");
            }
        }
        Utilities.log("XXXX " + Thread.currentThread().getName() + " addTransactionListener:added transaction listener OK");
    } catch (Exception e) {
        if (!e.getMessage().startsWith("TransactionListener has already been added") && !e.getMessage().startsWith("AID has already been registered")) {
            Utilities.log("XXXX " + Thread.currentThread().getName() + " addTransactionListener:exception when adding transaction listener: " + e.getClass().getName() + ":" + e.getMessage());
            return;
        } else {
            Utilities.log("XXXX " + Thread.currentThread().getName() + " addTransactionListener:exception when adding transaction listener: " + e.getClass().getName() + ":" + e.getMessage());
            return;
        }
    }
    try {
        se.setTechnologyTypes(SecureElement.BATTERY_ON_MODE, TechnologyType.ISO14443B);
        Utilities.log("XXXX " + Thread.currentThread().getName() + " addTransactionListener:NFC routing established OK");
    } catch (NFCException e) {
        Utilities.log("XXXX " + Thread.currentThread().getName() + " addTransactionListener:exception when setting technology types: " + e.getClass().getName() + ":" + e.getMessage());
    }
    try {
        runtimeStore.replace(TRANSACTION_LISTENER, myListener);
    } catch (IllegalArgumentException e) {
        Utilities.log("XXXX " + Thread.currentThread().getName() + " RuntimeStore.replace exception: " + e.getClass().getName() + ":" + e.getMessage());
    }
}
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) IOException(java.io.IOException) NFCException(net.rim.device.api.io.nfc.NFCException)

Aggregations

NFCException (net.rim.device.api.io.nfc.NFCException)3 SecureElement (net.rim.device.api.io.nfc.se.SecureElement)3 SecureElementManager (net.rim.device.api.io.nfc.se.SecureElementManager)3 IOException (java.io.IOException)2 Settings (nfc.sample.nfctransaction.Settings)1