我正在创建一个蓝牙应用程序。
我创建了一个带有exit命令的简单midlet,我创建了一个用于查找服务和发现设备的线程。这样做时,它会显示一个动画屏幕,我在其中为exit命令添加了父命令Listener。连接成功后,两个用户都用问候语表示(当前屏幕调用父显示方法setCurrent以显示自身)。此屏幕还将CommandListener设置为父级。现在我想添加更多命令。我在这个类中实现了CommandLIstener接口,添加了一些命令但命令不起作用。我不知道出了什么问题。我正在为您提供代码片段以充分描述我的问题:
package name
Imports here
public class MyMidlet extends MIDlet implements
CommandListener {
public CommandListener theListener;
public Display theDisplay;
public Command exitCommand;
public MyMidlet() {
// Retrieve the display for this MIDlet
//Create the initial screen
}
public void startApp() throws MIDletStateChangeException {
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d) {
// Determine if the exit command was selected
if (c == exitCommand) {
//End application here
notifyDestroyed();
} else {
//Start the new thread here
}
}
}
现在这里是类的代码,它由上面的midlet在一个单独的线程中调用;
package here;
imports here
public class MyService implements Runnable, CommandListener {
private MyMidlet parent;
private StreamConnection conn;
private OutputStream output;
private InputStream input;
public Command sendCommand;
private TextField messageToSend
Form form;
public BChatService(boolean isServer, BChatMidlet parent) {
//some stuff here
this.parent = parent;
}
public void run() {
//function for showing animation here
try {
input = conn.openInputStream();
output = conn.openOutputStream();
} catch (IOException e) {
displayError("IO Error",
"An error occurred while opening " +
"the input and output streams" +
"(IOException: " + e.getMessage() + ")");
try {
conn.close();
} catch (Exception ex) {
}
return;
}
// Create the Form here when service is discoverd and greets the users
Command sendCommand = new Command("Send", Command.ITEM, 2);
exitCommand = new Command("Exit", Command.EXIT, 1);
form.addCommand(exitCommand);
form.addCommand(sendCommand);
parent.theDisplay.setCurrent(form);
form.setCommandListener(this);
public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
// End the game
parent.destroyApp(true);
parent.notifyDestroyed();
}
if(c == sendCommand) {
form.append("SOme text here");
}
}
}
当我选择Send命令时,字符串不会以形式附加,也不会退出命令。
可能的原因是什么?
我需要实现此功能......还有其他方法可以实现这一目标吗?
答案 0 :(得分:0)
你确定问题出在命令上吗?我所知道的是,调用openInputStream会阻止整个程序,直到它得到答案或达到超时,即使你在不同的线程上运行它们(不知道为什么)。是否真的建立了连接?