package mini;
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException;
import javax.microedition.io.StreamConnection;
public class BTConnection implements Runnable { private BTMIDlet midlet; private StreamConnection sconn; private DataInputStream dis; private DataOutputStream dos; private Thread receiveThread; private Thread sendThread; private boolean connected = false; private String sendMessage = \ private String getMessage = \ public BTConnection(BTMIDlet midlet, StreamConnection sconn) throws IOException { this.midlet = midlet; this.sconn = sconn; this.dis = sconn.openDataInputStream(); this.dos = sconn.openDataOutputStream(); connected = true; startService(); } public void startService() { receiveThread = new Thread(this); sendThread = new Thread(this); receiveThread.start(); sendThread.start(); } public void close() { if(connected) { connected = false; try { dos.close(); dos = null;
dis.close(); dis = null; sconn.close(); sconn = null; synchronized(this) { notifyAll(); } } catch (IOException e) { System.out.println(\ } } }
public void run() { while(connected) { try { if(Thread.currentThread() == sendThread) { synchronized(this) { try { wait(); } catch (InterruptedException e) { System.out.println(\ midlet.setMessage(\被打断了。。\ } } if(dos != null) { dos.writeUTF(sendMessage); } } else if(Thread.currentThread() == receiveThread) { if(dis != null) { //读取数据,如果流中没有数据,该方法将阻塞当前线程 getMessage = dis.readUTF(); midlet.setMessage(getMessage); } } } catch (IOException ioe) { midlet.setMessage(\连接中断\ close(); } } }
public void sendMessage(String sendMessage) {
this.sendMessage = sendMessage; synchronized(this) { this.notifyAll(); } } }
package mini;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem; import javax.microedition.lcdui.TextField; import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class BTMIDlet extends MIDlet implements CommandListener, Runnable { private Command cmdExit = new Command(\退出\ private Command cmdGo = new Command(\搜索主机\ private Command cmdSend = new Command(\发送\ private Command cmdHost = new Command(\做主机\ private Form window; private StringItem st; private TextField mailBox; private StringItem systemMsg; private StringItem stateStr; private Client client; private Host host; private BTConnection btc = null;
private boolean first = true;
public BTMIDlet() { window = new Form(\端选择模式\ }
window.addCommand(cmdGo); window.addCommand(cmdHost); window.addCommand(cmdSend); window.addCommand(cmdExit); window.setCommandListener(this);
st = new StringItem(\接收信息:\
mailBox = new TextField(\发送信息\systemMsg = new StringItem(\系统信息:\stateStr = new StringItem(\连接状态:\没连接\window.append(mailBox); window.append(st);
window.append(systemMsg);
(Display.getDisplay(this)).setCurrent(window);
protected void destroyApp(boolean arg0) throws MIDletStateChangeException { }
protected void pauseApp() { }
protected void startApp() throws MIDletStateChangeException { }
public void commandAction(Command cmd, Displayable d) { if (first) { if (cmd == cmdGo) { client = new Client(this); window.removeCommand(cmdGo); window.removeCommand(cmdHost);
}
window.append(stateStr);
client.startDiscover();
window.setTitle(\客户端模式\first = false;
(new Thread(this)).start();
if (cmd == cmdHost) { host = new Host(this); window.removeCommand(cmdGo); window.removeCommand(cmdHost);
host.startHost(); window.setTitle(\主机模式\ first = false; } } else { if (cmd == cmdGo) { client.startDiscover(); window.removeCommand(cmdGo); } }
if (cmd == cmdSend) { if (!first) { if (!mailBox.getString().equals(\ if(btc != null) { btc.sendMessage(mailBox.getString()); systemMsg.setText(\连接成功\ } else { systemMsg.setText(\还没找到连接\ } } else { systemMsg.setText(\输入不能为空\ } } }
if(cmd == cmdExit) { notifyDestroyed();
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库J2ME蓝牙通话代码在线全文阅读。
相关推荐: