Skip to content

Instantly share code, notes, and snippets.

@Kermit
Created May 25, 2013 16:40
Show Gist options
  • Save Kermit/5649706 to your computer and use it in GitHub Desktop.
Save Kermit/5649706 to your computer and use it in GitHub Desktop.
package com.mobileis.common;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import com.mobileis.tc45.ATSessionManager;
import com.mobileis.tc45.WDTFeeder;
import com.siemens.icm.io.*;
public class Main extends MIDlet implements ATCommandListener {
private WDTFeeder wdtFeeder;
public boolean DEBUG;
String operator = null;
int m_Rings = 0;
public Main() {
super();
System.out.println(Runtime.getRuntime().freeMemory() + " bytes of "
+ Runtime.getRuntime().totalMemory() + " bytes available");
//addListener( this );
}
/*
* (non-Javadoc)
*
* @see javax.microedition.midlet.MIDlet#startApp()
*/
protected void startApp() throws MIDletStateChangeException {
String tmpStr = ATSessionManager.send("AT^SPIO=1\r");
if (tmpStr.indexOf("OK") == -1) {
System.out.println("Main: Cannot open GPIO");
}
wdtFeeder = new WDTFeeder(5000);
wdtFeeder.start();
{
tmpStr = ATSessionManager.send("AT+CGMR\r");
System.out.println("Siemens firmware: "+tmpStr);
}
try {
sendPin();
} catch (ATCommandFailedException e) {
System.out.println(e.getMessage());
System.exit(1);
}
for (int i = 0; i<40 && operator == null; i++) {
getOperator();
waitUp(2000);
}
System.out.println("Operator?: " + operator);
tmpStr = ATSessionManager.send("AT+CSQ\r");
System.out.println("Sygnal" + tmpStr);
ATCommand atCmd;
try {
atCmd = new ATCommand( false );
atCmd.addListener( this );
}
catch( ATCommandFailedException e )
{
System.out.println( "wyjatek przy tworzeniu obiektu ATCommand" );
}
}
private void getOperator() {
String response;
try {
response = ATSessionManager.send("AT+COPS?\r");
//log("response: " + response, LVL_DEBUG);
if (response.indexOf("+COPS") != -1) {
int i1 = response.indexOf("\"");
int i2 = response.indexOf("\"", i1 + 1);
if (i1 != -1 && i2 != -1)
operator = response.substring(i1 + 1, i2);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void waitUp(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
public void stopApp() {
System.out.println("Terminating application, restarting TC45...");
wdtFeeder.stop();
System.exit(0);
}
protected void pauseApp() {
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
wdtFeeder.stop();
}
private void sendPin() throws ATCommandFailedException {
String strRcv;
// Check PIN state
System.out.println("setup pin");
strRcv = ATSessionManager.send("AT+CPIN?\r");
if (strRcv.indexOf("ERROR") >= 0) {
// Give Module some Time and retry again
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
strRcv = ATSessionManager.send("AT+CPIN?\r");
if (strRcv.indexOf("ERROR") >= 0) {
System.out.println("SIM card not inserted...");
stopApp();
return;
}
}
if (strRcv.indexOf("+CPIN: READY") < 0) {
if (strRcv.indexOf("+CPIN: SIM PIN") >= 0) {
// Send the Pin to the device
System.out.println("requires pin");
strRcv = ATSessionManager.send("AT+CPIN="
+ getAppProperty("PIN") + "\r");
} else if (strRcv.indexOf("PUK") >= 0) {
// PUK required... Must exit application!
System.out.println("PUK Code required!");
stopApp();
return;
} else {
// Otherwise there is no SIM!
System.out.println("SIM Card required!");
stopApp();
return;
}
}
}
//metody ATCommandListenera
public void ATEvent(String Event) {
System.out.println("URC-Event: " + Event);
}
public void RINGChanged(boolean SignalState) {
String caller;
caller = ATSessionManager.send("AT+CLCC\r");
System.out.println("Dzwoni:" + caller);
System.out.println("Rozlaczam.");
ATSessionManager.send("ATH\r");
int cudzy1 = caller.indexOf('"');
int cudzy2 = caller.lastIndexOf('"');
if (caller.indexOf("CLCC") > 0) {
System.out.println("Dzwoni:" + caller);
System.out.println("Tryb tekstowy.");
ATSessionManager.send("AT+CMGF=1\r");
System.out.println("Wysylam SMS");
ATSessionManager.send("AT+CMGS=\"509709964\"\r");
if (cudzy1 == -1) {
ATSessionManager.send("Dzwoni numer: zastrzezony\032\r");
} else {
cudzy1++;
ATSessionManager.send("Dzwoni numer: " + caller.substring(cudzy1, cudzy2) + "\032\r");
}
}
//log("RING-Event: " + SignalState, LVL_DEBUG);
}
public void DCDChanged(boolean SignalState) {
//log("DCD-Event: " + SignalState, LVL_DEBUG);
}
public void DSRChanged(boolean SignalState) {
//log("DSR-Event: " + SignalState, LVL_DEBUG);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment