Search in sources :

Example 1 with NFCException

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

the class NdefListenerManager method registerListener.

/*
     * This registers with the NFC Reader Writer Manager indicating our interest in receiving notification when Smart Tags come
     * within range of the NFC antenna
     */
public void registerListener(NfcReadNdefSmartTagListener listener) {
    ReaderWriterManager nfcManager;
    Utilities.log("XXXX NfcReadNdefSmartTag about to add NDEF Message Listener");
    try {
        nfcManager = ReaderWriterManager.getInstance();
        /*
             * An NDEF record always contains:
             * 
             * * 3-bit TNF (Type Name Format) field: Indicates how to
             *   interpret the type field 
             * * Variable length type: Describes
             *   the record format 
             * * Variable length ID: A unique identifier
             *   for the record 
             * * Variable length payload: The actual data
             *   payload
             * 
             * The underlying record representation may be chunked across
             * several NDEF records when the payload is large.
             * 
             * NDEFRecord.TNF_WELL_KNOWN - indicates that we are interested
             * in NDEF records of Type Name Format (TNF): "WELL_KNOWN"
             * 
             * TNF_WELL_KNOWN included NFC RTD (Record Type Definitions)
             * types such as RTD_TEXT and RTD_URI meaning that they will
             * contain URIs or TEXT suitably encoded according to the NFC
             * RTD specification
             * 
             * "Sp" identifies the record type as Smart Poster - so we're
             * only interested in being notified of Smart Poster records
             * that contain plain text or URIs
             * 
             * The final parameter "true" identifies that the application
             * will be auto-started if it is not currently running when a
             * smart tag is brought within range of the NFC antenna.
             * 
             * This setting is persistent across device power resets.
             * 
             * When the application is restarted automatically on
             * presentation of a suitable smart tag to the device the
             * application will be re-started but it is necessary to
             * re-establish the NDEFMessageListener.
             * 
             * Upon registering the listener again the tag that caused the
             * application to be re-started is immediately presented to the
             * listener making what is really a two stage process seem
             * seamless.
             * 
             * It is important to re-start the listener within about 15 seconds
             * of the application being autostarted. Failure to do so will
             * result in the queued message being lost.
             * 
             * There is also no way to determine if the application was 
             * autostarted should you wish to automatically restart the 
             * listener in this case. Of course you could always leave your
             * own tracks in the sand using the Persistent Store.
             * 
             * This behaviour suggests that using the autostart option whilst
             * managing the setting up of the listener via the UI is a combination
             * that can lead to unexpected behaviour.
             * 
             */
        nfcManager.addNDEFMessageListener(listener, NDEFRecord.TNF_WELL_KNOWN, "Sp", true);
        Utilities.log("XXXX NfcReadNdefSmartTag add NDEF Message Listener success");
        rts.replace(Constants.LISTENER_STATE_TOKEN, new Boolean(true));
        ListenerControlScreen.getInstance().setLed();
    } catch (NFCException e) {
        e.printStackTrace();
        Utilities.log("XXXX NfcReadNdefSmartTag NFCException on register");
    }
}
Also used : NFCException(net.rim.device.api.io.nfc.NFCException) ReaderWriterManager(net.rim.device.api.io.nfc.readerwriter.ReaderWriterManager)

Example 2 with NFCException

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

the class NdefListenerManager method unRegisterListener.

/*
     * This unregisters with the NFC Reader Writer Manager We no longer want to be notified of Smart Tag events
     */
public void unRegisterListener() {
    ReaderWriterManager nfcManager;
    Utilities.log("XXXX NfcReadNdefSmartTag about to remove NDEF Message Listener");
    try {
        nfcManager = ReaderWriterManager.getInstance();
        /*
             * This is the converse of the call to register our interest in tags of a certain type. It should identify the same
             * TNF and record type as when it was registered.
             */
        nfcManager.removeNDEFMessageListener(NDEFRecord.TNF_WELL_KNOWN, "Sp");
        Utilities.log("XXXX NfcReadNdefSmartTag remove NDEF Message Listener success");
        rts.replace(Constants.LISTENER_STATE_TOKEN, new Boolean(false));
        ListenerControlScreen.getInstance().setLed();
    } catch (NFCException e) {
        e.printStackTrace();
        Utilities.log("XXXX NfcReadNdefSmartTag NFCException on unregister");
    }
}
Also used : NFCException(net.rim.device.api.io.nfc.NFCException) ReaderWriterManager(net.rim.device.api.io.nfc.readerwriter.ReaderWriterManager)

Example 3 with NFCException

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

the class NfcReadNdefSmartTagUpdate method run.

/*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     * 
     * This is the runnable unit of work that was scheduled from the NDEF Message handler
     */
public void run() {
    NDEFRecord[] records = _message.getRecords();
    try {
        Utilities.log("XXXX using helper classes to parse NDEF message");
        SmartPosterRecord sp = new SmartPosterRecord(records[0]);
        String uri = sp.getUri();
        String id = sp.getId();
        String mime = sp.getMimeType();
        int size = sp.getSize();
        String type = sp.getType();
        byte[] payload = sp.getPayload();
        //            _screen.logEvent("id="+id);
        _screen.logEvent("type=" + type);
        _screen.logEvent("URI=" + uri);
        //            _screen.logEvent("MIME="+mime);
        //            _screen.logEvent("size="+size);
        _screen.logEvent("Payload size=" + payload.length);
    } catch (NFCException e) {
        Utilities.log("XXXX NFCException using helper classes to parse NDEF message:" + e.getMessage());
    }
}
Also used : NDEFRecord(net.rim.device.api.io.nfc.ndef.NDEFRecord) NFCException(net.rim.device.api.io.nfc.NFCException) SmartPosterRecord(net.rim.device.api.io.nfc.ndef.rtd.SmartPosterRecord)

Example 4 with NFCException

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

the class NfcSnepResponderMsgBuilder method buildNDEFMessages.

public NDEFMessage[] buildNDEFMessages() {
    Utilities.log("XXXX NfcSnepResponder Received Push Request");
    _screen.logEvent("rcvd:Received Push Request");
    NDEFMessage[] listOfNdefMessages = null;
    NDEFMessage myNdefMessage;
    try {
        Utilities.log("XXXX NfcSnepResponder Constructing Media Type: " + Constants.VCARD_MIME_TYPE);
        _screen.logEvent("send:Constructing Media Type: " + Constants.VCARD_MIME_TYPE);
        Utilities.log("XXXX NfcSnepResponder Constructing vCard Media Type NDEF Message: " + Constants.VCARD_DATA);
        _screen.logEvent("send:Constructing vCard Media Type NDEF Message: " + Constants.VCARD_DATA);
        myNdefMessage = NDEFMessageUtils.createMediaTypeNDEFMessage(Constants.VCARD_MIME_TYPE, Constants.VCARD_DATA.getBytes("US-ASCII"));
        listOfNdefMessages = new NDEFMessage[] { myNdefMessage };
    } catch (NFCException e) {
        e.printStackTrace();
        Utilities.log("XXXX NfcSnepResponder Exception on creating NDEF Message: " + e.getMessage());
        _screen.logEvent("warn:Failed to create NDEFMessage");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        Utilities.log("XXXX NfcSnepResponder Exception on conversion of vCard data: " + e.getMessage());
        _screen.logEvent("warn:Exception on conversion of vCard data");
    }
    return listOfNdefMessages;
}
Also used : NFCException(net.rim.device.api.io.nfc.NFCException) NDEFMessage(net.rim.device.api.io.nfc.ndef.NDEFMessage) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 5 with NFCException

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

the class NfcVirtTargScreen method stopDetectionListener.

protected void stopDetectionListener(NfcVirtDetectionListener detectionListener) {
    ReaderWriterManager nfcManager;
    Utilities.log("XXXX NfcVirtTarg about to remove Detection Listener");
    try {
        nfcManager = ReaderWriterManager.getInstance();
        /*
             * This is the converse of the call to register our interest in tags of a certain type.
             */
        nfcManager.removeDetectionListener(detectionListener);
        Utilities.log("XXXX NfcVirtTarg remove Detection Listener success");
        _screen.logEvent("info:Detection Listener removed");
    } catch (NFCException e) {
        e.printStackTrace();
        Utilities.log("XXXX NfcVirtTarg NFCException on unregister");
        _screen.logEvent("info:Detection Listener remove failed");
    }
}
Also used : NFCException(net.rim.device.api.io.nfc.NFCException) ReaderWriterManager(net.rim.device.api.io.nfc.readerwriter.ReaderWriterManager)

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