Search in sources :

Example 21 with NFCException

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

Example 22 with NFCException

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

the class GameScreen method gameWon.

private void gameWon() {
    game_state.lockBoard();
    // we may already be listening so remove the NDEF listener in anticipation of adding it
    proto.stopListeningForMessages();
    // again shortly
    setStatusMessage(Constants.WINNER_MESSAGE, true);
    int[] win_line = game_state.getWinningLine(_symbol);
    if (_symbol == Constants.PLAYER_SYMBOL_CROSS) {
        win_line_bmp = BitmapFactory.addStateIndicator(cross_unfocused, Constants.WIN_LINE_COLOUR);
    } else {
        win_line_bmp = BitmapFactory.addStateIndicator(nought_unfocused, Constants.WIN_LINE_COLOUR);
    }
    for (int i = 0; i < 3; i++) {
        tiles[win_line[i]].setImage(win_line_bmp);
    }
    game_state.setGame_over(true);
    my_bid = new ProtocolMessageMasterBid();
    try {
        proto.sendMasterBid(my_bid);
        proto.listenForMessages();
    } catch (NFCException e) {
        Utilities.log("XXXX " + e.getClass().getName() + ":" + e.getMessage());
        setStatusMessage("It didn't work, please try again");
    }
    Utilities.popupMessage("You have won!");
}
Also used : ProtocolMessageMasterBid(nfc.sample.tictactoe.protocol.ProtocolMessageMasterBid) NFCException(net.rim.device.api.io.nfc.NFCException)

Example 23 with NFCException

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

the class StartGameScreen method gameMessage.

public void gameMessage(ProtocolMessage message) {
    if (message instanceof ProtocolMessageMasterBid) {
        ProtocolMessageMasterBid other_bid = (ProtocolMessageMasterBid) message;
        int device_status = 0;
        boolean initialised = false;
        if (my_bid.getBid() > other_bid.getBid()) {
            Utilities.log("XXXX won the bid: player 1");
            device_status = Constants.PLAYER_1;
            initialised = true;
        } else {
            if (my_bid.getBid() < other_bid.getBid()) {
                Utilities.log("XXXX lost the bid: player 2");
                device_status = Constants.PLAYER_2;
                initialised = true;
            } else {
                Utilities.popupMessage("It didn't work, try again!");
            }
        }
        if (initialised) {
            try {
                proto.disableMessaging();
            } catch (NFCException e) {
                Utilities.log("XXXX StartGameScreen:" + e.getClass().getName() + ":" + e.getMessage());
                Utilities.popupMessage("We had a problem, please try again");
            }
            final int player_no = device_status;
            final MainScreen next_screen;
            if (player_no == Constants.PLAYER_2) {
                next_screen = GameScreen.getInstance(player_no);
            } else {
                next_screen = new SymbolSelectionScreen();
            }
            UiApplication.getUiApplication().invokeLater(new Runnable() {

                public void run() {
                    UiApplication.getUiApplication().pushScreen(next_screen);
                }
            });
        }
    }
}
Also used : ProtocolMessageMasterBid(nfc.sample.tictactoe.protocol.ProtocolMessageMasterBid) NFCException(net.rim.device.api.io.nfc.NFCException) MainScreen(net.rim.device.api.ui.container.MainScreen)

Example 24 with NFCException

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

the class NfcSender method buildNDEFMessages.

public NDEFMessage[] buildNDEFMessages() {
    Utilities.log("XXXX NfcSender sending ProtocolMessage:" + _message);
    NDEFMessage[] ndef_messages = null;
    NDEFMessage game_message;
    try {
        byte[] payload = _message.marshall();
        Utilities.log("XXXX NfcSender sending payload 0x" + ByteArrayUtilities.byteArrayToHex(payload));
        game_message = NDEFMessageUtils.createExternalTypeMessage(Constants.SNEP_DOMAIN, Constants.SNEP_TYPE, payload);
        ndef_messages = new NDEFMessage[] { game_message };
    } catch (NFCException e) {
        Utilities.log("XXXX NfcSender:" + e.getClass().getName() + ":" + e.getMessage());
    }
    return ndef_messages;
}
Also used : NFCException(net.rim.device.api.io.nfc.NFCException) NDEFMessage(net.rim.device.api.io.nfc.ndef.NDEFMessage)

Aggregations

NFCException (net.rim.device.api.io.nfc.NFCException)24 ReaderWriterManager (net.rim.device.api.io.nfc.readerwriter.ReaderWriterManager)8 ProtocolMessageMasterBid (nfc.sample.tictactoe.protocol.ProtocolMessageMasterBid)5 NDEFMessage (net.rim.device.api.io.nfc.ndef.NDEFMessage)4 IOException (java.io.IOException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 NDEFRecord (net.rim.device.api.io.nfc.ndef.NDEFRecord)2 SecureElement (net.rim.device.api.io.nfc.se.SecureElement)2 SecureElementManager (net.rim.device.api.io.nfc.se.SecureElementManager)2 MainScreen (net.rim.device.api.ui.container.MainScreen)2 VirtualISO14443Part4TypeATarget (net.rim.device.api.io.nfc.emulation.VirtualISO14443Part4TypeATarget)1 BadFormatException (net.rim.device.api.io.nfc.ndef.BadFormatException)1 NDEFTagConnection (net.rim.device.api.io.nfc.ndef.NDEFTagConnection)1 SmartPosterRecord (net.rim.device.api.io.nfc.ndef.rtd.SmartPosterRecord)1 Settings (nfc.sample.nfctransaction.Settings)1 CardEmulation (nfc.sample.nfctransaction.nfc.CardEmulation)1 NfcTransScreen (nfc.sample.nfctransaction.ui.NfcTransScreen)1 GameProtocol (nfc.sample.tictactoe.protocol.GameProtocol)1 ProtocolMessageResetBidState (nfc.sample.tictactoe.protocol.ProtocolMessageResetBidState)1 ProtocolMessageTurnOver (nfc.sample.tictactoe.protocol.ProtocolMessageTurnOver)1