1.从SMSLib网站下载SMSLib代码。下载时,确保你下载的是SMSLib-Java-v1.0.1.zip。SMSLib for Java可以跟Java通信API或RxTx一起使用
2.以Java通信API 2.0作为开始,首先,确保你已经正确安装了API。解压javacomm20-win32.zip。在commapi子目录中,你将找到如下文件:
javax.comm.properties
win32com.dll
comm.jar
--------------------------
3.安装java通信API
把javax.comm.properties拷贝到你的Java运行时环境的lib目录中(JDk和JRE都拷贝)。把win32com.dll拷贝到你的JRE的bin目录中。
设置PATH=.;c:\j2sdk1.4.2_03\jre\bin;。现在,使用如下命令来运行Java 黑盒程序:
java -classpath .;../../comm.jar;BlackBox.jar; BlackBox
如果Java通信API被正确的安装了,那么如图所示,会出现一个显示你机器的可用串口(COM端口)的Swing窗口
发送短信模块:
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org .smslib .IOutboundMessageNotification;
import org .smslib .OutboundMessage;
import org .smslib .Service ;
import org .smslib .Message.MessageEncodings;
import org .smslib .modem.SerialModemGateway;
/**
* 短信发送测试类
* @author mazq
*
*/
public class SMSUtil{
public class OutboundNotification implements IOutboundMessageNotification
{
public void process(String gatewayId, OutboundMessage msg)
{
System.out.println( "Outbound handler called from Gateway: " + gatewayId);
System.out.println(msg);
}
}
public void sendSMS(String mobilePhones,String content){
// System.out.println(mobilePhones+"--"+content);
Service srv;
OutboundMessage msg;
OutboundNotification outboundNotification = new OutboundNotification();
srv = new Service ();
// SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM1", 115200, "wavecom", "9600");
SerialModemGateway gateway = new SerialModemGateway( "modem.com1" , "COM1" , 115200 , "wavecom" , "9600" );
gateway.setInbound( true );
gateway.setOutbound( true );
gateway.setSimPin( "0000" );
gateway.setOutboundNotification(outboundNotification);
srv.addGateway(gateway);
System.out.println( "初始化成功,准备开启服务" );
try {
srv.startService();
System.out.println( "服务启动成功" );
String[] phones = mobilePhones.split( "," );
for ( int i= 0 ;i<phones.length;i++){
msg = new OutboundMessage(phones[i], content); //手机号码,和短信内容
msg.setEncoding(MessageEncodings.ENCUCS2); //这句话是发中文短信必须的
srv.sendMessage(msg);
System.out.println(phones[i]+ " == " +content);
}
srv.stopService();
} catch (Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
SMSUtil util = new SMSUtil();
util.sendSMS( "1355xxxxxxx" , "测试短信" );
}
}