ciao ti è andata proprio proprio di culo!
poco fa ho fatto un sowftware per l'invio di sms e tramite un modem gsm
ti posto la classe che si occupa della comunicazione seriale
(ho usato javacomm)
codice:
package gsmcomm;
import java.util.*;
import java.io.*;
import javax.comm.*;
import gsmcomm.Sms;
/**
* Class Gsm
*
*/
public class Gsm {
// Fields
private static Enumeration portList;
private static CommPortIdentifier portId;
private static SerialPort serialPort;
private static OutputStream out;
private static InputStream in;
private String dev_serial;
private char invio=(13);
private char fine=(26);
private char virgolette=(34);
// Methods
// Constructors
public Gsm(String dev_serial){
this.dev_serial=dev_serial;
}
private void Log(String message){
Main.Log(message);
}
/*
* questo metodo assegna a Serialport il device passato al costrutture,
*@throws una eccezione se non trova il device o se viene generato un errore
*/
public void SetSerialPort()throws Exception{
portList = CommPortIdentifier.getPortIdentifiers();
boolean portFound = false;
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(dev_serial)) {
this.Log("Found port " + dev_serial);
portFound = true;
try {
serialPort =
(SerialPort) portId.open("SimpleWrite", 2000);
} catch (PortInUseException e) {
this.Log("Port in use.");
continue;
}
try {
out = serialPort.getOutputStream();
in =serialPort.getInputStream();
} catch (IOException e) {}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
try {
serialPort.notifyOnOutputEmpty(true);
} catch (Exception e) {
this.Log("Error setting event notification");
this.Log(e.toString());
System.exit(-1);
}
}
}
}
}
// Operations
/**
*
* @param smsID
* @return Sms
*/
public Sms ReadSMS( int smsID) {
return null;
}
/**
*
* @param sms
* @return void
*/
public void SendSMS( Sms sms) throws Exception {
if(sms.getNumber()==null || sms.getText()==null){
throw new Exception("Errore , testo o numeri nulli");
}
try{
out.write(("AT+CMGF=1"+String.valueOf(this.invio)).getBytes());
out.write(("AT+CMGS="+String.valueOf(this.virgolette)+sms.getNumber()+String.valueOf(this.virgolette)+String.valueOf(this.invio)).getBytes());
out.write((sms.getText()+String.valueOf(this.invio)).getBytes());
out.write(String.valueOf(this.fine).getBytes());
this.Log("invio riuscito:"+sms);
}catch(Exception e){
throw new Exception("Errore durante l'invio del messaggio: "+e.getMessage());
}
}
/**
*
* @param smsID
* @return void
*/
public void DeleteSMS(int posizione) throws Exception {
try{
out.write(("AT+CMGD="+posizione+String.valueOf(this.invio)).getBytes());
this.Log("eliminazione dalla SIM riuscita del messaggio in posizione:"+posizione);
}catch(Exception e){
throw new Exception("Errore durante l'eliminazione del messaggio in posizione:"+posizione);
}
}
public Sms getSmsFromString(String smsstring){
StringTokenizer t=new StringTokenizer(smsstring,"\n");
int counter=0;
int counter_t2=0;
String numero=null;
String testo=null;
int id_sim=-1;
while(t.hasMoreTokens()){
String currentstring=t.nextToken();
StringTokenizer t2=new StringTokenizer(currentstring,",");
while(t2.hasMoreTokens()){
String currenttoken=t2.nextToken();
if(counter==0){
if(counter_t2==0){
//imposto l'id del messaggio sulla SIM
id_sim=Integer.parseInt(currenttoken);
}else if(counter_t2==2){
//imposto il numero del mittente
numero=currenttoken;
}
}
counter_t2++;
}
if(counter==1){
testo=currentstring;
}
/*reimposto il counter utilizzato quando faccio il parsing della prima linea
che contiene:
* token 0:3
token 1:"REC READ"
token 2:"+39222222222"
token 3:"06/01/30
token 4:15:06:24+04"
*/
counter_t2=0;
counter++;
}
if(numero!=null && testo!=null && id_sim!=-1){
return new Sms(numero, testo, id_sim);
}else{
return null;
}
}
public void DeleteListSms(ArrayList listsms) throws Exception{
for(int i=0;i<listsms.size();i++){
//un messaggio viene automaticamente messo in posizione 1
this.DeleteSMS(1);
}
}
/**
*
* @return ArrayList
*/
public ArrayList ListSMS( ) throws Exception {
ArrayList listasms=new ArrayList();
try{
out.write(("AT+CMGL="+String.valueOf(this.virgolette)+"ALL"+String.valueOf(this.virgolette)+String.valueOf(this.invio)).getBytes());
Thread.sleep(5000);
byte [] lettura=new byte[in.available()];
int check_lettura=in.read(lettura);
String buffer=new String(lettura);
int inizio_messaggio=buffer.indexOf("+CMGL: ");
while(inizio_messaggio!=-1){
/*
tolgo al buffer la parte antecedente all'inizio del messaggio e la lunghezza del
codice di inizio messaggio
*/
buffer=buffer.substring(inizio_messaggio+"+CMGL: ".length());
/*
verifico che ci siano almeno ancora 2 messaggi
*/
int fine_mex=0;
if(buffer.indexOf("+CMGL: ")==-1){
fine_mex=buffer.length();
}else{
fine_mex=buffer.indexOf("+CMGL: ");
}
String mex_corrente=buffer.substring(0,fine_mex);
//System.out.println("mex corrente:"+mex_corrente);
listasms.add(this.getSmsFromString(mex_corrente));
inizio_messaggio=buffer.indexOf("+CMGL: ");
}
}catch(Exception e){
throw new Exception("Errore durante la lista dei messaggi: "+e.getMessage());
}
return listasms;
}
}