Categories: Code Examples | Java | NFC
This page was last modified 13:20, 3 January 2008.
NFC PushRegistry launch and detect example
From Forum Nokia Wiki
Example of registering and launching midlet using NFC push registry and getting event of the tag that launched the midlet. Note that push registration can also be done in jad file.
package com.nokia.nfc.sample.app; import javax.microedition.contactless.ContactlessException; import javax.microedition.contactless.DiscoveryManager; import javax.microedition.contactless.ndef.NDEFMessage; import javax.microedition.contactless.ndef.NDEFRecordListener; import javax.microedition.contactless.ndef.NDEFRecordType; import javax.microedition.io.PushRegistry; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.TextField; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; /* * Example of launching midlet using push registry * and getting event of the tag that launched the midlet. */ public class PushAndDetectExample extends MIDlet implements NDEFRecordListener { private Form form; private TextField pushTextField; private DiscoveryManager dm; protected void startApp() throws MIDletStateChangeException { // Create UI form = new Form("Form"); pushTextField = new TextField("Pushregistration", "", 255, TextField.UNEDITABLE); form.append(pushTextField); Display.getDisplay(this).setCurrent(form); // Register target to discovery dm = DiscoveryManager.getInstance(); try { dm.addNDEFRecordListener(this, new NDEFRecordType( NDEFRecordType.EXTERNAL_RTD, "urn:nfc:ext:yourcompany.com:pushdetectexample")); } catch (ContactlessException e) { // Catch ContactlessException } try { // Make list of connections that are already registered String[] regConns = PushRegistry.listConnections(false); // Boolean to tell if wanted TargetType is already registered boolean registered = false; // Go trough list to see if wanted TargetType is already registered for (int i = 0; i < regConns.length; i++) { if (regConns[i] .equals("ndef:external_rtd?name=urn:nfc:ext:yourcompany.com:pushdetectexample")) { registered = true; } } // If TargetType is not registered - register it if (!registered) { // Register this MIDlet to be launched when any tag with right // TargetType is touched PushRegistry .registerConnection( "ndef:external_rtd?name=urn:nfc:ext:yourcompany.com:pushdetectexample", "com.nokia.nfc.sample.app.PushAndDetectExample", "*"); pushTextField.setString("Succeeded"); } else { // If target was alredy regisered write that on screen pushTextField.setString("Connection already registered"); } } catch (Exception e) { // In case of exception write message on screen pushTextField.setString("Exception: " + e.getMessage()); } } protected void pauseApp() { // TODO Auto-generated method stub } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { // TODO Auto-generated method stub } public void recordDetected(NDEFMessage ndefMessage) { // Write notification on the form when NDEF message is detected form.append("Record detected\n"); } }
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| NFC APIs specification | tenthor | Symbian Networking & Messaging | 2 | 2007-01-09 23:31 |
| Capture outgoing sms using PushRegistry | rl0pes | Mobile Java Networking & Messaging & Security | 2 | 2006-03-20 16:38 |
| PushRegistry - Connection permanently blocked after firmware update | biobronch | Mobile Java General | 9 | 2008-01-18 13:29 |
| 6126: System.getProperty("internal.se.url") returns NULL | dpomeroy | Near Field Communication | 1 | 2007-10-04 16:17 |
| NFC Shell | mpcoloma | General Discussion | 1 | 2007-01-31 10:34 |
