?? clientbox.java
字號:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package com.mir.bluetooth;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 APIimport 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;i<records.size();i++){ ServiceRecord sr=(ServiceRecord)records.elementAt(i); if(accessService(sr)){ //訪問到一個可用的服務即可 break; } } } }; fetchThread.start(); } } private boolean accessService(ServiceRecord sr){ boolean result=false; try { String url = sr.getConnectionURL( ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false); StreamConnection conn = (StreamConnection) Connector.open(url); DataOutputStream dos=conn.openDataOutputStream(); dos.writeUTF(input.getString()); dos.close(); DataInputStream dis=conn.openDataInputStream(); String echo=dis.readUTF(); dis.close(); showInfo("反饋結果是:"+echo); result=true; } catch (IOException e) { } return result; } public synchronized void run() { //發現設備和服務的過程中,給用戶以Gauge Gauge g=new Gauge(null,false,Gauge.INDEFINITE,Gauge.CONTINUOUS_RUNNING); this.append(g); showInfo("藍牙初始化..."); boolean isBTReady = false; try { LocalDevice localDevice = LocalDevice.getLocalDevice(); discoveryAgent = localDevice.getDiscoveryAgent(); isBTReady = true; } catch (Exception e) { e.printStackTrace(); } if (!isBTReady) { showInfo("藍牙不可用"); //刪除Gauge this.delete(1); return; } uuidSet = new UUID[2]; //標志我們的響應服務的UUID集合 uuidSet[0] = new UUID(0x1101); uuidSet[1] = ECHO_SERVER_UUID; try { discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this); } catch (BluetoothStateException e) { } try { //阻塞,由inquiryCompleted()回調方法喚醒 wait(); } catch (InterruptedException e1) { e1.printStackTrace(); } showInfo("設備搜索完畢,共找到"+devices.size()+"個設備,開始搜索服務"); transIDs = new int[devices.size()]; for (int i = 0; i < devices.size(); i++) { RemoteDevice rd = (RemoteDevice) devices.elementAt(i); try { //記錄每一次服務搜索的事務id transIDs[i] = discoveryAgent.searchServices(null, uuidSet, rd, this); } catch (BluetoothStateException e) { continue; } } try { //阻塞,由serviceSearchCompleted()回調方法在所有設備都搜索完的情況下喚醒 wait(); } catch (InterruptedException e1) { e1.printStackTrace(); } showInfo("服務搜索完畢,共找到"+records.size()+"個服務,準備發送請求"); if(records.size()>0){ 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(); } } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -