?? blueserverbox.java
字號:
import javax.microedition.lcdui.*;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.ServiceRecord;
import javax.microedition.io.StreamConnectionNotifier;
import javax.bluetooth.*;
import javax.microedition.io.Connector;
import java.io.*;
import javax.microedition.io.StreamConnection;
import java.util.Vector;
/**
*
* <p>Title: </p>
*
* <p>設置一個藍牙服務器 </p>
* <p> 要設置一個能提供消費服務的藍牙服務器,將有四個主要步驟:</p>
* <p> 1.創建一個你想要提供的可消費服務的服務記錄, </p>
* <p> 2.增加一個新的服務記錄到服務發現數據庫, </p>
* <p> 3.注冊服務。 </p>
* <p> 4.等候客戶端的引入連接。 </p>
* <p> 兩個有重要意義的相關操作: </p>
* <p> 1.修改服務記錄,如果服務屬性能使客戶端可視需要改變; </p>
* <p> 2.當所有的都做了,從SDDB移除服務記錄。 </p>
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author sld
* @version 1.0
*/
public class BlueServerBox extends Form implements Runnable, CommandListener
{
//消息發送框
private TextField textField = null;
//消息反饋框
private StringItem stringItem = null;
//發送按鈕
private Command cmdOk = null;
//退出按鈕
private Command cmdExit = null;
//本地設備類實例
private LocalDevice localDevice = null;
//服務記錄實例
private ServiceRecord serviceRecord = null;
//服務通告實例
private StreamConnectionNotifier notifier = null;
//藍牙服務url
private String url = "btspp://localhost:F0E0D0C0B0A000908070605040302010;name=BTServer;authorize=false";
//連接實例
private StreamConnection streamConnection = null;
//輸出數據流
private DataOutputStream dataOutputStream = null;
//輸入數據流
private DataInputStream dataInputStream = null;
//連接容器
public Vector connect = null;
//接收阻塞標記
private boolean blSend = false;
public BlueServerBox()
{
super("Displayable Title");
textField = new TextField("服務器端","",50,TextField.ANY);
stringItem = new StringItem("反饋:","");
cmdOk = new Command("發送",Command.OK,1);
cmdExit = new Command("退出",Command.EXIT,2);
this.addCommand(cmdOk);
this.addCommand(cmdExit);
this.append(textField);
this.append(stringItem);
this.setCommandListener(this);
connect = new Vector();
new Thread(this).start();
}
public void commandAction(Command command, Displayable displayable)
{
if(command.getCommandType() == Command.EXIT)
{
BlueMIDlet.isClose = true;
this.clear();
BlueMIDlet.quitApp();
}
else if(command.getCommandType() == Command.OK)
{
this.blSend = true;
Thread fetchThread = new Thread()
{
StreamConnection streamConnection = null;
public void run()
{
sendMessage();
}
};
fetchThread.start();
}
}
public void run()
{
try
{
//得到本地設備
this.localDevice = LocalDevice.getLocalDevice();
this.localDevice.setDiscoverable(DiscoveryAgent.GIAC);
}
catch (BluetoothStateException ex)
{
ex.printStackTrace();
}
try
{
//獲得客戶端連接通告
notifier = (StreamConnectionNotifier)Connector.open(this.url);
}
catch (IOException ex1)
{
ex1.printStackTrace();
}
//獲得服務記錄
serviceRecord = localDevice.getRecord(notifier);
StreamConnection conn = null;
try
{
//從通告獲得遠端藍牙設備的連接
conn = notifier.acceptAndOpen();
dataOutputStream = conn.openDataOutputStream();
dataInputStream = conn.openDataInputStream();
}
catch (IOException ex2)
{
}
while(true)
{
if(this.blSend)
{
try
{
synchronized(this)
{
wait();
}
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
this.receiveMessage();
}
}
/**
* 發送消息
*/
public void sendMessage()
{
try
{
// dataOutputStream.writeUTF(textField.getString());
/*
byte[] b = textField.getString().getBytes();
String str = toHexString(b, 0, b.length, false);
b= str.getBytes();
dataOutputStream.write(b, 0, b.length);
*/
String str = textField.getString();
String str1 = toHexString(str);
//dataOutputStream.writeUTF(str1);
byte[] b = str1.getBytes();
dataOutputStream.write(b, 0, b.length);
textField.setString(str1);
}
catch (IOException ex)
{
ex.printStackTrace();
}
this.blSend = false;
synchronized(this)
{
notify();
}
}
/**
* 接收消息
*/
public void receiveMessage()
{
try
{
if (dataInputStream != null)
{
String text = dataInputStream.readUTF();
this.stringItem.setText(text); //This method blocks until input data is available
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
/**
* 斷開連接
*/
private void clear()
{
try
{
dataOutputStream.close();
dataInputStream.close();
}
catch (IOException ex)
{
}
}
// 轉化字符串為十六進制編碼
public static String toHexString(String s)
{
String str="";
for (int i=0;i<s.length();i++)
{
int ch = (int)s.charAt(i);
String s4 = Integer.toHexString(ch);
str = str + s4;
}
return str;
}
//轉化字節數組為十六進制編碼
private String toHexString(byte[] b, int off, int len, boolean shorten)
{
StringBuffer sb = new StringBuffer();
for (int i=off; i<off+len; )
{
if (!shorten)
sb.append("0x");
String s = Integer.toHexString(b[i]).toUpperCase();
if (s.length() == 1)
{
sb.append("0");
sb.append(s);
}
else
sb.append(s.substring(s.length()-2));
if (++i != len)
if (!shorten)
sb.append(" ");
}
return sb.toString();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -