Skip to main content

Trying to achieve Mission Impossible: Getting Cell Info Using WMA API

1 reply [Last post]
krishna_adoni
Offline
Joined: 2005-03-15

hi,

In my application i am trying to display location, which is similar to "Cell Info Display Feature", after going through few article i came to know that Cell info will be broad casted to the mobile. so i have written a program which keeps on polling for CBS Message with 1 to 65000 as message id's i.e first the program keeps polling for CBS Message with message id 1 for 15 secs and within that time if the message is not been received then i close that connection and keep polling for CBS Message with next Message id.

but the problem i am facing is, programe is running fine till Message ID 34 and then the mobile getting stuck. i am testing the application in Nokia e60.

here is the source code

/*
*
* Copyright © 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
package example.cbs;

import java.io.IOException;

import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

import javax.wireless.messaging.*;

/**
* An example MIDlet displays text from a CBS MessageConnection
*/
public class CBSReceive extends MIDlet implements CommandListener, Runnable, MessageListener {
/** user interface command for indicating Exit request. */
Command exitCommand = new Command("Exit", Command.EXIT, 2);
Command startCommand = new Command("Start", Command.ITEM, 1);
TextField t1 = new TextField("L. Limit","",5,TextField.DECIMAL);
TextField t2 = new TextField("U. Limit","",5,TextField.DECIMAL);
TextField t3 = new TextField("Result","",10000,TextField.ANY);

/** user interface text box for the "waiting" message. */
Alert waiting;

/** current display. */
Display display;

/** instance of a thread for asynchronous networking and user interface. */
Thread thread;

/** Connections detected at start up. */
String[] connections;

/** Flag to signal end of processing. */
boolean done;

/** The Message Identifier of the CBS messages we're listening for */
//String cbsMessageID;

/** CBS message connection for inbound text messages. */
MessageConnection cbsconn;

/** Current message read from the network. */
Message msg;
Form userform = null;
/** Alert displaying the contents of the received message */
Alert receivedMessage;

//
String cbsConnection = "";
int cbsMessageID = 0;
String strnum = "";
//
CBSReceive midlet;
/**
* Initialize the MIDlet with the current display object and
* graphical components.
*/
public CBSReceive() {
//cbsMessageID = getAppProperty("CBS-Message-Identifier");
display = Display.getDisplay(this);
//waiting = new Alert("CBS Receive", null, null, AlertType.INFO);
// waiting.addCommand(exitCommand);
//waiting.setCommandListener(this);
//waiting.setTimeout(Alert.FOREVER);
midlet = this;

userform = new Form("Input");

userform.append(t1);
userform.append(t2);
userform.append(t3);
userform.addCommand(startCommand);
userform.addCommand(exitCommand);
userform.setCommandListener(this);

/*receivedMessage = new Alert("CBS Received", null, null, AlertType.INFO);
receivedMessage.setTimeout(Alert.FOREVER);
receivedMessage.addCommand(exitCommand);
receivedMessage.setCommandListener(this);*/
}

/**
* Start creates the thread to do the MessageConnection receive
* text.
* It should return immediately to keep the dispatcher
* from hanging.
*/
public void startApp() {
/** CBS connection to be read. */
display.setCurrent(userform);

}

/**
* Notification that a message arrived.
* @param conn the connection with messages available
*/
public void notifyIncomingMessage(MessageConnection conn) {
if (thread == null) {
done = false;
thread = new Thread(this);
thread.start();
}
}

/** Message reading thread. */
public void run() {
/** Check for cbs connection. */
try {
/**
* Loop reading messages and updating the text
* window.
*/
while (!done) {
msg = cbsconn.receive();

if (msg != null) {
if (msg instanceof TextMessage) {
String str = t3.getString()+" "+(((TextMessage)msg).getPayloadText());
t3.setString(str);
} else {
StringBuffer buf = new StringBuffer();
byte[] data = ((BinaryMessage)msg).getPayloadData();

for (int i = 0; i < data.length; i++) {
buf.append(Integer.toHexString((int)data[i]));
buf.append(' ');
}

t3.setString(buf.toString());
}

//display.setCurrent(waiting);
}
}

cbsconn.close();
} catch (IOException e) {
// e.printStackTrace();
strnum = t3.getString()+" IOException";
t3.setString(strnum);
}
}

/**
* Pause signals the thread to stop by clearing the thread field.
* If stopped before done with the iterations it will
* be restarted from scratch later.
*/
public void pauseApp() {
done = true;
thread = null;
}

/**
* Destroy must cleanup everything. The thread is signaled
* to stop and no result is produced.
* @param unconditional true if a forced shutdown was requested
*/
public void destroyApp(boolean unconditional) {
done = true;
thread = null;

if (cbsconn != null) {
try {
cbsconn.close();
} catch (IOException e) {
strnum += " Second "+(e.toString());
t3.setString(strnum);
// Ignore any errors on shutdown
}
}
}
public void startAction()
{
cbsMessageID = Integer.parseInt(t1.getString());
new Thread()
{

public void run()
{
while(cbsMessageID < Integer.parseInt(t2.getString()))
{

cbsConnection = "cbs://:" + (cbsMessageID++);
System.out.println(" "+cbsConnection+" cbsconn "+cbsconn);
/** Open the message connection. */
if (cbsconn == null)
{
try {
cbsconn = (MessageConnection)Connector.open(cbsConnection);
strnum = t3.getString()+" "+cbsMessageID;
t3.setString(strnum);
cbsconn.setMessageListener(midlet);
} catch (IOException ioe) {
strnum += " first";
t3.setString(strnum);
ioe.printStackTrace();
}
}
try
{
Thread.currentThread().sleep(1000*15);
cbsconn = null;
}catch(InterruptedException ex)
{
strnum = t3.getString()+" Interrupted Exception";
t3.setString(strnum);
System.out.println("Inter");
}
}
}
}.start();
//display.setCurrent(waiting);

/** Initialize the text if we were started manually. */
connections = PushRegistry.listConnections(true);

if ((connections == null) || (connections.length == 0)) {
t3.setString("A");//"Waiting for CBS with Message ID " + cbsMessageID + "...");
} else {
done = false;
thread = new Thread(this);
thread.start();
}
}
/**
* Respond to commands, including exit
* @param c user interface command requested
* @param s screen object initiating the request
*/
public void commandAction(Command c, Displayable s) {
try {
if ((c == exitCommand) || (c == Alert.DISMISS_COMMAND))
{
destroyApp(false);
notifyDestroyed();
}
else
{
startAction();
}
} catch (Exception ex)
{
strnum += " Third "+(ex.toString());
t3.setString(strnum);
ex.printStackTrace();
}
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
terrencebarr
Offline
Joined: 2004-03-04

Krishna,

Can you provide more information?

- In which line of code does the application "get stuck"?
- have you tried this on a different device?
- Have you tried to run this code on WTK using the SMS simulation facility?

-- Terrence