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
Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Problem listening to incoming message on a particular port

I want to listen an imcoming message to a particular port.I have done this for other app and i was able to do it efficientlt.But the same code is not working for other app.Here is my code

 

package com.imimobile.common;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.UnsupportedEncodingException;

import javax.microedition.io.Connection;
import javax.microedition.io.Connector;
import javax.microedition.media.Player;
import javax.microedition.media.control.ToneControl;
import javax.microedition.media.control.VolumeControl;
import javax.wireless.messaging.BinaryMessage;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;

public class ListeningThread extends Thread
{
    private boolean _stop;
    StringBuffer sb;
    private Connection _conn; 
    MessageConnection msgConn ;
    String msg = null;
    StringBuffer buf;
    /**
     * Stops this thread from listening for messages
     */
    private synchronized void stop()
    {
        _stop = true;
        
        try 
        {                
            if( _conn != null )
            {                    
                _conn.close(); 
            }
        } 
        catch (IOException ioe) 
        {                
        }
    }   

    /**
     * Listens for incoming messages until stop() is called
     * @see #stop()
     * @see java.lang.Runnable#run()
     */
    public void run()
    {
        try 
        {               
            _conn =  Connector.open("sms://:" + "4444"); 
            for(;;)
            {
                if ( _stop )
                {
                    return;
                }
             msgConn = (MessageConnection)_conn;
              
                notifyIncomingMessage(msgConn);
            }
        } 
        catch (IOException ioe)
        {                             
           ioe.printStackTrace();
        }
    }
    
    public void notifyIncomingMessage(MessageConnection conn) {
        Message m = null;
		try {
			m = msgConn.receive();
		} catch (InterruptedIOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        String address = m.getAddress();
       
             if ( m instanceof TextMessage )
             {
                  TextMessage tm = (TextMessage)m;
                  msg = tm.getPayloadText();
             }
             else if (m instanceof BinaryMessage) {
                  
            	 byte[] data = ((BinaryMessage) m).getPayloadData();

     			// convert Binary Data to Text
     			try {
     				msg = new String(data, "UTF-8");
     			
     			} catch (UnsupportedEncodingException e) {
     				e.printStackTrace();
     			}
             }
             else
                  System.out.println("Invalid Message Format");
             System.out.println("Message body is--"+msg);
             if(msg.equalsIgnoreCase("EN")){
                  UiApplication.getUiApplication().invokeAndWait(new Runnable() {
			 			public void run() {
			 				myAlarm();
			 				
			 			}});
             }    
        
  }

    public void myAlarm()
    {
        try
        {

        	Player player;
        	byte tempo =120; // set tempo to 120 bpm
        	byte d = 16; // eighth-note
        	byte d16 = 12; // eighth-note

        	byte C4 = ToneControl.C4;;
        	byte D4 = (byte) (C4 + 2); // a whole step
        	byte E4 = (byte) (C4 + 4); // a major third
        	byte G4 = (byte) (C4 + 7); // a fifth
        	byte rest = ToneControl.SILENCE; // rest

        	byte[] alarmSequence = { ToneControl.VERSION, 1, // version 1
        		ToneControl.TEMPO, tempo, // set tempo
        		ToneControl.BLOCK_START, 0, // start define "A" section
        		E4, d16, E4, d16, E4, d16, rest, d, rest, d16, 	ToneControl.BLOCK_END, 0, 	ToneControl.PLAY_BLOCK, 0, // play "A" section
        		ToneControl.PLAY_BLOCK, 0, // play "A" section again
        		ToneControl.PLAY_BLOCK, 0, // play "A" section again
        		ToneControl.PLAY_BLOCK, 0, // play "A" section again
        		ToneControl.PLAY_BLOCK, 0, // play "A" section again
        	};

        	try {
        		player = javax.microedition.media.Manager.createPlayer(javax.microedition.media.Manager.TONE_DEVICE_LOCATOR);
        		player.realize();
        		ToneControl tc = (ToneControl) player.getControl("ToneControl");
        		tc.setSequence(alarmSequence);
        		VolumeControl vc = (VolumeControl) player.getControl("VolumeControl");
        		vc.setLevel(120);
        		player.start();
        	} catch (Exception e) {
        		e.printStackTrace();
        	} finally {
        	}
        }//end of try block
        catch(Exception e){
        	
        }
    }
   
}

 Can you tell me what is the issue,that inspite of listening to port number 4444,my incoming message from the server is coming at port 0 ie Inbox

Developer
Posts: 17,019
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Problem listening to incoming message on a particular port

i have no clue how to send a sms to a certain port, maybe you don't send it to 4444 but just as a normal sms?
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: Problem listening to incoming message on a particular port

Simon,the sms is coming from a webserver,and they are sending it to port 4444,bcoz the same sms they are sending to the android version of the app.

Highlighted
Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: Problem listening to incoming message on a particular port

So all i need to do is listen to that incoming sms on port 4444