You Are Here:

Community: Wiki

This page was last modified on 2 October 2008, at 18:24.

Use sms as data bearer

From Forum Nokia Wiki

Moving data over GPRS or other data connections is typically the least cost method. But sometimes, especially in emerging markets, users may not have access to a data service.

Here is the full source code for a Midlet that supports user-generated localization for an application to connect people to community radio stations.

Video demo and discussion about the full app here.

When a typical data bearer is not present, the Midlet falls back to SMS as the bearer. This code snippet shows how.

/*
*
* Written by Gergely Herenyi e-mail: gergely.herenyi at nokia.com
*/

 
package com.nokia.radiostation.connectivity;
 
import com.nokia.radiostation.languages.Language;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.wireless.messaging.BinaryMessage;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MessageListener;
import javax.wireless.messaging.TextMessage;
 
/**
* SMS handling class
*/

public class SMSBearer implements MessageListener {
 
/** Message connection for sending/receiving SMSs */
MessageConnection messconn;
/** Reference to the parent connection object*/
Connection parent;
/** The maximum data an SMS can transfer in bytes */
public static final int SMS_SIZE = 133;
 
/**
* Constructor for SMSBearer
*
* @param parent the parent connection object
*/

public SMSBearer(Connection parent) {
this.parent = parent;
register();
}
 
/**
* Registers for incoming messaged on port 1976
*/

public void register() {
try {
messconn = (MessageConnection) Connector.open("sms://:1976");
messconn.setMessageListener(this);
} catch (IOException e) {
e.printStackTrace();
}
}
 
/**
* Handles incoming messages
*
* @param conn MessageConnection where the message arrives
*/

public void notifyIncomingMessage(MessageConnection conn) {
if (conn == messconn) {
try {
TextMessage msg = (TextMessage) messconn.receive();
String address = msg.getAddress();
if (address.startsWith("sms://")) {
address = address.substring(6);
}
if (address.lastIndexOf(':') != -1) {
address = address.substring(0, address.lastIndexOf(':'));
}
String request = msg.getPayloadText();
processRequest(request, address);
} catch (IOException e) {
e.printStackTrace();
}
}
}
 
/**
* Processes and replies to a request
*
* @param request the request, which is received
* @param number the number where the reply should be sent to
*/

public void processRequest(String request, String number) {
if (request.startsWith("RADIO")) {
sendSMS(number, parent.parent.stations[parent.parent.selectedStation].toByte());
} else if (request.startsWith("GET_LANGUAGES")) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);
try {
os.writeUTF(Language.getLanguages()[Language.getSelectedLanguage()]);
} catch (IOException e) {
e.printStackTrace();
}
sendSMS(number, baos.toByteArray());
} else if (request.startsWith("GET_LANGUAGE_")) {
sendSMS(number, Language.binaryLanguage);
}
}
 
/**
* Sends an SMS
*
* @param number where the SMS is sent to
* @param data the message to be sent
*/

public void sendSMS(String number, byte[] data) {
String port = "1976";
int parts = data.length / (SMS_SIZE - 2) + 1;
int offset = 0;
for (int i = 0; i < parts; i++) {
int length = Math.min(data.length - offset, SMS_SIZE - 2);
byte[] dataToSend = new byte[2 + length];
dataToSend[0] = (byte) i;
dataToSend[1] = (byte) parts;
 
System.arraycopy(data, offset, dataToSend, 2, length);
offset += length;
try {
MessageConnection msgConn = (MessageConnection) Connector.open("sms://" + number + ":" + port);
BinaryMessage msg = (BinaryMessage) msgConn.newMessage(MessageConnection.BINARY_MESSAGE, "sms://" + number + ":" + port);
msg.setPayloadData(dataToSend);
msgConn.send(msg);
msgConn.close();
} catch (IOException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
 
}

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 © 2009 Nokia 
RDF Facets: qdcZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fTrainE5fforE5fE45mergingE5fMktsE5fChallengeX qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxTrainE20forE20E45mergingE20MktsE20ChallengeE20E2dE20ForumE20NokiaE20WikiX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2fX qfnZtopicQUqfnTopicZconnectivityQRqdcZtypeQUqrdfsZE52esourceQRqmarsZrelevanceQNx100X qfnZtopicQUqfnTopicZjavaQRqdcZtypeQUqrdfsZE52esourceQRqmarsZrelevanceQNx100X qfnZtopicQUqfnTopicZmessagingQRqmarsZrelevanceQNx100X qfnZtopicQUqfnTopicZsmsQRqdcZtypeQUqrdfsZE52esourceQRqmarsZrelevanceQNx100X qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qfnZupdatedQDx2008E2d10E2d03X qfnZuserE5ftagQSxconnectivityX qfnZuserE5ftagQSxjavaX qfnZuserE5ftagQSxmessagingX qfnZuserE5ftagQSxsmsX qmarsZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ
User Rating: qfnZuserE5FratingQNx5E2E0000X