Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
New Contributor
Posts: 2
Registered: ‎11-19-2010
My Device: Not Specified

SMS Midlet Problem

Hello All,

 

Seems like i am bothering you guys with an obvious question but couldnt help it as i am struggling with it for last few days and couldnt make in or out of it.

 

Application: I am trying to intercept incoming SMS in my application. I dont mind if they end up in inbox afterwards. So the default SMS port sms://:0 works for me.

 

Problem: I am developing using eclipse puglin and the following is how my code looks.I am trying to write the message into a file after receiving it.

 

 

 // Sample message listener program.
 import java.io.*;

import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.io.file.FileConnection;
import javax.wireless.messaging.*;
 
 public class SMSLogApp extends  MIDlet implements MessageListener {
      MessageConnection messconn;
      boolean done;
      Reader reader;
      // Initial tests setup and execution.
      public void startApp() {
         try {
      // Get our receiving port connection.
          messconn = (MessageConnection)Connector.open("sms://:5002");
             // Register a listener for inbound messages.
           messconn.setMessageListener(this);
             // Start a message-reading thread.
             done = false;
          reader = new Reader();
         new Thread(reader).start();
        } catch (IOException e) {
          // Handle startup errors
   }
      }
      // Asynchronous callback for inbound message.
      public void notifyIncomingMessage(MessageConnection conn) {
    	  System.out.println("Calling Message Notifier");
        if (conn == messconn) {
            reader.handleMessage();
    }
      }
      // Required MIDlet method - release the connection and
      // signal the reader thread to terminate.
      public void pauseApp() {
  done = true;
   try {
      messconn.close();
  } catch (IOException e) {
          // Handle errors
   }
      }
      // Required MIDlet method - shutdown.
      // @param unconditional forced shutdown flag
      public void destroyApp(boolean unconditional) {
         done = true;
   try {
      messconn.setMessageListener(null);
             messconn.close();
  } catch (IOException e) {
          // Handle shutdown errors.
         }
      }
      // Isolate blocking I/O on a separate thread, so callback
      // can return immediately.
      class Reader implements Runnable {
    private int pendingMessages = 0;
       
       // The run method performs the actual message reading.
         public void run() {
        while (!done) {
            synchronized(this) {
               if (pendingMessages == 0) {
                        try {
                      wait();
                    } catch (Exception e) {
                            // Handle interruption
                     }
                  }
              pendingMessages--;
                 }
  
            // The benefit of the MessageListener is here.
                 // This thread could via similar triggers be
           // handling other kind of events as well in
            // addition to just receiving the messages.
    
             Message msg = null;
               try {
            	   	msg = messconn.receive();
                 	} 
               catch (InterruptedIOException ex) {
                     ex.printStackTrace();
                 } 
               catch (IOException ex) {
                     ex.printStackTrace();
               }
               
                               
                    
               
              
                 String senderAddress = msg.getAddress(); // Get info from message
                 String timestamp = msg.getTimestamp().toString();
                 
                 
                                  
                 if (msg instanceof TextMessage) {
                     String msgReceived = ((TextMessage) msg).getPayloadText();
                     // Do something with the message here
                     String outputString = senderAddress + " " + timestamp + " " + msgReceived + "\n";
                     //System.out.println("String is " + outputString);
                     System.out.println("Hello 1 " + outputString);
                     try {
                    	 FileConnection fc = (FileConnection)Connector.open("file:///store/home/user/newSMS.txt", Connector.READ_WRITE);
                         // If no exception is thrown, then the URI is valid, but the file may or may not exist.
                         if (fc.exists())
                         {
                        	 //fc.delete();
                         }
                         else
                         {
                        	 fc.create();  // create the file if it doesn't exist
                        	 OutputStream outStream = fc.openOutputStream(); 
                        	 outStream.write(outputString.getBytes());
                        	 outStream.close();
                          	 fc.close();
                        	                     
                         } 
						
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
                 } else if (msg instanceof BinaryMessage) {
                     byte[] msgReceived = ((BinaryMessage) msg).getPayloadData();
                     // do something with the binary message here
                     String outputString = senderAddress + " " + timestamp + " " + msgReceived + "\n";
                     //System.out.println("String is " + outputString);
                     System.out.println("Hello 2 " + outputString);
                     try {
                    	 FileConnection fc = (FileConnection)Connector.open("file:///store/home/user/newSMS.txt", Connector.READ_WRITE);
                         // If no exception is thrown, then the URI is valid, but the file may or may not exist.
                         if (fc.exists())
                         {
                        	 //Upload File and Delete
                        	 //fc.delete();
                         }
                         else
                         {
                        	 fc.create();  // create the file if it doesn't exist
                        	 OutputStream outStream = fc.openOutputStream(); 
                        	 outStream.write(outputString.getBytes());
                        	 outStream.close();
                          	 fc.close();
                        	                     
                         } 
						
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					
                 }
                             

          }
  }
  
    public synchronized void handleMessage() {
          pendingMessages++;
              notify();
   }
       
       }
  }
  

 

 

 

First of all my simulator does not seem to like sms://:0 and therefore need to change it to sms://:5002 to at least make the two simulators talk. Now they are talking (exchanging SMS) alright, but no file is being created and i dont even see System.out.prinln messages in the debug mode. I am using 9800 simlator.

 

Following the KB article

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800429/How_To_...

 

I have tried to modify my jad file to include Push Registry settings and recompiled cod as follows. In the project folder i used rapc on command line and provided it with the jar and (modified) jad file. This over wrote the cod file genearted by the eclipse. I then run the project in the simulator through eclipse. It doesnt help either.

 

My guess is that for some reason my Push-Registry, modified jad, or modified cod is not being registered with the blackberry services. The SMS messages exchange between the simulators works fine (messages land in inbox for both sides) but nothing get intercepted, and hence no file is generated.

 

Moreover i hace tried to run

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800429/How_To_...

 

as is but it gives midlet not found exception DynamicPushRegistr and same goes for the other example quoted above PushRegistry.

 

I have run the above application in debug mode and i do not see any errors, but i also do not see any System.out.prinln messages.

 

Kinldy help