Amigos,
Eu estou criando uma rotina que conecta na porta serial virtual COM40 que é uma conexão bluetooth com meu celular.
Estou tentando enviar uma mensagem de SMS via java, mas não estou conseguindo.
Faço os testes via hyperterminal e rola numa boa, mas qdo tento no java, não dá exceção nenhuma mas tb não envia.
Alguém poderia me ajudar?
Ps.: Estou usando a lib RXTX
package cc.marcio.serial.util;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import java.io.IOException;
import java.io.OutputStream;
/**
* <b>Description:</b> TODO: Add here the class description
*
* @author <a href="mailto:marciobarroso@gmail.com">Márcio Alves Barroso</a>
* @since 03/10/2008 15:50:53
* @version 0.1
*/
public class SerialUtil {
private Integer bitsPorSegundos;
public SerialUtil() {
this.bitsPorSegundos = 19200;
}
public void sendSMS(String port, String mensagem, Integer numero) {
StringBuilder command = new StringBuilder();
command.append("AT\n");
command.append("AT+CMGF=1\n");
command.append("AT+CMGS=\"+"+numero+"\"\n");
command.append("Teste de envio de mensagem"+(char)26);
SerialPort sp = this.open(port);
this.sendMessage(sp, command.toString());
this.close(sp);
}
public void sendMessage(SerialPort port, String message) {
OutputStream os;
try {
os = port.getOutputStream();
os.write(message.getBytes());
Thread.sleep(100);
os.flush();
} catch (IOException e) {
throw new SerialException("A porta " + port.getName() + " não está acessível para escrita.", e);
} catch (InterruptedException e) {
throw new SerialException("A porta " + port.getName() + " não foi encontrada.", e);
}
}
public void close(SerialPort port) {
port.close();
}
public SerialPort open(String port) {
try {
CommPortIdentifier portIdentifier = CommPortIdentifier
.getPortIdentifier(port);
if (portIdentifier.isCurrentlyOwned()) {
System.out.println("Error: Port is currently in use");
} else {
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams( this.bitsPorSegundos,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
return serialPort;
} else {
throw new SerialException("Esta implementação suporta somente conexão com portas serial");
}
}
} catch (NoSuchPortException e) {
throw new SerialException("A porta " + port + " não foi encontrada.", e);
} catch (PortInUseException e) {
throw new SerialException("A porta " + port + " já está em uso.", e);
} catch (UnsupportedCommOperationException e) {
throw new SerialException("A porta " + port + " não suporta esta operação", e);
}
return null;
}
public static void main(String[] args) {
new SerialUtil().sendSMS("COM40", "teste de envio de mensagem", 86742075);
}
}