ورود

View Full Version : سوال: دریافت SMS در J2ME



davar111
یک شنبه 09 مرداد 1390, 18:23 عصر
دوستان به شدت به این سورس نیاز دارم . واسه ارسالش مشکلی ندارم .ولی واسه دریافتش هر کدی پیدا کردم مشکل داره !:عصبانی++:

davar111
یک شنبه 09 مرداد 1390, 18:29 عصر
این سورسیه که پیدا کردم . ولی مشکل داره تو public class SMSReceive extends MIDlet و int intData = (int)data & 0xFF
لطفا کمک کنید .


/*
J2ME SMS
Source code example for receiving SMS in J2ME
*
* @(#)SMSReceive.java 1.11 04/03/22
*
* Copyright (c) 1999-2004 Sun Microsystems, Inc. All rights reserved.
* PROPRIETARY/CONFIDENTIAL
* Use is subject to license terms
*/

package example.sms;

import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.wireless.messaging.*;

import java.io.IOException;

/**
* An example MIDlet displays text from an SMS MessageConnection
*/
public class SMSReceive extends MIDlet
implements CommandListener, Runnable, MessageListener {

/** user interface command for indicating Exit request. */
Command exitCommand = new Command("Exit", Command.EXIT, 2);
/** user interface command for indicating Reply request */
Command replyCommand = new Command("Reply", Command.OK, 1);
/** user interface text box for the contents of the fetched URL. */
Alert content;
/** current display. */
Display display;
/** instance of a thread for asynchronous networking and user interface. */
Thread thread;
/** Connections detected at start up. */
String[] connections;
/** Flag to signal end of processing. */
boolean done;
/** The port on which we listen for SMS messages */
String smsPort;
/** SMS message connection for inbound text messages. */
MessageConnection smsconn = null;
/** Current message read from the network. */
Message msg;
/** Address of the message's sender */
String senderAddress;
/** Alert that is displayed when replying */
Alert sendingMessageAlert;
/** Prompts for and sends the text reply */
SMSSender sender;
/** The screen to display when we return from being paused */
Displayable resumeScreen;

/**
* Initialize the MIDlet with the current display object and
* graphical components.
*/
public SMSReceive() {
smsPort = getAppProperty("SMS-Port");

display = Display.getDisplay(this);

content = new Alert("SMS Receive");
content.setTimeout(Alert.FOREVER);
content.addCommand(exitCommand);
content.setCommandListener(this);
content.setString("Receiving...");

sendingMessageAlert = new Alert("SMS", null, null, AlertType.INFO);
sendingMessageAlert.setTimeout(5000);
sendingMessageAlert.setCommandListener(this);

sender = new SMSSender(smsPort, display, content, sendingMessageAlert);

resumeScreen = content;
}

/**
* Start creates the thread to do the MessageConnection receive
* text.
* It should return immediately to keep the dispatcher
* from hanging.
*/
public void startApp() {
// SMS connection to be read.
String smsConnection = "sms://:" + smsPort;
content.setString(smsConnection);

// Open the message connection.

// if (smsconn == null) {
try {
smsconn = (MessageConnection)Connector.open(smsConnection, Connector.READ);
// smsconn.setMessageListener(this);
} catch (Throwable t) {
content.setString(t.toString());
}
// } catch (IOException ioe) {
// content.setString(ioe.toString());
// ioe.printStackTrace();
// }
// }
/*
// Initialize the text if we were started manually.
connections = PushRegistry.listConnections(true);
if (connections == null || connections.length == 0) {
content.setString("Waiting for SMS on port " + smsPort + "...");
}
done = false;
thread = new Thread(this);
thread.start();
*/
display.setCurrent(resumeScreen);
}

/**
* Notification that a message arrived.
* @param conn the connection with messages available
*/
public void notifyIncomingMessage(MessageConnection conn) {
if (thread == null) {
done = false;
thread = new Thread(this);
thread.start();
}
}

/** Message reading thread. */
public void run() {
/** Check for sms connection. */
try {
msg = smsconn.receive();
if (msg != null) {
senderAddress = msg.getAddress();
content.setTitle("From: " + senderAddress);
if (msg instanceof TextMessage) {
content.setString(((TextMessage)msg).getPayloadTex t());
} else {
StringBuffer buf = new StringBuffer();
byte[] data = ((BinaryMessage)msg).getPayloadData();
for (int i = 0; i < data.length; i++) {
int intData = (int)data & 0xFF;
if (intData < 0x10) {
buf.append("0");
}
buf.append(Integer.toHexString(intData));
buf.append(' ');
}
content.setString(buf.toString());
}
content.addCommand(replyCommand);
display.setCurrent(content);
}
} catch (IOException e) {
// e.printStackTrace();
}
}
/**
* Pause signals the thread to stop by clearing the thread field.
* If stopped before done with the iterations it will
* be restarted from scratch later.
*/
public void pauseApp() {
done = true;
thread = null;
resumeScreen = display.getCurrent();
}

/**
* Destroy must cleanup everything. The thread is signaled
* to stop and no result is produced.
* @param unconditional true if a forced shutdown was requested
*/
public void destroyApp(boolean unconditional) {
done = true;
thread = null;
if (smsconn != null) {
try {
smsconn.close();
} catch (IOException e) {
// Ignore any errors on shutdown
}
}
}

/**
* Respond to commands, including exit
* @param c user interface command requested
* @param s screen object initiating the request
*/
public void commandAction(Command c, Displayable s) {
try {
if (c == exitCommand || c == Alert.DISMISS_COMMAND) {
destroyApp(false);
notifyDestroyed();
} else if (c == replyCommand) {
reply();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

/**
* Allow the user to reply to the received message
*/
private void reply() {
// remove the leading "sms://" for diplaying the destination address
String address = senderAddress.substring(6);
String statusMessage = "Sending message to " + address + "...";
sendingMessageAlert.setString(statusMessage);
sender.promptAndSend(senderAddress);
}
}

dr_jacky_2005
دوشنبه 10 مرداد 1390, 09:42 صبح
کلا کلاس SMSSender و مشتق شده ها ازش رو کامنت کنید.(نداریم دیگه،چه کنیم! )

واسه خط 144


int intData = (int)(data[i] & 0xFF);

davar111
چهارشنبه 12 مرداد 1390, 22:18 عصر
اینو خود generate , IDE کرده . نمیدونم درست باشه یا نه ! :ناراحت:


package example.sms;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Display;

/**
*
* @author Administrator
*/
class SMSSender {

SMSSender(String smsPort, Display display, Alert content, Alert sendingMessageAlert) {
throw new UnsupportedOperationException("Not yet implemented");
}

void promptAndSend(String senderAddress) {
throw new UnsupportedOperationException("Not yet implemented");
}

}



به شدت بهش نیاز دارم . کمک کنین :((

zacaria
جمعه 21 مرداد 1390, 17:16 عصر
سلام
برای ارسا لو دریافت از این کلاس استفاده میکنم:


import javax.microedition.io.Connector;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.wireless.messaging.BinaryMessage;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MessageListener;
import javax.wireless.messaging.TextMessage;


public class jcSMS implements MessageListener, ItemStateListener{

public static boolean recieve;
private MessageConnection clientConn;
public MessageConnection serverConn;
String address = "";
public static String msgReceived = "";

public jcSMS(String mobile){
try{
address = "sms://" + mobile + ":5000";
clientConn = (MessageConnection)Connector.open(address);
serverConn = (MessageConnection)Connector.open("sms://:5000");
// Register the listener for inbound messages.
serverConn.setMessageListener(this);
}
catch(Exception ex){
System.out.println("Connection could not be obtained");
ex.printStackTrace();
}
}

public synchronized void notifyIncomingMessage(MessageConnection conn) {
synchronized (this)
{
notify();
}
Message msg = null;
// Try reading (maybe block for) a message
try {
msg = conn.receive();
getSMS(msg);
recieve = true;
}
catch (Exception e) {
// Handle reading errors

}
}

public void itemStateChanged(Item itm) {
throw new UnsupportedOperationException("Not supported yet.");
}

public void getSMS(Message msg){
// Process the received message
if (msg instanceof TextMessage) {
TextMessage tmsg = (TextMessage)msg;
msgReceived = tmsg.getPayloadText();
}
else
{
// process received message
if (msg instanceof BinaryMessage) {
BinaryMessage bmsg = (BinaryMessage)msg;
byte[] data = bmsg.getPayloadData();
// Handle the binary message...
msgReceived = data.toString();
}
}
}

public boolean sendSMS(String payloadText){
try{
TextMessage tmsg = (TextMessage)clientConn.newMessage(MessageConnecti on.TEXT_MESSAGE);
tmsg.setAddress(address);
tmsg.setPayloadText(payloadText);
clientConn.send(tmsg);
closeConn();
}
catch (Exception e){
e.printStackTrace();
return false;
}
return true;
}

public void closeConn(){
try{clientConn.close();}catch(Exception e){}
}
}




به شدت بهش نیاز دارم . کمک کنین :(([/QUOTE]

davar111
پنج شنبه 10 شهریور 1390, 14:34 عصر
ممنون . لطف کردین دوست عزیز

dr_jacky_2005
پنج شنبه 17 شهریور 1390, 09:29 صبح
سلام
برای ارسا لو دریافت از این کلاس استفاده میکنم:


import javax.microedition.io.Connector;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.wireless.messaging.BinaryMessage;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MessageListener;
import javax.wireless.messaging.TextMessage;


public class jcSMS implements MessageListener, ItemStateListener{

public static boolean recieve;
private MessageConnection clientConn;
public MessageConnection serverConn;
String address = "";
public static String msgReceived = "";

public jcSMS(String mobile){
try{
address = "sms://" + mobile + ":5000";
clientConn = (MessageConnection)Connector.open(address);
serverConn = (MessageConnection)Connector.open("sms://:5000");
// Register the listener for inbound messages.
serverConn.setMessageListener(this);
}
catch(Exception ex){
System.out.println("Connection could not be obtained");
ex.printStackTrace();
}
}

public synchronized void notifyIncomingMessage(MessageConnection conn) {
synchronized (this)
{
notify();
}
Message msg = null;
// Try reading (maybe block for) a message
try {
msg = conn.receive();
getSMS(msg);
recieve = true;
}
catch (Exception e) {
// Handle reading errors

}
}

public void itemStateChanged(Item itm) {
throw new UnsupportedOperationException("Not supported yet.");
}

public void getSMS(Message msg){
// Process the received message
if (msg instanceof TextMessage) {
TextMessage tmsg = (TextMessage)msg;
msgReceived = tmsg.getPayloadText();
}
else
{
// process received message
if (msg instanceof BinaryMessage) {
BinaryMessage bmsg = (BinaryMessage)msg;
byte[] data = bmsg.getPayloadData();
// Handle the binary message...
msgReceived = data.toString();
}
}
}

public boolean sendSMS(String payloadText){
try{
TextMessage tmsg = (TextMessage)clientConn.newMessage(MessageConnecti on.TEXT_MESSAGE);
tmsg.setAddress(address);
tmsg.setPayloadText(payloadText);
clientConn.send(tmsg);
closeConn();
}
catch (Exception e){
e.printStackTrace();
return false;
}
return true;
}

public void closeConn(){
try{clientConn.close();}catch(Exception e){}
}
}




به شدت بهش نیاز دارم . کمک کنین :(([/QUOTE]
توی windows mobile ]ا تست کردین؟
اس ام اس رو توی برنامه میگیره؟
همونطور که میدونید برای اجرای JAVA در WM، باید یک MIDletManager نصب کنیم توی گوشی ویندوزیمون.
بهترین منیجر هم اینه:
Esmertec Jbed 20090506.2.1 by Aqrab

اگه بتونی یه تست کنی ببینی توی برنامه مسیج رو دریافت میکنید،یک دنیا ممنونتون میشم.