×
Namespaces

Variants
Actions
(Difference between revisions)

Como criar uma URL usando NFC

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot addition of Template:ArticleMetaData)
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update of Template:ArticleMetaData)
(One intermediate revision by one user not shown)
Line 12: Line 12:
 
|id= <!-- Article Id (Knowledge base articles only) -->
 
|id= <!-- Article Id (Knowledge base articles only) -->
 
|language=Lang-Portuguese
 
|language=Lang-Portuguese
|translated-by= <!-- [[User: XXXXX]] -->
+
|translated-by=[[User:Valderind4]]
|translated-from-title=<!-- Title only -->
+
|translated-from-title=NFC URLWriter
|translated-from-id= <!-- Id of translated revision -->
+
|translated-from-id=51953 <!-- automated guess -->
 
|review-by=<!-- After re-review: [[User:username]] -->
 
|review-by=<!-- After re-review: [[User:username]] -->
 
|review-timestamp=<!-- After re-review: YYYYMMDD -->
 
|review-timestamp=<!-- After re-review: YYYYMMDD -->
Line 20: Line 20:
 
|update-timestamp=<!-- After significant update: YYYYMMDD -->
 
|update-timestamp=<!-- After significant update: YYYYMMDD -->
 
|creationdate=20090906
 
|creationdate=20090906
|author=[[User:Valderind4]]
+
|author=[[User:Geri-m]]
 
}}[[Category:Java ME]]
 
}}[[Category:Java ME]]
 
[[Category:Lang-Portuguese]]
 
[[Category:Lang-Portuguese]]
 
[[Category:Code Examples]]
 
[[Category:Code Examples]]
Original em: [[NFC URLWriter]]
 
  
 
Este código mostra um MIDlet básico que escreve uma URL para uma tag/Transponder externo. (Mostrado no NFC Forum Developer Training, WIMA 2008 e NFC-Congress Hagenberg 2008)
 
Este código mostra um MIDlet básico que escreve uma URL para uma tag/Transponder externo. (Mostrado no NFC Forum Developer Training, WIMA 2008 e NFC-Congress Hagenberg 2008)
Line 201: Line 200:
 
}
 
}
 
</code>
 
</code>
 +
<!-- Translation --> [[en:NFC URLWriter]]

Revision as of 02:28, 19 December 2011

Article Metadata

Exemplo de código
Código fonte: Media:URLWriter.zip

Artigo
Tradução:
Originado de NFC URLWriter
Por valderind4
Última alteração feita por hamishwillee em 19 Dec 2011

Este código mostra um MIDlet básico que escreve uma URL para uma tag/Transponder externo. (Mostrado no NFC Forum Developer Training, WIMA 2008 e NFC-Congress Hagenberg 2008) Baixe o apropriado NetBeans projetoDownload: File:URLWriter.zip.

package at.nfcresearch.wima.examples;
 
// Packages for contactless Communcation
import java.io.IOException;
import javax.microedition.contactless.ContactlessException;
import javax.microedition.contactless.DiscoveryManager;
import javax.microedition.contactless.TargetProperties;
import javax.microedition.contactless.TargetListener;
import javax.microedition.contactless.TargetType;
 
// Packages for NDEF
import javax.microedition.contactless.ndef.NDEFRecord;
import javax.microedition.contactless.ndef.NDEFMessage;
import javax.microedition.contactless.ndef.NDEFRecordType;
import javax.microedition.contactless.ndef.NDEFTagConnection;
import javax.microedition.io.Connector;
 
 
// Packages for GUI Stuff
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
 
import javax.microedition.midlet.*;
 
public class URLWriter extends MIDlet implements TargetListener, CommandListener {
 
// Display Stuff
private Command exitCommand;
private Form form;
 
public URLWriter() {
exitCommand = new Command("Exit", Command.EXIT, 1);
form = new Form("NFC-Research.at: URL Writer");
 
form.addCommand(exitCommand);
form.setCommandListener(this);
form.append("Touch Tag to write URL");
 
// Registration of the TargetListener for external contactless
// Targets (in this RFID_TAG).
try {
DiscoveryManager dm = DiscoveryManager.getInstance();
dm.addTargetListener(this, TargetType.NDEF_TAG);
 
} catch (ContactlessException ce) {
displayAlert("Unable to register TargetListener: " + ce.toString(), AlertType.ERROR);
}
 
}
 
public void startApp() {
Display.getDisplay(this).setCurrent(form);
}
 
public void pauseApp() {
}
 
public void destroyApp(boolean unconditional) {
 
}
 
/**
* Implementation of the Call-Back Function of the TargetListener
* @param targetProperties: Array of Targets found by the Phone
*
*/

public void targetDetected(TargetProperties[] targetProperties) {
 
// in case no targets found, exit the method
if (targetProperties.length == 0) {
return;
}
 
// NDEF Connection for write Operation
NDEFTagConnection ndconn = getNDEFTAGConnection(targetProperties);
try {
// Open NDEF Connection to the first Tag found
// ndconn = (NDEFTagConnection) Connector.open(targetProperties[0].getUrl());
 
 
// Headerbyte: open Bookmark in Browser
byte[] headerByte = {0x00};
 
// Payload Text: URL as a byte-Array
byte[] urlBytes = "http://www.nfc-research.com".getBytes();
 
// Create NDEF Record
NDEFRecord r = new NDEFRecord(new NDEFRecordType(NDEFRecordType.NFC_FORUM_RTD, "urn:nfc:wkt:U"), null, null);
 
// Append Payload Manually
r.appendPayload(headerByte);
r.appendPayload(urlBytes);
 
// Create Recoard Array and Add Record
NDEFRecord[] myRecArray = new NDEFRecord[]{r};
 
// Generate a Message out of the Array
NDEFMessage myMessage = new NDEFMessage(myRecArray);
 
// Write Message to Tag
ndconn.writeNDEF(myMessage);
 
 
displayAlert("Information written", AlertType.INFO);
 
} catch (IOException io) {
displayAlert("IOException during open() or Write(): " + io.toString(), AlertType.ERROR);
} catch (ContactlessException io) {
displayAlert("ContactlessException during writeNDEF(): " + io.toString(), AlertType.ERROR);
} catch (Exception e) {
displayAlert("Exception: " + e.toString(), AlertType.ERROR);
} // in case of an exception, close connection properly
finally {
 
// close Connection again
if (ndconn != null) {
try {
ndconn.close();
} catch (IOException ex) {
displayAlert("IOException during close(): " + ex.toString(), AlertType.ERROR);
}
}
}
 
}
 
private NDEFTagConnection getNDEFTAGConnection(TargetProperties[] tProp) {
for (int j = 0; j < tProp.length; j++) {
Class[] connections = tProp[j].getConnectionNames();
if (connections != null) {
for (int i = 0; i < connections.length; i++) {
if (connections[i].getName().equals(
"javax.microedition.contactless.ndef.NDEFTagConnection")) {
try {
return (NDEFTagConnection) Connector.open(tProp[0].getUrl(connections[i]));
} catch (Exception e) {
displayAlert("Error writting NDEF: " + e.toString(), AlertType.ERROR);
}
}
}
}
}
return null;
}
 
/**
* Implementation of the Call-Back function of the CommandListener
* @param command: command key pressed
* @param displayable: associated displayable Object
*/

public void commandAction(Command command, Displayable displayable) {
if (command == exitCommand) {
DiscoveryManager dm = DiscoveryManager.getInstance();
dm.removeTargetListener(this, TargetType.NDEF_TAG);
destroyApp(false);
notifyDestroyed();
}
}
 
private void displayAlert(String error, AlertType type) {
Alert err = new Alert(form.getTitle(), error, null, type);
Display.getDisplay(this).setCurrent(err, form);
}
}
96 page views in the last 30 days.
×