⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bluetoothechodemo.java

📁 Bluetooth echo demo , to connect an server with j2se !
💻 JAVA
字号:
import javax.bluetooth.*;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.io.*;
import java.io.*;

/** * A pretty straight-forward class with menu that allows you to invoke a bluetooth server or bluetooth client * In keeping this demo simple the remainder the program output is shown in the console area. * */
public final class BluetoothEchoDemo extends MIDlet implements CommandListener {    // Shared UUID by server and client     public static final UUID RFCOMM_UUID = new UUID(0x0003);	
	
  // Soft-keys  private final Command selectCmd = new Command("Select", Command.SCREEN, 2);
  private final Command exitCmd = new Command("Exit", Command.EXIT, 1);          // Menu  private static final String[] menuLabels = { "Server", "Client" };      private final List menu = new List("Bluetooth Echo Demo", List.IMPLICIT, menuLabels, null);  // Classes for Server and Client  private EchoClient echoClient;  private EchoServer echoServer;  public BluetoothEchoDemo() {    menu.addCommand(exitCmd);    menu.addCommand(selectCmd);    menu.setCommandListener(this);  }  public void startApp() {    Display.getDisplay(this).setCurrent(menu);  }
  protected void destroyApp(boolean unconditional) {  }  protected void pauseApp() {
  }  public void commandAction(Command c, Displayable d) {    if (c == exitCmd) {      destroyApp(true);      notifyDestroyed();      return;    }    // Instantiate the select client type    switch (menu.getSelectedIndex()) {      case 0:                   echoServer = new EchoServer();        break;
              case 1: 
       echoClient = new EchoClient();                   break;      default: // this shouldn't occur        break;    };  }   /**  * @abstract to assemble the data in the given stream connection  *           probably best to move this method to a common class  *             * @param Stream Connection  * @return data read through the stream as a string  *  */  public final static String readData(StreamConnection conn) {        InputStream input = null;    byte[] data = null;   
        try {       	      	      input = conn.openInputStream();
      
      // Probably want to throw an exception if length is not greater then 0
      int length = input.read();             data= new byte[length];      length = 0;
      // Assemble data      while (length != data.length) {     
        int ch = input.read(data, length, data.length - length);        if (ch == -1) {          throw new IOException("Can't read data");        }        length += ch;      }          } catch (IOException e) {      System.err.println(e);    } finally {            	      // close input stream	      if (input != null) {        try {          input.close();        } catch (IOException e) {
        }
      }     }    return new String(data);         }   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -