You Are Here:

Community: Wiki

This page was last modified on 15 June 2009, at 05:19.

Simple NFCIP peer to peer chat

From Forum Nokia Wiki

Example of NFCIP P2P communication in Nokia 6131 NFC. User can exchange messages with other phones by touching them. By default MIDlet is in reading state and when user wants to write something MIDlet goes to writing state.

Note that NFCIPConnection class used in this example is proprietary implementation for Nokia 6131 NFC so this example might not work in any other phone.

package com.nokia.nfc.sample.app;
 
import java.io.IOException;
 
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
 
import com.nokia.nfc.p2p.NFCIPConnection;
 
/* Example of P2P communication in Nokia 6131 NFC. User can exchange messages
* with other phones by touching them. By default MIDlet is in reading state and
* when user wants to write something MIDlet goes to writing state.
*/

public class P2PChat extends MIDlet implements Runnable, CommandListener {
 
// Initiator URL, used to make the connection to other phone
private static final String INITIATOR_URL="nfc:rf;type=nfcip;mode=initiator";
// Target URL, used to listen for incoming connections
private static final String TARGET_URL = "nfc:rf;type=nfcip;mode=target";
 
// UI elements
private Display display;
private Form form;
private TextBox writeTb;
// Commands
private Command writeCmd;
private Command cancelCmd;
private Command exitCmd;
 
// Is the MIDlet in reading or writing state
boolean reading = true;
 
// MIDlet UI initialization and starting the thread
protected void startApp() throws MIDletStateChangeException {
 
// Initialize UI
display = Display.getDisplay(this);
form = new Form("Form");
writeCmd = new Command("Write", Command.ITEM, 1);
cancelCmd = new Command("Cancel", Command.CANCEL, 1);
exitCmd = new Command("Exit", Command.EXIT, 1);
form.addCommand(writeCmd);
form.addCommand(exitCmd);
form.setCommandListener(this);
display.setCurrent(form);
 
// Start the thread
Thread thread = new Thread(this);
thread.start();
 
}
 
// Thread to exchange data between phones
public void run() {
NFCIPConnection conn = null;
// Reading state
if (reading) {
// Read while in reading state
while (reading) {
try {
// Start waiting for other phone to initiate connection,
// blocks until connection made or another NFCIP connection
// open called
conn = (NFCIPConnection) Connector.open(TARGET_URL);
// Receive data from connection
byte[] data = conn.receive();
// Send empty message to connection
conn.send(null);
// Append received string to form
form.append(">> " + new String(data) + "\n");
// Close connection
conn.close();
} catch (Exception ex) {
try {
if (conn != null) {
conn.close();
}
} catch (IOException e) {
}
}
}
// Writing state
} else {
// Write while in writing state. If writing fails MIDlet will do the
// writing loop until it succeeds or changed back to reading state.
while (!reading) {
try {
// Try to connect another phone, blocks until connection
// made or another NFCIP connection open called
conn = (NFCIPConnection) Connector.open(INITIATOR_URL);
// Send string from TextBox to connection
conn.send(writeTb.getString().getBytes());
// Receive reply
conn.receive();
// Append sent message to form
form.append("<< " + writeTb.getString() + "\n");
// Close connection
conn.close();
display.setCurrent(form);
read();
} catch (Exception ex) {
try {
if (conn != null) {
conn.close();
}
} catch (IOException e) {
}
}
}
}
}
 
public void commandAction(Command c, Displayable d) {
if (c == writeCmd && d == form) {
writeTb();
} else if (c == writeCmd && d == writeTb) {
write();
} else if (c == cancelCmd) {
read();
} else if (c == exitCmd) {
notifyDestroyed();
}
}
 
// Go to read state
private void read() {
reading = true;
display.setCurrent(form);
// Start new thread that kills the current connection
Thread thread = new Thread(this);
thread.start();
}
 
// Display TextBox
private void writeTb() {
writeTb = new TextBox("Text to write", null, 255, TextField.ANY);
writeTb.addCommand(writeCmd);
writeTb.addCommand(cancelCmd);
writeTb.setCommandListener(this);
display.setCurrent(writeTb);
}
 
// Go to write state
private void write() {
reading = false;
// Show alert
Alert a = new Alert("Writing", "Touch to write", null, AlertType.INFO);
a.addCommand(cancelCmd);
a.setTimeout(Alert.FOREVER);
a.setCommandListener(this);
display.setCurrent(a);
// Start new thread that kills the current connection
Thread thread = new Thread(this);
thread.start();
}
 
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
}
 
protected void pauseApp() {
}
 
}

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2010 Nokia 
RDF Facets: qdcZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fHowE5ftoE5fuseE5fE52E44ebugX qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxHowE20toE20useE20E52E44ebugE20E2dE20ForumE20NokiaE20WikiX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfnTypeZCommunityContentQ qdcZtypeQUqfnTypeZE52esourceQ qdcZtypeQUqfnTypeZWebpageQ qdcZtypeQUqfnTypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2fX qfnZtypeQUqfnTypeZCommunityContentQ qfnZtypeQUqfnTypeZE52esourceQ qfnZtypeQUqfnTypeZWebpageQ qfnZtypeQUqfnTypeZWikiContentQ qfnZupdatedQDx2008E2d10E2d03X qmarsZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfnTypeZCommunityContentQ qrdfZtypeQUqfnTypeZE52esourceQ qrdfZtypeQUqfnTypeZWebpageQ qrdfZtypeQUqfnTypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ
User Rating: qfnZuserE5FratingQNx5E2E0000X