📄 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 + -