Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Exemplo de como executar e detectar NFC PushRegistry

From Wiki
Jump to: navigation, search
Article Metadata

Compatibilidade
Plataforma(s):
Series 40

Artigo
Tradução:
Por valderind4
Última alteração feita por lpvalente em 25 Jan 2014

Este artigo é um exemplo de como executar e registrar um MIDlet utilizando NFC push registry. Este exemplo também mostra como capturar o evento relacionado com a execução do MIDlet. Note que o push registration pode ser feito no arquivo JAD.

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");
}
}
This page was last modified on 25 January 2014, at 18:15.
212 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×