📄 smsservice.java
字号:
</ul>
@param message a OutgoingMessage containing the message you wish to send.
@return One of ERR_* values.
@see OutgoingMessage
@see CPhoneBook
@see SMSService#sendMessage(LinkedList)
@see SMSService#setPhoneBook(String)
*/
public int sendMessage(OutgoingMessage message) {
LinkedList messageList;
OutgoingMessage msg;
int error;
synchronized (_SYNC_) {
messageList = new LinkedList();
messageList.add(message);
error = sendMessage(messageList);
if (error == ERR_OK) {
msg = (OutgoingMessage) messageList.get(0);
message.setDispatchDate(msg.getDispatchDate());
}
return error;
}
}
/**
Send an series of SMS messages from the GSM device. This method is used
when you want to send more than one message as a batch. If your GSM device
support the feature of keeping the GSM link open during message dispatch,
this method should work faster than calling the sendMessage(OutgoingMessage)
method many times.
<br>
Just create a LinkedList object, add as many OutgoingMessage objects you wish
and call the method.
<br><br>
<strong>Notes:</strong>
<ul>
<li>If you have set a phonebook, you can create the OutgoingMessage
object with a nickname, instead of the actual phone number.</li>
</ul>
@param messageList a LinkedList filled with OutgoingMessage objects.
@return One of ERR_* values.
@see OutgoingMessage
@see CPhoneBook
@see SMSService#sendMessage(OutgoingMessage)
@see SMSService#setPhoneBook(String)
*/
public int sendMessage(LinkedList messageList) {
LinkedList outList;
OutgoingMessage message;
String response, pdu;
int i, j, error;
synchronized (_SYNC_) {
if (getConnected()) {
//if (phoneBook.isLoaded()) outList = phoneBook.expandPhoneBookEntries(messageList);
//else
outList = messageList;
switch (operationMode) {
case MODE_ASCII :
try {
serialDriver.send(SMSATCommands.AT_ASCII_MODE);
response = serialDriver.getResponse();
serialDriver.send(SMSATCommands.AT_KEEP_LINK_OPEN);
response = serialDriver.getResponse();
serialDriver.send(SMSATCommands.AT_CHARSET_HEX);
response = serialDriver.getResponse();
error = ERR_OK;
for (i = 0; i < outList.size(); i++) {
message = (OutgoingMessage) outList.get(i);
serialDriver.send(
SMSUtils.substituteSymbol(
SMSATCommands.AT_SEND_MESSAGE,
"{1}",
message.getRecipient()));
while (serialDriver.dataAvailable())
serialDriver.skipBytes(1);
serialDriver.send(message.getHexText());
serialDriver.send((char) 26);
response = serialDriver.getResponse();
if (response.indexOf(SMSATCommands.AT_OK)
> -1) {
message.setDispatchDate(new Date());
deviceInfo.getStatistics().incTotalOut();
} else {
message.setDispatchDate(null);
error = ERR_SEND_FAILED;
}
}
return error;
} catch (Exception e) {
e.printStackTrace();
setConnected(false);
return ERR_NOT_CONNECTED;
}
case MODE_PDU :
try {
serialDriver.send(SMSATCommands.AT_PDU_MODE);
response = serialDriver.getResponse();
//serialDriver.send(SMSATCommands.AT_KEEP_LINK_OPEN);
//response = serialDriver.getResponse();
error = ERR_OK;
for (i = 0; i < outList.size(); i++) {
message = (OutgoingMessage) outList.get(i);
pdu = message.getPDU(smscNumber);
j = pdu.length();
j /= 2;
if (smscNumber == null);
else if (smscNumber.length() == 0)
j--;
else {
j -= ((smscNumber.length() - 1) / 2);
j -= 2;
}
/////
j = j - 1;
////
String at_comm_send =
SMSUtils.substituteSymbol(
SMSATCommands.AT_SEND_MESSAGE,
"\"{1}\"",
"" + j);
serialDriver.send(at_comm_send);
while (!serialDriver.dataAvailable()) {
try {
Thread.sleep(10);
} catch (Exception e) {
}
};
while (serialDriver.dataAvailable())
serialDriver.skipBytes(1);
serialDriver.send(pdu);
serialDriver.send((char) 26);
response = serialDriver.getResponse();
if (response.indexOf(SMSATCommands.AT_OK)
> -1) {
message.setDispatchDate(new Date());
deviceInfo.getStatistics().incTotalOut();
} else {
message.setDispatchDate(null);
error = ERR_SEND_FAILED;
}
}
return error;
} catch (Exception e) {
e.printStackTrace();
setConnected(false);
return ERR_NOT_CONNECTED;
}
default :
return ERR_SEND_FAILED;
}
} else
return ERR_NOT_CONNECTED;
}
}
/**
Deletes an SMS message from the GSM device memory.
<br><br>
<strong>Notes:</strong>
<ul>
<li>A deleted message cannot be recovered.</li>
</ul>
@param message a valid IncomingMessage object, i.e. an object which is
previously read with readMessages() from the GSM device.
@return One of ERR_* values.
@see IncomingMessage
@see SMSService#deleteMessage(int)
*/
public int deleteMessage(IncomingMessage message) {
synchronized (_SYNC_) {
return deleteMessage(message.getMemIndex());
}
}
/**
Deletes an SMS message from the GSM device memory.
<br><br>
<strong>Notes:</strong>
<ul>
<li>A deleted message cannot be recovered.</li>
<li>It is highly recommended to use the other form of the deleteMessage()
method.</li>
</ul>
@param memIndex the memory index of the GSM device's memory from where
the message (if there is any message there) should be deleted.
@return One of ERR_* values.
@see SMSService#deleteMessage(IncomingMessage)
*/
public int deleteMessage(int memIndex) {
String response;
synchronized (_SYNC_) {
if (getConnected()) {
if (memIndex > 0) {
try {
serialDriver.send(
SMSUtils.substituteSymbol(
SMSATCommands.AT_DELETE_MESSAGE,
"{1}",
"" + memIndex));
response = serialDriver.getResponse();
if (response.indexOf(SMSATCommands.AT_OK) > -1)
return ERR_OK;
else
return ERR_MESSAGE_NOT_FOUND;
} catch (Exception e) {
e.printStackTrace();
return ERR_GENERIC_ERROR;
}
} else
return ERR_GENERIC_ERROR;
} else
return ERR_NOT_CONNECTED;
}
}
/**
Virtual method, called upon receipt of a message (Asynchronous mode only!)
<br><br>
<strong>Notes:</strong>
<ul>
<li>If you plan to use jSMSEngine API in asynchronous mode, you should
override this method, making it do your job upon message receipt.</li>
</ul>
@param message the received message.
@return return true if you wish the message to be deleted from the GSM device's memory.
Otherwise false.
@see SMSService#setReceiveMode(int)
*/
public boolean received(IncomingMessage message) {
return false;
}
/**
* Checks if the message is SMS-DELIVER.
*
* @author George Karadimas
* @param pdu the message pdu
* @return true if the message is SMS-DELIVER
*/
private boolean isIncomingMessage(String pdu) {
int index, i;
i = Integer.parseInt(pdu.substring(0, 2), 16);
index = (i + 1) * 2;
i = Integer.parseInt(pdu.substring(index, index + 2), 16);
if ((i & 0x03) == 0)
return true;
else
return false;
}
/**
* Checks if the message is SMS-STATUS-REPORT.
*
* @param pdu the message pdu
* @return true if the message is SMS-STATUS-REPORT
*/
private boolean isStatusReportMessage(String pdu) {
String str;
int index, i;
str = pdu.substring(0, 2);
i = Integer.parseInt(str, 16);
index = (i + 1) * 2;
str = pdu.substring(index, index + 2);
i = Integer.parseInt(str, 16);
if ((i & 0x02) == 2)
return true;
else
return false;
}
private void setConnected(boolean connected) {
this.connected = connected;
}
private void setInitialized(boolean initialized) {
this.initialized = initialized;
}
private String getManufacturer() throws Exception {
String response;
String whatToDiscard;
whatToDiscard =
"+"
+ SMSUtils.substituteSymbol(
SMSUtils.substituteSymbol(
SMSATCommands.AT_MANUFACTURER,
"AT+",
""),
"\r",
"")
+ ": ";
serialDriver.send(SMSATCommands.AT_MANUFACTURER);
response = serialDriver.getResponse();
if (response.length() == 0)
response = "";
else if (response.indexOf(SMSATCommands.AT_OK) > -1) {
response =
SMSUtils.substituteSymbol(response, SMSATCommands.AT_OK, "");
response = SMSUtils.substituteSymbol(response, "\r", "");
response = SMSUtils.substituteSymbol(response, "\n", "");
response = SMSUtils.substituteSymbol(response, whatToDiscard, "");
response = SMSUtils.substituteSymbol(response, "\"", "");
} else
response = DEFAULT_VALUE_NOT_REPORTED;
return response;
}
private String getModel() throws Exception {
String response;
String whatToDiscard;
whatToDiscard =
"+"
+ SMSUtils.substituteSymbol(
SMSUtils.substituteSymbol(
SMSATCommands.AT_MODEL,
"AT+",
""),
"\r",
"")
+ ": ";
serialDriver.send(SMSATCommands.AT_MODEL);
response = serialDriver.getResponse();
if (response.length() == 0)
response = "";
else if (response.indexOf(SMSATCommands.AT_OK) > -1) {
response =
SMSUtils.substituteSymbol(response, SMSATCommands.AT_OK, "");
response = SMSUtils.substituteSymbol(response, "\r", "");
response = SMSUtils.substituteSymbol(response, "\n", "");
response = SMSUtils.substituteSymbol(response, whatToDiscard, "");
response = SMSUtils.substituteSymbol(response, "\"", "");
} else
response = DEFAULT_VALUE_NOT_REPORTED;
return response;
}
private String getSerialNo() throws Exception {
String response;
String whatToDiscard;
whatToDiscard =
"+"
+ SMSUtils.substituteSymbol(
SMSUtils.substituteSymbol(
SMSATCommands.AT_SERIALNO,
"AT+",
""),
"\r",
"")
+ ": ";
serialDriver.send(SMSATCommands.AT_SERIALNO);
response = serialDriver.getResponse();
if (response.length() == 0)
response = "";
else if (response.indexOf(SMSATCommands.AT_OK) > -1) {
response =
SMSUtils.substituteSymbol(response, SMSATCommands.AT_OK, "");
response = SMSUtils.substituteSymbol(response, "\r", "");
response = SMSUtils.substituteSymbol(response, "\n", "");
response = SMSUtils.substituteSymbol(response, whatToDiscard, "");
} else
response = DEFAULT_VALUE_NOT_REPORTED;
return response;
}
private String getImsi() throws Exception {
String response;
String whatToDiscard;
whatToDiscard =
"+"
+ SMSUtils.substituteSymbol(
SMSUtils.substituteSymbol(SMSATCommands.AT_IMSI, "AT+", ""),
"\r",
"")
+ ": ";
serialDriver.send(SMSATCommands.AT_IMSI);
response = serialDriver.getResponse();
if (response.length() == 0)
response = "";
else if (response.indexOf(SMSATCommands.AT_OK) > -1) {
response =
SMSUtils.substituteSymbol(response, SMSATCommands.AT_OK, "");
response = SMSUtils.substituteSymbol(response, "\r", "");
response = SMSUtils.substituteSymbol(response, "\n", "");
response = SMSUtils.substituteSymbol(response, whatToDiscard, "");
} else
response = DEFAULT_VALUE_NOT_REPORTED;
return response;
}
private String getSwVersion() throws Exception {
String response;
String whatToDiscard;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -