Community: Wiki

你在这里: You Are Here: Olet tässä: Vous êtes ici: Sie befinden sich hier: Tu sei qui: 現在のページ: Você está aqui: Вы здесь: Usted está aquí:
This page was last modified 18:33, 20 May 2008.

Simple NFC PushRegistry example

From Forum Nokia Wiki

Simple Java code example of using PushRegistry in NFC devices.

package com.nokia.nfc.sample.app;
 
import javax.microedition.contactless.DiscoveryManager;
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;
 
// Simple Java code example of using PushRegistry to launch midlet.
public class PushRegistrySimpleExample extends MIDlet {
 
    private Form form;
    private TextField pushTextField;
    private TextField launchTextField;
    private DiscoveryManager dm;
 
    protected void startApp() throws MIDletStateChangeException {
 
        // Create UI
        form = new Form("Form");
        pushTextField = new TextField("Pushregistration", "", 255,
                TextField.UNEDITABLE);
        launchTextField = new TextField("Launch type", "", 255,
                TextField.UNEDITABLE);
        form.append(pushTextField);
        form.append(launchTextField);
        Display.getDisplay(this).setCurrent(form);
 
        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:pushexample")) {
                    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:pushexample",
                                "com.nokia.nfc.sample.app.PushRegistrySimpleExample",
                                "*");
                pushTextField.setString("Succeeded");
            } else {
                // If target was already registered 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());
        }
 
        // Get instance of NFC Discovery Manager
        dm = DiscoveryManager.getInstance();
 
        // Show how MIDlet was launched: By touching tag or trough
        // phones menu (touch/manual).
        String launchType = dm.getProperty("LaunchType");
        launchTextField.setString(launchType);
    }
 
    protected void pauseApp() {
    }
 
    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    }
}

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditFurlTechnocratiMagnoliaTwitter  Share this page Share this page Invite a friend Invite a friend
E-mail Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us Regional websites: Chinese Japanese © 2008 Nokia 
RDF Facets: qfnZtopicQUqfnTopicZjavaQ qfnZtypeQUqfnTypeZCommunityContentQ qfnZtypeQUqfnTypeZWebpageQ qfnZtypeQUqfnTypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX