如何使用java comm从gsm读取短信?
我找到了使用 java comm 发送短信的免费源项目: http://code.google.com/ p/find-ur-pal/source/browse/src/?r=21
发送短信的功能如下:
public void run(){
boolean timeOut=false;
long startTime=(new Date()).getTime();
while ((step <7) && (!timeOut)){
// log(""+((new Date()).getTime() - startTime);
//check where we are in specified delay
timeOut=((new Date()).getTime() - startTime)>delay;
//if atz does not work, type to send cntrlZ and retry, in case a message was stuck
if (timeOut && (step==1)) {
step=-1;
mySerial.send( ""+cntrlZ);
}
//read incoming string
String result= mySerial.getIncommingString() ;
// log ("<- "+result+"\n--------");
int expectedResult=-1;
try{
//log ("Step:"+step);
switch (step){
case 0:
mySerial.send("atz");
delay=LONG;
startTime=(new Date()).getTime();
break;
case 1:
delay=STANDARD;
mySerial.send("ath0");
startTime=(new Date()).getTime();
break;
case 2:
expectedResult=result.indexOf("OK");
//log ("received ok ="+expectedResult);
if (expectedResult>-1){
mySerial.send("at+cmgf=1");
startTime=(new Date()).getTime();
}else{
step=step-1;
}
break;
case 3:
expectedResult=result.indexOf("OK");
// log ("received ok ="+expectedResult);
if (expectedResult>-1){
mySerial.send("at+csca=\""+csca+"\"");
startTime=(new Date()).getTime();
}else{
step=step-1;
}
break;
case 4:
expectedResult=result.indexOf("OK");
// log ("received ok ="+expectedResult);
if (expectedResult>-1){
mySerial.send("at+cmgs=\""+recipient+"\"");
startTime=(new Date()).getTime();
}else{
step=step-1;
}
break;
case 5:
expectedResult=result.indexOf(">");
// log ("received ok ="+expectedResult);
if (expectedResult>-1){
mySerial.send(message+cntrlZ);
startTime=(new Date()).getTime();
}else{
step=step-1;
}
delay=VERYLONG;//waitning for message ack
break;
case 6:
expectedResult=result.indexOf("OK");
//read message number
if (expectedResult>-1){
int n=result.indexOf("CMGS:");
result=result.substring(n+5);
n=result.indexOf("\n");
status=0;
messageNo=Long.parseLong(result.substring(0,n).trim() );
log ("sent message no:"+messageNo);
}else{
step=step-1;
}
break;
}
step=step+1;
aThread.sleep(100);
}catch (Exception e){
e.printStackTrace();
}
}
mySerial.closeConnection() ;
//if timed out set status
if (timeOut ) {
status=-2;
log("*** time out at step "+step+"***");
}
}
AT命令按照规范发送。它工作得很好,但现在我已经阅读了收件箱中的短信。我写了类似的函数,如下所示:
public void receiveMessage() throws Exception
{
int expectedResult = 0;
SerialParameters params = defaultParameters;
mySerial =new SerialConnection (params);
mySerial.openConnection();
// step 1
mySerial.send("atz");
delay=LONG;
Thread.sleep(100);
//aThread.sleep(100);
String result= mySerial.getIncommingString() ;
// step 2
delay=STANDARD;
mySerial.send("ath0");
Thread.sleep(100);
// step 3
result= mySerial.getIncommingString() ;
expectedResult=result.indexOf("OK");
//log ("received ok ="+expectedResult);
if (expectedResult>-1){
mySerial.send("at+cmgf=1");
//startTime=(new Date()).getTime();
}
// step 4
result= mySerial.getIncommingString() ;
expectedResult=result.indexOf("OK");
//log ("received ok ="+expectedResult);
if (expectedResult>-1){
//mySerial.send("at+cmgl=\"ALL\"");
mySerial.send("at+cmgr=1");
//startTime=(new Date()).getTime();
}
Thread.sleep(100);
result= mySerial.getIncommingString() ;
}
在步骤 1 中,我发送 atz 命令,得到响应 OK,然后命令 ath0 和响应 OK,然后命令 at+cmgl=\"ALL\" 并再次响应 OK,但是我的消息在哪里?我在想最后一个响应(getIncommingString)应该包含从收件箱读取的消息。
我知道是 SMSLib 和其他库。但要使用该库,我必须添加许多其他库(用于日志记录)。我想要一个简单的应用程序来发送和接收短信。
谢谢
I found free source project to send sms using java comm:
http://code.google.com/p/find-ur-pal/source/browse/src/?r=21
Function sending sms looks like this:
public void run(){
boolean timeOut=false;
long startTime=(new Date()).getTime();
while ((step <7) && (!timeOut)){
// log(""+((new Date()).getTime() - startTime);
//check where we are in specified delay
timeOut=((new Date()).getTime() - startTime)>delay;
//if atz does not work, type to send cntrlZ and retry, in case a message was stuck
if (timeOut && (step==1)) {
step=-1;
mySerial.send( ""+cntrlZ);
}
//read incoming string
String result= mySerial.getIncommingString() ;
// log ("<- "+result+"\n--------");
int expectedResult=-1;
try{
//log ("Step:"+step);
switch (step){
case 0:
mySerial.send("atz");
delay=LONG;
startTime=(new Date()).getTime();
break;
case 1:
delay=STANDARD;
mySerial.send("ath0");
startTime=(new Date()).getTime();
break;
case 2:
expectedResult=result.indexOf("OK");
//log ("received ok ="+expectedResult);
if (expectedResult>-1){
mySerial.send("at+cmgf=1");
startTime=(new Date()).getTime();
}else{
step=step-1;
}
break;
case 3:
expectedResult=result.indexOf("OK");
// log ("received ok ="+expectedResult);
if (expectedResult>-1){
mySerial.send("at+csca=\""+csca+"\"");
startTime=(new Date()).getTime();
}else{
step=step-1;
}
break;
case 4:
expectedResult=result.indexOf("OK");
// log ("received ok ="+expectedResult);
if (expectedResult>-1){
mySerial.send("at+cmgs=\""+recipient+"\"");
startTime=(new Date()).getTime();
}else{
step=step-1;
}
break;
case 5:
expectedResult=result.indexOf(">");
// log ("received ok ="+expectedResult);
if (expectedResult>-1){
mySerial.send(message+cntrlZ);
startTime=(new Date()).getTime();
}else{
step=step-1;
}
delay=VERYLONG;//waitning for message ack
break;
case 6:
expectedResult=result.indexOf("OK");
//read message number
if (expectedResult>-1){
int n=result.indexOf("CMGS:");
result=result.substring(n+5);
n=result.indexOf("\n");
status=0;
messageNo=Long.parseLong(result.substring(0,n).trim() );
log ("sent message no:"+messageNo);
}else{
step=step-1;
}
break;
}
step=step+1;
aThread.sleep(100);
}catch (Exception e){
e.printStackTrace();
}
}
mySerial.closeConnection() ;
//if timed out set status
if (timeOut ) {
status=-2;
log("*** time out at step "+step+"***");
}
}
AT commands are sending according to specifications. And it works perfectly, but now I have read sms from Inbox. I have write similar function like this:
public void receiveMessage() throws Exception
{
int expectedResult = 0;
SerialParameters params = defaultParameters;
mySerial =new SerialConnection (params);
mySerial.openConnection();
// step 1
mySerial.send("atz");
delay=LONG;
Thread.sleep(100);
//aThread.sleep(100);
String result= mySerial.getIncommingString() ;
// step 2
delay=STANDARD;
mySerial.send("ath0");
Thread.sleep(100);
// step 3
result= mySerial.getIncommingString() ;
expectedResult=result.indexOf("OK");
//log ("received ok ="+expectedResult);
if (expectedResult>-1){
mySerial.send("at+cmgf=1");
//startTime=(new Date()).getTime();
}
// step 4
result= mySerial.getIncommingString() ;
expectedResult=result.indexOf("OK");
//log ("received ok ="+expectedResult);
if (expectedResult>-1){
//mySerial.send("at+cmgl=\"ALL\"");
mySerial.send("at+cmgr=1");
//startTime=(new Date()).getTime();
}
Thread.sleep(100);
result= mySerial.getIncommingString() ;
}
In step 1 I send atz command and I got response OK then command ath0 and response OK and then command at+cmgl=\"ALL\" and again response OK, but where are my messages ? I was thinking then the last response (getIncommingString) should contain messages read from inbox.
I know that are SMSLib and other libraries. But to use that libraries I have to added a lot of other libraries (for logging). I want to have simple application to send and receive sms.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您正在从 SIM 卡读取短信,则必须首先执行 AT+CMGL 来查找存储的任何短信 (SMS-DELIVER) 的索引。然后你需要使用AT+CMGR来读取特定的短信。您是在 PDU 模式还是文本模式下工作?
只是作为旁注。为什么要发送 ATZ 和 ATH0 命令?这些是与配置文件和呼叫相关的命令。
要查看调制解调器允许的所有消息状态:
典型的响应是:
因此,要查看 SIM 卡上的所有消息:
要查看 SIM 卡上的所有未读(新)消息:
还有另一个选项可以阻止 SMS 消息存储在您的 SIM 卡上。这是通过使用 AT+CNMI 命令配置要启用的未经请求的消息来控制的。然后,每当收到 SMS 时,您都会异步收到 +CMT 消息。如果您想了解更多信息,请告诉我。
使用主动提供的方法有一些好处。主要的一点是您无需管理您的 SIM 卡内存(没有内存已满的风险)。此外,如果短信数量过多,您的 SIM 卡实际上可能会变得无法使用。
If you are reading SMS's from the SIM Card then you must first execute AT+CMGL to find out the indexes of any SMS's (SMS-DELIVERs) stored. Then you need to use AT+CMGR to read a specific SMS. Are you working in PDU mode or Text mode?
Just as a side note. Why are you sending ATZ and ATH0 commands? These are profile and call related commands.
To see all message statuses allowed from your modem:
A typical response would be:
So to look at all messages on your SIM card:
To see all unread (new) messages on your SIM card:
There is another option where you can prevent SMS messages being stored on your SIM card. This is controlled by using the AT+CNMI command to configure unsolicited messages to be enabled. Then whenever a SMS is received then you will receive a +CMT message asynchronously. If you want to know more about that one just let me know.
There are a few benefits making use of the unsolicited approach. The main one being you don't have to manage your SIM cards memory (no risk of it getting full). Also with large quantities of SMS's your SIM card can actually become unusable.