如何读取使用Java通讯GSM短信?
-
27-10-2019 - |
题
我发现免费的源项目中使用Java通讯发送短信: http://code.google.com/ p /找到-UR-PAL /源/浏览/ 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 I发送ATZ命令和我响应行然后命令ATH0和响应确定,然后命令在+ CMGL = \“ALL \”,并再次响应行,但其中是我的消息?我想,那么最后一个响应(getIncommingString)应包含的消息收件箱中从读取。
我知道是SMSLib和其他库。但是,使用该库我已经加了很多其他库(用于日志记录)。我希望有简单的应用程序来发送和接收短信。
由于
解决方案
如果您是从SIM卡中读取短信的,那么你必须先执行AT + CMGL找出存储任何SMS的(SMS-提供)的索引。然后,你需要使用AT + CMGR读取特定的SMS。你在PDU模式或文本模式下工作?
正如一个方面说明。你为什么要送ATZ和ATH0命令?这些简档和呼叫相关的命令。
要看到从调制解调器所允许的所有消息的状态:
AT+CGML=?
一个典型的反应是:
+CMGL: ("REC UNREAD","REC READ","STO UNSENT","STO SENT","ALL")
所以看的所有消息在SIM卡上:
AT+CGML="ALL"
要查看您的SIM卡上的所有未读(新)消息:
AT+CGML="REC UNREAD"
还有另一种选择,你可以防止短信被存储在SIM卡上。这是通过使用AT + CNMI命令配置请自来的邮件启用控制。然后,只要接收到的短信,那么你将异步接收一个+ CMT消息。如果您想了解更多关于一个只是让我知道。
有利用未经请求的方法的一些好处。主要的一个是,你不必来管理你的SIM卡存储器(没有也得到充分的风险)。也有大量的短信是你的SIM卡,实际上可能无法使用。
不隶属于 StackOverflow