×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    3

    SMS sending, Nokia S40

    Hi!

    It is needed to send sms by pressing a key from the game thread, to wait until sending is complete and depending on result of sending to fulfil certain actions.

    To make this, the synchronized method is called out of the game thread, where the thread of sending message is created, the game thread is transfered into waiting mode. After completing sending the result of sending is set and the game thread awakes.

    The following code is working properly at SonyEricsson (k800, k750), Samsung (U600, D830), Motorola (v3x, v980) and Nokia S60 (6630).

    But at nokias S40 (checked 6111, 6280, 6131) this code's behaviour is a bit strange: by transferring the game thread into waiting mode, it seems that the same happens to the thread of sending message - it is seen from the fact, that sending message request appears only after timeout (i.e. when the game thread is awoke.)

    I wonder, what is really happening in this case and what I am doing wrong.

    Best regards,
    Roman.


    Code:
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.lcdui.Display;
    
    //
    //	GameMidlet
    //
    public class GameMidlet extends MIDlet
    {
    	private static GameMidlet game_midlet;
    	private static GameEngine game_engine;
    
    	private boolean midlet_started = false;
    
    	public GameMidlet()
    	{
    		//
    	}
    
    	public void startApp()
    	{
    		try
    		{
    			if (!midlet_started)
    			{
    				midlet_started = true;
    
    				game_midlet = this;
    
    				game_engine = new GameEngine();
    				game_engine.go();
    
    				Display.getDisplay(this).setCurrent(game_engine);
    			}
    		}
    		catch (Exception e)
    		{
    			//
    		}
    	}
    
    	public void pauseApp()
    	{
    		//
    	}
    
    	public void destroyApp(boolean unconditional)
    	{
    		//
    	}
    
    	public static void exitApp()
    	{
    		game_midlet.destroyApp(false);
    		game_midlet.notifyDestroyed();
    	}
    
    	public static GameMidlet midlet()
    	{
    		return game_midlet;
    	}
    
    	public static GameEngine engine()
    	{
    		return game_engine;
    	}
    
    }
    
    
    import javax.microedition.lcdui.Graphics;
    import javax.microedition.lcdui.game.GameCanvas;
    
    //
    // GameEngine
    //
    public class GameEngine
    	extends GameCanvas
    		implements Runnable
    {
    	private String result = "status";
    
        protected void keyPressed(int key)
    	{
    		switch (key)
    		{
    			case -6:	// left soft key
    				GameMidlet.midlet().exitApp();
    				break;
    
    			case -7:	// right soft key
    				String url = "+5550001";
    				String txt = "short message";
    
    				if (SMS.newSMS().send(url, txt, 10000L))
    				{
    					result = "successfull";
    				}
    				else
    				{
    					result = "failed";
    				}
    				break;
    		}
    	}
    
    
        public GameEngine()
    	{
    		super(false);
    
    		setFullScreenMode(true);
        }
    
        public void run()
    	{
    		Graphics graphics = getGraphics();
    
    		long prevTime;
    		long currTime;
    		long delta;
    
    		try
    		{
    			while (true)
    			{
    				prevTime = System.currentTimeMillis();
    
    				paint(graphics);
    				flushGraphics();
    
    				currTime = System.currentTimeMillis();
    
    				delta = 50 - (currTime - prevTime);
    
    				if (delta > 0)
    					Thread.sleep(delta);
    			}
    		}
    		catch (Exception e)
    		{
    			//
    		}
    	}
    
    
    	public void go()
    	{
    		new Thread(this).start();
        }
    
    
    	public void paint(Graphics g)
    	{
    		g.setColor(0x178bff);
    		g.fillRect(0, 0, g.getClipWidth(), g.getClipHeight());
    
    		g.setColor(0xff0000);
    		g.drawString(result, 0, 0, Graphics.TOP|Graphics.LEFT);
    	}
    
    }
    
    
    
    import javax.wireless.messaging.MessageConnection;
    import javax.wireless.messaging.TextMessage;
    import javax.microedition.io.Connector;
    
    //
    //	SMS
    //
    public class SMS
    	implements Runnable
    {
    	private MessageConnection connection = null;
    	private TextMessage message = null;
    
    	private String url = "";
    	private String msg = "";
    
    	private boolean success = false;
    
    	private synchronized void done()
    	{
    		notify();
    	}
    
    	public SMS()
    	{
    		//
    	}
    
    	public static SMS newSMS()
    	{
    		return new SMS();
    	}
    
    	public synchronized boolean send(String url, String msg, long timeout)
    	{
    		this.url = "sms://" + url;
    		this.msg = msg;
    
    		if (timeout < 0)
    			timeout = 0;
    
    		Thread thread = new Thread(this);
    		thread.start();
    
    		try
    		{
    			wait(timeout);
    		}
    		catch (Exception e)
    		{
    			//
    		}
    
    // this block don't work at Nokia 6280, 6131, 6111 also 
    //		if (thread.isAlive())
    //		{
    //			thread.interrupt();
    //		}
    
    		return success;
    	}
    
    	public void run()
    	{
    		try
    		{
    			connection = (MessageConnection) Connector.open(url);
    			message    = (TextMessage) connection.newMessage("text");
    
    			message.setPayloadText(msg);
    
    			connection.send(message);
    
    			success = true;
    		}
    		catch (Exception e)
    		{
    			success = false;
    		}
    		finally
    		{
    			if (connection != null)
    			{
    				try
    				{
    					connection.close();
    				}
    				catch (Exception e)
    				{
    					//
    				}
    			}
    
    			done();
    		}
    	}
    }

  2. #2
    Nokia Developer Expert
    Join Date
    May 2007
    Location
    Mexico D.F
    Posts
    351

    Re: SMS sending, Nokia S40

    Seems to be a deadlock somehow.
    When we talk about threads not all the phones behave in the same way.

    Why don't you try Thread.sleep(timeout) instead of wait() . If you check there is a difference between the two approaches regarding the lock of the monitor.

    Tell us how it goes
    :Ruben

  3. #3
    Nokia Developer Champion
    Join Date
    Sep 2007
    Posts
    915

    Re: SMS sending, Nokia S40

    Quote Originally Posted by RomkaRomka View Post
    Hi!

    It is needed to send sms by pressing a key from the game thread, to wait until sending is complete and depending on result of sending to fulfil certain actions.

    To make this, the synchronized method is called out of the game thread, where the thread of sending message is created, the game thread is transfered into waiting mode. After completing sending the result of sending is set and the game thread awakes.

    The following code is working properly at SonyEricsson (k800, k750), Samsung (U600, D830), Motorola (v3x, v980) and Nokia S60 (6630).

    But at nokias S40 (checked 6111, 6280, 6131) this code's behaviour is a bit strange: by transferring the game thread into waiting mode, it seems that the same happens to the thread of sending message - it is seen from the fact, that sending message request appears only after timeout (i.e. when the game thread is awoke.)

    I wonder, what is really happening in this case and what I am doing wrong.

    Best regards,
    Roman.


    Code:
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.lcdui.Display;
    
    //
    //	GameMidlet
    //
    public class GameMidlet extends MIDlet
    {
    	private static GameMidlet game_midlet;
    	private static GameEngine game_engine;
    
    	private boolean midlet_started = false;
    
    	public GameMidlet()
    	{
    		//
    	}
    
    	public void startApp()
    	{
    		try
    		{
    			if (!midlet_started)
    			{
    				midlet_started = true;
    
    				game_midlet = this;
    
    				game_engine = new GameEngine();
    				game_engine.go();
    
    				Display.getDisplay(this).setCurrent(game_engine);
    			}
    		}
    		catch (Exception e)
    		{
    			//
    		}
    	}
    
    	public void pauseApp()
    	{
    		//
    	}
    
    	public void destroyApp(boolean unconditional)
    	{
    		//
    	}
    
    	public static void exitApp()
    	{
    		game_midlet.destroyApp(false);
    		game_midlet.notifyDestroyed();
    	}
    
    	public static GameMidlet midlet()
    	{
    		return game_midlet;
    	}
    
    	public static GameEngine engine()
    	{
    		return game_engine;
    	}
    
    }
    
    
    import javax.microedition.lcdui.Graphics;
    import javax.microedition.lcdui.game.GameCanvas;
    
    //
    // GameEngine
    //
    public class GameEngine
    	extends GameCanvas
    		implements Runnable
    {
    	private String result = "status";
    
        protected void keyPressed(int key)
    	{
    		switch (key)
    		{
    			case -6:	// left soft key
    				GameMidlet.midlet().exitApp();
    				break;
    
    			case -7:	// right soft key
    				String url = "+5550001";
    				String txt = "short message";
    
    				if (SMS.newSMS().send(url, txt, 10000L))
    				{
    					result = "successfull";
    				}
    				else
    				{
    					result = "failed";
    				}
    				break;
    		}
    	}
    
    
        public GameEngine()
    	{
    		super(false);
    
    		setFullScreenMode(true);
        }
    
        public void run()
    	{
    		Graphics graphics = getGraphics();
    
    		long prevTime;
    		long currTime;
    		long delta;
    
    		try
    		{
    			while (true)
    			{
    				prevTime = System.currentTimeMillis();
    
    				paint(graphics);
    				flushGraphics();
    
    				currTime = System.currentTimeMillis();
    
    				delta = 50 - (currTime - prevTime);
    
    				if (delta > 0)
    					Thread.sleep(delta);
    			}
    		}
    		catch (Exception e)
    		{
    			//
    		}
    	}
    
    
    	public void go()
    	{
    		new Thread(this).start();
        }
    
    
    	public void paint(Graphics g)
    	{
    		g.setColor(0x178bff);
    		g.fillRect(0, 0, g.getClipWidth(), g.getClipHeight());
    
    		g.setColor(0xff0000);
    		g.drawString(result, 0, 0, Graphics.TOP|Graphics.LEFT);
    	}
    
    }
    
    
    
    import javax.wireless.messaging.MessageConnection;
    import javax.wireless.messaging.TextMessage;
    import javax.microedition.io.Connector;
    
    //
    //	SMS
    //
    public class SMS
    	implements Runnable
    {
    	private MessageConnection connection = null;
    	private TextMessage message = null;
    
    	private String url = "";
    	private String msg = "";
    
    	private boolean success = false;
    
    	private synchronized void done()
    	{
    		notify();
    	}
    
    	public SMS()
    	{
    		//
    	}
    
    	public static SMS newSMS()
    	{
    		return new SMS();
    	}
    
    	public synchronized boolean send(String url, String msg, long timeout)
    	{
    		this.url = "sms://" + url;
    		this.msg = msg;
    
    		if (timeout < 0)
    			timeout = 0;
    
    		Thread thread = new Thread(this);
    		thread.start();
    
    		try
    		{
    			wait(timeout);
    		}
    		catch (Exception e)
    		{
    			//
    		}
    
    // this block don't work at Nokia 6280, 6131, 6111 also 
    //		if (thread.isAlive())
    //		{
    //			thread.interrupt();
    //		}
    
    		return success;
    	}
    
    	public void run()
    	{
    		try
    		{
    			connection = (MessageConnection) Connector.open(url);
    			message    = (TextMessage) connection.newMessage("text");
    
    			message.setPayloadText(msg);
    
    			connection.send(message);
    
    			success = true;
    		}
    		catch (Exception e)
    		{
    			success = false;
    		}
    		finally
    		{
    			if (connection != null)
    			{
    				try
    				{
    					connection.close();
    				}
    				catch (Exception e)
    				{
    					//
    				}
    			}
    
    			done();
    		}
    	}
    }

    Hi,

    As expert told you you can use sleep methond and you can use the mechanism of flag also.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    3

    Re: SMS sending, Nokia S40

    I have already checked method Sleep - the situation with it was the same.

    Another queer fact is that the same behaviour of the threads was observed at Sun's emulator...

    Best regards,
    Roman.

  5. #5
    Nokia Developer Expert
    Join Date
    May 2007
    Location
    Mexico D.F
    Posts
    351

    Re: SMS sending, Nokia S40

    Another suggestion:

    when you create this thread

    Code:
    Thread thread = new Thread(this);
    		thread.start();
    Why don't you try using

    Code:
    thread.join();
    instead of using a timeout.


    Let us know
    :Ruben

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    3

    Re: SMS sending, Nokia S40

    I have tried using methods sleep and join before using wait - the result was the same for all of these methods. Besides, if the handset has real problems with sending SMS, usage of method join can lead to a rather long hanging. Due to this reason I refused of using join, and use wait(timeout).

    Best regards,
    Roman.

Similar Threads

  1. Series 60 Concept Emulator (SDK Beta 0.2 Linux) not working
    By mattbee in forum Mobile Java Tools & SDKs
    Replies: 1
    Last Post: 2003-06-10, 11:43

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×