www.pudn.com > bluetoothj2me.rar > ClientBox.java


import java.io.DataInputStream;  
import java.io.DataOutputStream;  
import java.io.IOException;  
 
import java.util.Vector;  
 
import javax.microedition.io.Connector;  
import javax.microedition.io.StreamConnection;  
import javax.microedition.lcdui.Command;  
import javax.microedition.lcdui.CommandListener;  
import javax.microedition.lcdui.Displayable;  
import javax.microedition.lcdui.Form; 
import javax.microedition.lcdui.Gauge;  
import javax.microedition.lcdui.StringItem;  
import javax.microedition.lcdui.TextField;  
 
//jsr082 API  
import javax.bluetooth.BluetoothStateException;  
import javax.bluetooth.DeviceClass;  
import javax.bluetooth.DiscoveryAgent;  
import javax.bluetooth.DiscoveryListener;  
import javax.bluetooth.LocalDevice;  
import javax.bluetooth.RemoteDevice;  
import javax.bluetooth.ServiceRecord;  
import javax.bluetooth.UUID;  
 
/**  
* 客户端GUI  
* @author Jagie  
*  
* TODO To change the template for this generated type comment go to  
* Window - Preferences - Java - Code style - Code Templates  
*/  
public class ClientBox extends Form implements Runnable, CommandListener,  
       DiscoveryListener {  
 
     
   //字串输入框  
   TextField input = new TextField(null, "", 50, TextField.ANY);  
   //loger  
   StringItem result = new StringItem("结果:", "");  
 
   private DiscoveryAgent discoveryAgent;  
 
     
   private UUID[] uuidSet;  
 
   //响应服务的UUID  
   private static final UUID ECHO_SERVER_UUID = new UUID(  
           "F0E0D0C0B0A000908070605040302010", false);  
 
   //设备集合  
   Vector devices = new Vector();  
   //服务集合  
   Vector records = new Vector();  
     
   //服务搜索的事务id集合  
   int[] transIDs;  
   StupidBTMIDlet midlet;  
 
   public ClientBox(StupidBTMIDlet midlet) {  
       super("");  
       this.midlet=midlet;  
         
       this.append(result);  
         
       this.addCommand(new Command("取消",Command.CANCEL,1));  
       this.setCommandListener(this);  
         
       new Thread(this).start();  
   }  
     
   public void commandAction(Command arg0, Displayable arg1) {  
       if(arg0.getCommandType()==Command.CANCEL){  
           midlet.showMainMenu();  
       }else{  
           //匿名内部Thread,访问远程服务。  
           Thread fetchThread=new Thread(){  
               public void run(){  
                   for(int i=0;i0){  
           this.append(input);  
           this.addCommand(new Command("发送",Command.OK,0));  
       }  
         
       //删除Gauge  
       this.delete(1);  
         
   }  
     
   /**  
    * debug  
    * @param s  
    */  
     
   private void showInfo(String s){  
       StringBuffer sb=new StringBuffer(result.getText());  
       if(sb.length()>0){  
           sb.append("\n");  
       }  
       sb.append(s);  
       result.setText(sb.toString());  
 
   }  
     
   /**  
    * 回调方法  
    */  
 
   public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {  
 
       if (devices.indexOf(btDevice) == -1) {  
           devices.addElement(btDevice);  
       }  
   }  
 
   /**  
    * 回调方法,唤醒初始化线程  
    */  
   public void inquiryCompleted(int discType) {  
 
       synchronized (this) {  
           notify();  
       }  
   }  
   /**  
    * 回调方法  
    */  
   public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {  
       for (int i = 0; i < servRecord.length; i++) {  
           records.addElement(servRecord[i]);  
       }  
   }  
     
   /**  
    * 回调方法,唤醒初始化线程  
    */  
 
   public void serviceSearchCompleted(int transID, int respCode) {  
         
       for (int i = 0; i < transIDs.length; i++) {  
           if (transIDs[i] == transID) {  
               transIDs[i] = -1;  
               break;  
           }  
       }  
         
       //如果所有的设备都已经搜索服务完毕,则唤醒初始化线程。  
 
       boolean finished = true;  
       for (int i = 0; i < transIDs.length; i++) {  
           if (transIDs[i] != -1) {  
               finished = false;  
               break;  
           }  
       }  
 
       if (finished) {  
           synchronized (this) {  
               notify();  
           }  
       }  
 
   }  
 
}