Hi,
Better paste this code in Edit plus or TextPad to be understood.
Doesnt the MIDlet get system resources when run through OTA. What are the points that should be taken care of before doing PR through OTA.
Like classnames in PR entry and the name of the MIDlet should be same etc etc.
When i run it through PR via OTA, the notifyIncomingMessage() is not called, then the code hangs at receive(). The RMS also doesnt seems to be working. It doesn picks up the already saved data.
PR Entry -
mms://:ECGMIDletAppID , ECGMIDlet, *
Here's the code code
----------------------------------------------------------------------
// ECG2
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
import javax.microedition.rms.* ;
import java.util.*;
public class ECGMIDlet extends MIDlet implements CommandListener, MessageListener, Runnable
{
private static byte application_state;
private int patient_selected;
private Display application_display;
private List stored_ECG;
private MessageConnection main_msg_con;
private MMSReceiver receiver;
private Thread receiver_thread;
private boolean new_mms_flag;
private byte[] received_mms_data;
private ECGRMS ECGRMS_object;
private
String[][] main_List_Data;
private boolean application_end;
private boolean run_executed;
public ECGMIDlet()
{
String[] incoming_con = PushRegistry.listConnections(true);
new_mms_flag=false;
try
{
//main_msg_con = (MessageConnection) Connector.open("mms://:ECGMIDletAppID");
//receiver = new MMSReceiver(this,main_msg_con);
//receiver_thread=new Thread(receiver);
if( incoming_con==null || incoming_con.length==0 )
{
application_state=1;
//main_msg_con.setMessageListener(this);
//receiver_thread.start();
}
else
{
application_state=2;
System.out.println("ECGMIDlet() : state=2 SET! ");
//main_msg_con.setMessageListener(this);
//receiver_thread.start();
}
//ECGRMS_object = new ECGRMS();
//application_display=Display.getDisplay(this);
System.out.println("Exiting ECGMIDlet constructor ");
}
catch(Exception e)
{
System.out.println("constructor : " + e);
}
}
public void startApp()
{
try
{
ECGRMS_object = new ECGRMS();
application_display=Display.getDisplay(this);
main_msg_con = (MessageConnection) Connector.open("mms://:ECGMIDletAppID");
receiver = new MMSReceiver(this,main_msg_con);
receiver_thread=new Thread(receiver);
main_msg_con.setMessageListener(this);
System.out.println("StartApp() : open, setMessageListener");
}
catch(Exception e)
{
System.out.println("startApp: open : " + e);
}
if(application_state == 1)
{
//main_msg_con.setMessageListener(this);
receiver_thread.start();
buildMainList();
if(stored_ECG != null)
{
addCmd();
stored_ECG.setCommandListener(this);
application_display.setCurrent(stored_ECG);
}
else
{
System.out.println("No DATA to be shown");
//notifyDestroyed();
}
}
if(application_state == 2)
{
try
{
/*
//main_msg_con.setMessageListener(this);
receiver_thread.start();
while( ! receiver.isRunExecuted())
{
System.out.println("startApp: state=2: sleep");
Thread.sleep(1000);
}
//receiver.setRunExecuted(false);
//application_state=1;
System.out.println("startApp: state=2: before saveData()");
ECGRMS_object.saveData(received_mms_data);
buildMainList();
addCmd();
stored_ECG.setCommandListener(this);
application_display.setCurrent(new Graph(this,received_mms_data));
*/
System.out.println("startApp: state=2 : before buildlist ");
buildMainList();
if(stored_ECG != null)
{
addCmd();
stored_ECG.setCommandListener(this);
application_display.setCurrent(stored_ECG);
}
else
{
System.out.println("No DATA to be shown from startApp");
//notifyDestroyed();
}
Thread t=new Thread(this);
t.start();
System.out.println("Thread started... ");
//while( !run_executed )
{
//System.out.println("startApp (new): state=2: sleep");
//Thread.sleep(1000);
}
//ECGRMS_object.saveData(received_mms_data);
//buildMainList();
//addCmd();
//stored_ECG.setCommandListener(this);
//application_display.setCurrent(new Graph(this,received_mms_data));
}
catch(Exception e)
{
System.out.println("startApp: status=2 : " + e);
}
}
}
public void run()
{
if(application_state == 2)
{
try
{
//MessageConnection msg_con1 = (MessageConnection) Connector.open("mms://:ECGMIDletAppID");
//msg_con.setMessageListener(main_Object_reference);
System.out.println("Before receive : Main");
MultipartMessage received_msg = (MultipartMessage) main_msg_con.receive();
System.out.println("After receive : Main");
MessagePart msg_part = ((MultipartMessage)received_msg).getMessagePart("id1");
fillData(msg_part.getContent(), received_msg.getAddress(), received_msg.getSubject() );
//run_executed=true;
ECGRMS_object.saveData(received_mms_data);
buildMainList();
addCmd();
stored_ECG.setCommandListener(this);
application_display.setCurrent(new Graph(this,received_mms_data));
System.out.println("run : 1: after wait");
}
catch(Exception e)
{
System.out.println("run: state=2 : " + e);
}
}
}
public void notifyIncomingMessage(MessageConnection conn)
{
System.out.println("notifyIncomingMessage() : " + conn.toString());
if(main_msg_con == conn && application_state==1)
{
new_mms_flag=true;
receiver.handleMessage();
processNewMMS();
}
if(main_msg_con == conn && application_state==2)
{
System.out.println("NotifyIncoming msg: state=2");
}
}
public void processNewMMS()
{
try
{
while(!receiver.isRunExecuted())
{
System.out.println("processNewMessage: sleep");
Thread.sleep(100);
}
receiver.setRunExecuted(false);
ECGRMS_object.saveData(received_mms_data);
buildMainList();
addCmd();
stored_ECG.setCommandListener(this);
application_display.setCurrent(new Graph(this,received_mms_data));
}
catch(Exception e)
{
System.out.println("processNewMessage() : " + e);
}
}
public void fillData(byte[] data, String mob_no, String patient_name)
{
// ';' - 59
//Masking - ( byte[index] & 255)
String str= new String(data) + ';' + mob_no + ";" + patient_name ;
received_mms_data = str.getBytes();
}
public void buildMainList()
{
String[] arr = null;
try
{
main_List_Data = ECGRMS_object.retreiveList();
if( main_List_Data != null )
{
arr = new String[main_List_Data.length];
for(int i=0; i<main_List_Data.length; i++)
{
arr[i] = main_List_Data[i][2];
}
stored_ECG = new List("Exclusive", Choice.IMPLICIT, arr, null);
}
else
{
arr=new String[1];
arr[0] = "No Data to be shown.....";
stored_ECG = new List("Exclusive", Choice.IMPLICIT, arr, null);
}
}
catch(Exception e)
{
System.out.println("buildMainList() : " + e );
}
}
public void addCmd()
{
stored_ECG.addCommand(new Command("Exit", Command.BACK, 0));
stored_ECG.addCommand(new Command("View", Command.ITEM, 0));
stored_ECG.addCommand(new Command("Open", Command.ITEM, 0));
stored_ECG.addCommand(new Command("Forward", Command.ITEM, 0));
stored_ECG.addCommand(new Command("Delete",Command.ITEM,0));
}
public void commandAction(Command c, Displayable d)
{
patient_selected= ((List)d).getSelectedIndex() ;
try
{
if(c.getLabel().equals("View"))
{
application_display.setCurrent(new Graph(this, getSelectedECGByteArray( Integer.parseInt(main_List_Data[getSelectedECGIndex()][0]) ) ) );
}
}
catch(Exception e)
{
System.out.println("commandAction : View : " + e);
}
if(c.getLabel().equals("Delete"))
{
application_display.setCurrent(new DeleteECG(this, Integer.parseInt(main_List_Data[getSelectedECGIndex()][0]) ));
}
if(c.getLabel().equals("Forward"))
{
application_display.setCurrent(new ForwardECG(this));
}
}
public ECGRMS getECGRMSObject()
{
return ECGRMS_object;
}
public byte[] getSelectedECGByteArray(int Id) throws RecordStoreException
{
return ECGRMS_object.retreiveData(Id);
}
public int getSelectedECGIndex()
{
return patient_selected;
}
public boolean isApplicationEnd()
{
return application_end;
}
public Display getMainDisplay()
{
return application_display;
}
public int getApplicationState()
{
return application_state;
}
public List getMainList()
{
return stored_ECG;
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
System.out.println("Destroyed");
application_end=true;
receiver.handleMessage();
}
}
// ECGMIDlet END
//----------------------------------------------------------------------------------------------------------
// ECGRMS class Begins
//----------------------------------------------------------------------------------------------------------
class ECGRMS
{
private RecordStore ECG_RecordStore;
private RecordEnumeration record_enumeration;
public ECGRMS()
{
}
public void saveData(byte[] received_mms_data) throws RecordStoreException
{
try
{
System.out.println("1");
ECG_RecordStore = RecordStore.openRecordStore("ECGDatabase", true, RecordStore.AUTHMODE_PRIVATE, false ) ;
System.out.println("2");
ECG_RecordStore.addRecord(received_mms_data, 0, received_mms_data.length) ;
System.out.println("3");
ECG_RecordStore.closeRecordStore();
System.out.println("4");
}
catch (Exception e)
{
System.out.println("saveData() : " + e);
e.printStackTrace() ;
}
}
public void deleteData(int Id) throws RecordStoreException
{
ECG_RecordStore = RecordStore.openRecordStore("ECGDatabase", true, RecordStore.AUTHMODE_PRIVATE, false ) ;
ECG_RecordStore.deleteRecord(Id);
ECG_RecordStore.closeRecordStore();
}
public byte[] retreiveData(int Id) throws RecordStoreException
{
ECG_RecordStore = RecordStore.openRecordStore("ECGDatabase", true, RecordStore.AUTHMODE_PRIVATE, false ) ;
byte[] arr = ECG_RecordStore.getRecord(Id);
String str=new String(arr, 0, 1799);
ECG_RecordStore.closeRecordStore();
return str.getBytes();
}
public String[][] retreiveList() throws RecordStoreException
{
String list[][]=null;
int num_records;
ECG_RecordStore = RecordStore.openRecordStore("ECGDatabase", true, RecordStore.AUTHMODE_PRIVATE, false ) ;
record_enumeration = ECG_RecordStore.enumerateRecords(null,null,false);
num_records = record_enumeration.numRecords();
if( num_records != 0 )
{
list = new String[num_records][3];
int i,j;
i=j=-1;
while( record_enumeration.hasNextElement() )
{
list[++i][0] = "" + record_enumeration.nextRecordId();
}
byte arr[];
for(i=0; i < num_records; i++)
{
arr = ECG_RecordStore.getRecord( Integer.parseInt(list[i][0]) );
String str = new String(arr, 1801, arr.length - 1801 );
list[i][1]= str.substring( 0, str.indexOf(';') );
list[i][2]= str.substring( str.indexOf(';') + 1, str.length() - 1 );
}
}
ECG_RecordStore.closeRecordStore();
return list;
}
}
// ECGRMS class END
//----------------------------------------------------------------------------------------------------------
// ForwardECG class Starts
//----------------------------------------------------------------------------------------------------------
class ForwardECG extends Form implements CommandListener
{
private ECGMIDlet main_Object_reference;
private TextField mobile_no;
public ForwardECG(ECGMIDlet app)
{
super("Forward");
main_Object_reference=app;
append(mobile_no = new TextField("Mobile No. ", "", 11, TextField.NUMERIC));
addCmd();
setCommandListener(this);
}
public String getMobileNumber()
{
return mobile_no.getString();
}
public void addCmd()
{
addCommand(new Command("Back", Command.BACK, 0));
addCommand(new Command("Send", Command.CANCEL, 0));
}
public void commandAction(Command c, Displayable d)
{
if(c.getLabel().equals("Back"))
{
main_Object_reference.getMainDisplay().setCurrent( main_Object_reference.getMainList() );
}
if(c.getLabel().equals("Send"))
{
System.out.println("ECG of " + main_Object_reference.getSelectedECGIndex() + " sent to " + getMobileNumber() );
main_Object_reference.getMainDisplay().setCurrent( main_Object_reference.getMainList() );
}
}
}
// ForwardECG class END
//----------------------------------------------------------------------------------------------------------
// DeleteECG class Begins
//----------------------------------------------------------------------------------------------------------
class DeleteECG extends Form implements CommandListener
{
private ECGMIDlet main_Object_reference;
private int Id;
public DeleteECG(ECGMIDlet app, int Id)
{
super("Delete");
main_Object_reference=app;
this.Id = Id;
append("Are you sure you want to delete " + main_Object_reference.getSelectedECGIndex() + " ?");
addCmd();
setCommandListener(this);
}
public void addCmd()
{
addCommand(new Command("Yes", Command.BACK , 0));
addCommand(new Command("No" , Command.CANCEL, 0));
}
public void commandAction(Command c, Displayable d)
{
try
{
if(c.getLabel().equals("Yes"))
{
main_Object_reference.getECGRMSObject().deleteData(Id);
main_Object_reference.buildMainList();
main_Object_reference.addCmd();
main_Object_reference.getMainList().setCommandListener(main_Object_reference);
main_Object_reference.getMainDisplay().setCurrent( main_Object_reference.getMainList() );
}
if(c.getLabel().equals("No"))
{
main_Object_reference.getMainDisplay().setCurrent( main_Object_reference.getMainList() );
}
}
catch(Exception e)
{
System.out.println("DeleteECG : commandAction: " + e);
}
}
}
// Delete class END
//-----------------------------------------------------------------------------------------------------------
// Graph class starts
//----------------------------------------------------------------------------------------------------------
class Graph extends Canvas implements CommandListener
{
ECGMIDlet main_Object_reference;
int window_x1;
Timer timer;
TimerTask task;
boolean right,left;
private byte[] arr;
public Graph(ECGMIDlet app, byte[] arr)
{
main_Object_reference=app;
this.arr=arr;
timer=new Timer();
setCommandListener(this);
addCmd();
}
public void paint(Graphics g)
{
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
g.drawString("" + main_Object_reference.getMainList().getString(main_Object_reference.getSelectedECGIndex()) ,0,0,Graphics.TOP | Graphics.LEFT);
drawGraph(g,window_x1);
}
public void drawGraph(Graphics g, int window_x1)
{
int x1,y1,x2,y2,j;
int maxX=getWidth();
int maxY=getHeight();
j=0;
x1=j;
y1= ( maxY - (arr[window_x1] & 255) ) / 2;
for(int i=1; i<maxX; i++)
{
x2=++j;
y2= (maxY - (arr[ window_x1 + (3 * (i*2)) ] & 255) ) / 2;
g.drawLine(x1,y1,x2,y2);
x1=x2;
y1=y2;
}
}
public void keyPressed(int keyCode)
{
right=left=false;
task= new TimerTask()
{
public void run()
{
if( ( window_x1 < (12 * ( (300 - getWidth()) / 2 ) ) ) && right==true )
{
window_x1 +=12;
repaint();
}
if( (window_x1 > 0) && left==true)
{
window_x1 -=12;
repaint();
}
}
};
if( ( window_x1 < (12 * ( (300 - getWidth()) / 2 ) ) ) && getGameAction(keyCode) == Canvas.RIGHT )
{
right=true;
timer.schedule(task,0,100);
}
if( (window_x1 > 0) && getGameAction(keyCode) == Canvas.LEFT )
{
left=true;
timer.schedule(task,0,100);
}
}
public void keyReleased(int keyCode)
{
task.cancel();
}
public void keyRepeated(int keyCode)
{
}
private void addCmd()
{
addCommand(new Command("Back", Command.BACK, 0));
addCommand(new Command("Zoom",Command.ITEM,0));
}
public void commandAction(Command c, Displayable d)
{
if(c.getLabel().equals("Back"))
main_Object_reference.getMainDisplay().setCurrent(main_Object_reference.getMainList());
if(c.getLabel().equals("Zoom"))
{
System.out.println("Zoom...");
}
}
}
// Graph class END
//----------------------------------------------------------------------------------------------------------
//MMSSender class & MMSReceiver
//----------------------------------------------------------------------------------------------------------
class MMSSender implements Runnable
{
private ECGMIDlet main_Object_reference;
private MessageConnection msg_con;
private MultipartMessage sending_msg;
private MessagePart msg_part;
private boolean run_executed;
public MMSSender(ECGMIDlet ob, MessageConnection msg_con)
{
this.msg_con=msg_con;
main_Object_reference=ob;
run_executed=false;
}
public boolean isRunExecuted()
{
return run_executed;
}
public void setRunExecuted(boolean executed)
{
run_executed=executed;
}
public void run()
{
try
{
sending_msg= (MultipartMessage)msg_con.newMessage(MessageConnection.MULTIPART_MESSAGE);
}
catch(Exception e)
{
System.out.println("MMSSender: run(): " + e);
}
}
}
class MMSReceiver implements Runnable
{
private ECGMIDlet main_Object_reference;
private MessageConnection msg_con;
private MultipartMessage received_msg;
private MessagePart msg_part;
private boolean run_executed;
public MMSReceiver(ECGMIDlet ob, MessageConnection msg_con)
{
this.msg_con=msg_con;
main_Object_reference=ob;
run_executed=false;
}
public boolean isRunExecuted()
{
return run_executed;
}
public void setRunExecuted(boolean executed)
{
run_executed=executed;
}
public void run()
{
/*
if(main_Object_reference.getApplicationState() == 2)
{
try
{
MessageConnection msg_con1 = (MessageConnection) Connector.open("mms://:ECGMIDletAppID");
//msg_con1.setMessageListener(main_Object_reference);
received_msg= (MultipartMessage) msg_con1.receive();
msg_part=((MultipartMessage)received_msg).getMessagePart("id1");
main_Object_reference.fillData(msg_part.getContent(), received_msg.getAddress(), received_msg.getSubject() );
run_executed=true;
System.out.println("run : 1: after wait");
}
catch(Exception e)
{
System.out.println("run: state=2 : " + e);
}
}*/
if(main_Object_reference.getApplicationState() == 1)
{
while( ! main_Object_reference.isApplicationEnd() )
{
try
{
System.out.println("run : 1: before wait");
synchronized(this)
{
received_msg=null;
msg_part=null;
wait();
}
if(main_Object_reference.isApplicationEnd())
{
break;
}
System.out.println("run: state=1: before receive()");
received_msg= (MultipartMessage) msg_con.receive();
msg_part=((MultipartMessage)received_msg).getMessagePart("id1");
main_Object_reference.fillData(msg_part.getContent(), received_msg.getAddress(), received_msg.getSubject() );
run_executed=true;
System.out.println("run : 1: after wait");
}
catch(Exception e)
{
System.out.println("run : 3 : " + e);
}
}
System.out.println("Exiting run()");
}
}
public synchronized void handleMessage()
{
System.out.println("notifying");
notify();
System.out.println("returning after notifying");
}
}
//----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------