java版本的短信猫

版权声明:博客知识产权来源命运的信徒,切勿侵权 https://blog.csdn.net/qq_37591637/article/details/85858934

之前win32.com.dll动态库只支持jdk32位的,而且版本比较老;

我建议RXTX串口通讯库

1、环境配置

1.1、将rxtxSerial.dll复制到 JAVA_HOME\bin目录下


1.2、将RXTXcomm.jar复制到 JAVA_HOME\jre\lib\ext目录下


1.3、将javax.comm.properties复制到 JAVA_HOME\lib目录下


jar包导入lib包下面


注意,myelispe一定要配置这个java不能是myelispe自带的


 代码如下

package cn.com.dao;

import java.io.IOException;

import org.smslib.AGateway;
import org.smslib.AGateway.Protocols;
import org.smslib.GatewayException;
import org.smslib.SMSLibException;
import org.smslib.Service;
import org.smslib.TimeoutException;
import org.smslib.modem.SerialModemGateway;

public class BeforeMessage {
	/*
	 * author:命运的信徒 date:2019/1/5 arm:java版本的短信猫
	 */
	// 1.设置串行模块网关参数
	private static final SerialModemGateway smg = new SerialModemGateway(
			"modem.com3", "COM3", 115200, "Caimore", "6200");

	// 2.服务启动的相关方法
	public static Service doIt() {
		// 1.实例化服务对象
		Service service = Service.getInstance();
		OutBoundMessageNotficationImpl obm = new OutBoundMessageNotficationImpl();
		// 当短信发送出去以后就会回调函数
		service.setOutboundMessageNotification(obm);
		// 2.设置循环模式
		try {
			service.addGateway(getGateway());
			// 3.设置
			service.S.SERIAL_POLLING = true;
			service.startService();
		} catch (GatewayException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SMSLibException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return service;
	}

	public static AGateway getGateway() {
		// 1.设置协议
		smg.setProtocol(Protocols.PDU);
		// 2.设置接收信息
		smg.setInbound(true);
		// 3.设置发送信息
		smg.setOutbound(true);
		// 4.设置sim卡的锁
		smg.setSimPin("0000");
		return smg;
	}
}
package cn.com.dao;
import org.smslib.AGateway;
import org.smslib.IOutboundMessageNotification;
import org.smslib.OutboundMessage;
public class OutBoundMessageNotficationImpl implements
		IOutboundMessageNotification {
	//继承接口,返回发送短信之后的网关和信息对象,把这些参数传到process方法里面
	@Override
	public void process(AGateway arg0, OutboundMessage arg1) {
		System.out.println("发送短信接收之后的网关id:"+arg0.getGatewayId());
		System.out.println("发送出去短信的对象为"+arg1);

	}

}
package cn.com.dao;
import java.io.IOException;
import org.smslib.GatewayException;
import org.smslib.Message.MessageEncodings;
import org.smslib.OutboundMessage;
import org.smslib.SMSLibException;
import org.smslib.Service;
import org.smslib.TimeoutException;
public class SendMeaages {
/*author:命运的信徒
 * date:2019/1/5
 * arm:短信猫
 */
	private static Service service=BeforeMessage.doIt();
	private long lasttime=0;
	//1.开启服务
	public void startService(){
		
		BeforeMessage.doIt();
	}
	//2.关闭服务
	public void stopService(){
		try {
			service.startService();
			service.removeGateway(BeforeMessage.getGateway());
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (GatewayException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SMSLibException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	//3.发送信息
	public boolean doSend(String phone,String text) throws TimeoutException, GatewayException, IOException, InterruptedException{
		boolean flag=false;
		//1.发送信息	
		OutboundMessage obm=new OutboundMessage(phone,text);
		//2.发送中文信息一定要添加这一句
		obm.setEncoding(MessageEncodings.ENCUCS2);
		//3.开始判断,从1970年1月1日0点到现在的时间-最后一次的时候,如果时间小于5000,就等待一段时间
		long diff=System.currentTimeMillis()-lasttime;
		if(diff<5000){
			try {
				Thread.sleep(5000-diff);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		//4.信息对象不为空的话,就发送数据
		if(null!=obm){  
			flag=service.sendMessage(obm);
			if(flag){
				
				System.out.println("发送成功....");
			}else{
				System.out.println("发送失败...");
			}
			
		}
		//把最后一次的时间设置为当前的时候,每隔5000以后才能发一次信息
		lasttime = System.currentTimeMillis();
		return flag;
	}
}

简洁版本

package cn.com.way02;
import java.io.IOException;
import org.smslib.AGateway.Protocols;
import org.smslib.Message.MessageEncodings;
import org.smslib.OutboundMessage;
import org.smslib.SMSLibException;
import org.smslib.Service;
import org.smslib.TimeoutException;
import org.smslib.modem.SerialModemGateway;
public class TestWay01 {
	public static boolean SmsMessage(String phone, String text)
			throws TimeoutException, SMSLibException, IOException,
			InterruptedException {
		boolean result = false;
		// 1.创建模块实例
		SerialModemGateway smg = new SerialModemGateway("modem.com3", "COM3",
				115200, "Caimore", "");
		smg.setProtocol(Protocols.PDU);// 设置协议为pdu
		smg.setInbound(true);// 设置可以接收信息
		smg.setOutbound(true);// 设置可以发生信息
		// 2.获取单例的服务
		Service service = Service.getInstance();
		try {
			// 服务添加网关
			service.addGateway(smg);
			// 启用轮循模式
			service.S.SERIAL_POLLING = true;
			// 开启服务
			service.startService();
			// 3、发送短信
			OutboundMessage obg = new OutboundMessage(phone, text);
			// 如果发生的是中文的短信的话,一定要加上这一句话
			obg.setEncoding(MessageEncodings.ENCUCS2);
			result = service.sendMessage(obg);
			// 4、关闭程序
			service.stopService();
			service.removeGateway(smg);
		} catch (Exception e) {
			e.printStackTrace();
			service.stopService();
			service.removeGateway(smg);
			return false;
		}

		return result;
	}

	public static void main(String[] args) {
		try {
			SmsMessage("15656215623", "java版本RXTX包的短信猫测试成功!");
		} catch (Exception e) {

		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_37591637/article/details/85858934