Få en midlet til at køre på en enhed
Nu har jeg brugt ret lang tid på at finde ud af, hvordan man opnår det ønskede skrevet nedenunder. Men uden held.1:
Jeg vil gerne lave en midlet, som ikke skal kunne andet, end at vise, at scenariet virker ved at sende en sms.
2:
Midletten skal deployes til min enhed, hvilket er en XT65 (guides findes til CT65, som vist virker ens)
3:
Midletten skal startes på enheden, hvorefter den sender en sms.
Jeg har testet AT-kommandoer fra HyperTerminal, og herigennem kan jeg godt tilgå enheden, og få den til at sende sms.
Den kode har jeg så forsøgt at overføre til en midlet, og opnå det samme resultat. Men det virker ikke. Og det er ikke lykkede smig at debugge på selve enheden, så jeg er ikke klar over, hvor i processen problemet opstår.
Deployment til enheden foregår enten igennem Eclipse (på en mystik automatisk måde), eller ved at paste filerne ind på "Module" i Explorer.
Så har jeg fundet nogle AT-kommandoer, som skulle sætte java-koden igang på enheden, men kommandoen forstås ikke i HyperTerminalen.
Jeg har også prøvet at tage strømmen fra enheden, for på den måde at få en reboot til at opstå, men det virkede heller ikke.
Alle råd er meget velkomne!
Min kode er:
package example.helloworld;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.CommConnection;
import javax.microedition.io.Connector;
import javax.microedition.midlet.*;
import com.siemens.icm.io.ATCommand;
import com.siemens.icm.io.ATCommandFailedException;
import com.siemens.icm.io.ATCommandListener;
public class HelloWorld extends MIDlet implements ATCommandListener{
ATCommand _atc = null;
OutputStream os = null;
InputStream is = null;
/* Member variables */
boolean runThreads = true; // Flag for stopping threads
DemoThread thread1; // First instance of DemoThread
DemoThread thread2; // Second instance of DemoThread
/* Private class implementing the thread to be started by the
* main application */
private class DemoThread extends Thread {
int loops;
public DemoThread(int waitTime)
{
/* Store number of loops to execute */
loops = waitTime;
System.out.println("Thread(" + loops + "): Created");
}
public void run()
{
System.out.println("Thread(" + loops + "): Started");
for (int i = 1; i <= loops; i++) {
/* Check if main application asked thread to die */
if (runThreads != true) {
System.out.println("Thread(" + loops + "): Stopped from outside");
/* Leave thread */
return;
}
/* Print loop counter and wait 1 second,
* do something useful here instead */
System.out.println("Thread(" + loops + "): Loop " + i);
try
{
Thread.sleep(1000);
} catch(InterruptedException e) {
System.out.println(e);
}
}
System.out.println("Thread(" + loops + "): Finished naturally");
}
}
public HelloWorld() {
System.out.println("\nConstructor");
/*
thread1 = new DemoThread(2);
thread2 = new DemoThread(6);
thread1.run();
thread2.run();*/
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
notifyDestroyed();
}
protected void pauseApp() {
//System.out.println("pauseApp()");
}
protected void startApp() throws MIDletStateChangeException {
System.out.println("startApp");
thread1 = new DemoThread(2);
thread2 = new DemoThread(6);
//thread1.run();
//thread2.run();
thread1.start();
thread2.start();
String message = "\nHello World\n";
String telefonnumber="+45xxxxxxxx";
sendSMS(telefonnumber, message);
try
{
Thread.sleep(4000);
} catch(InterruptedException e) {
System.out.println(e);
}
destroyApp(true);
}
protected void sendSMS(String number, String message)
{
//forums states that there has to be a pause after communication (changed to 'static'):
try
{
//SERIAL:
/*
CommConnection comm = (CommConnection)
Connector.open("comm:com0;baudrate=115200;blocking=on");
InputStream is = comm.getInputStream();
OutputStream os = comm.getOutputStream();
* */
CommConnection comm = (CommConnection)
Connector.open("comm:com1;baudrate=115200;blocking=on");
try
{
_atc = new ATCommand(false);
}
catch (ATCommandFailedException ex)
{
//ex.printStackTrace();
}
synchronized ( _atc )
{ // ATC could be shared, we need to sync it
_atc.addListener( this );
try
{
String response="";
/*SEND THIS TO PHONE:
* AT+CPIN="6276"
AT+CMGF=1
AT+CSMP=17,167,0,16
AT+CMGS="+45xxxxxxxx"
Test
* */
//test found online:
synchronized (System.out)
{
System.out.println("Try send simple sms direct " + message + " to " + number);
System.out.notifyAll();
}
//Method 1:
response=_atc.send("AT+CPIN=\"6276\"\r");
System.out.println(response);
Thread.sleep(300);
response=_atc.send("AT+CMGF=1\r");
System.out.println(response);
Thread.sleep(300);
response=_atc.send("AT+CSMP=17,167,0,16\r");
System.out.println(response);
Thread.sleep(300);
response=_atc.send("AT+CMGS=\"+45xxxxxxxx\"\r");
System.out.println(response);
Thread.sleep(300);
if(message.length()<1)
{
response=_atc.send("TESTING1\r");
}else{
response=_atc.send(message+"\r");
}
System.out.println(response);
Thread.sleep(300);
response=_atc.send("<cntr>+z\r");
System.out.println(response);
Thread.sleep(300);
/*eller: ?*/
os.write(response.getBytes(), 0, response.length());
os.flush();
Thread.sleep(300);
//Method2:
String testString="AT+CPIN=\"6276\"" +
"AT+CMGF=1"+
"AT+CSMP=17,167,0,16" +
"AT+CMGS=\"+45xxxxxxxx\"" +
"TESTING2";
os.write(testString.getBytes(), 0, testString.length());
os.flush();
Thread.sleep(300);
//found in example online:
_atc.notifyAll();
Thread.sleep(300);
}catch (ATCommandFailedException ex){
ex.printStackTrace();
}
}
}catch(Exception e){
System.out.println("exception");
e.printStackTrace();
}
}
public void ATEvent(String arg0) {
// TODO Auto-generated method stub
}
public void CONNChanged(boolean arg0) {
// TODO Auto-generated method stub
}
public void DCDChanged(boolean arg0) {
// TODO Auto-generated method stub
}
public void DSRChanged(boolean arg0) {
// TODO Auto-generated method stub
}
public void RINGChanged(boolean arg0) {
// TODO Auto-generated method stub
}
public static void main(String[] args)
{
try
{
example.helloworld.HelloWorld test=new example.helloworld.HelloWorld();
test.startApp();
//test.thread1.start();
//test.startApp();
}catch(Exception ex){
System.out.println("\nError running midlet");
}
}
}
//ReceiveAndTreat();
/*
String addr = "sms://"+number;
MessageConnection conn = (MessageConnection)Connector.open(addr);
TextMessage msg = (TextMessage)conn.newMessage(MessageConnection.TEXT_MESSAGE);
msg.setPayloadText("Hello World!");
conn.send(msg); */