📄 clientbox.java
字号:
package midlet.bluetoothone;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;
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;
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;
/**
* <p>蓝牙入门示例 --- 客户端应用代码</p>
*
* @author 刘光辉
* @version 2009.02.05
*/
public class ClientBox extends Form implements Runnable, CommandListener, DiscoveryListener {
private static final Command CMD_CANCLE = new Command("取消",Command.CANCEL,1);
private static final Command CMD_OK = new Command("发送",Command.OK,0);
//字符串输入框
private TextField input = new TextField(null, "", 50, TextField.ANY);
//结果
private StringItem result = new StringItem("结果:", "");
//JSR-82规范定义蓝牙无线技术
private DiscoveryAgent discoveryAgent;
private UUID[] uuidSet;
private static final UUID ECHO_SERVER_UUID = new UUID("F0E0D0C0B0A000908070605040302010", false);
//设备集合
private Vector devices = new Vector();
//服务集合
private Vector records = new Vector();
//服务搜索的事务id集合
private int[] transIDs;
private BluetoothMIDlet btMIDlet;
/**
* <p>客户端构造函数<p>
*
*/
public ClientBox(BluetoothMIDlet btMIDlet) {
super("蓝牙客户端");
this.btMIDlet = btMIDlet;
this.append(result);
this.addCommand(CMD_CANCLE);
this.setCommandListener(this);
new Thread(this).start();
}
/**
* <p>命令处理<p>
* @param c
* @param d
*/
public void commandAction(Command c, Displayable d) {
if(c.getCommandType() == Command.CANCEL){
btMIDlet.showMainMenu();
}
else{
//匿名内部Thread,访问远程服务
Thread fetchThread = new Thread()
{
public void run(){
for(int i=0;i<records.size();i++){
ServiceRecord sr = (ServiceRecord) records.elementAt(i);
// System.out.println(sr.getAttributeIDs());
// System.out.println(sr.getHostDevice());
if(accessService(sr)){
//访问到一个可用的服务即可
break;
}
}
}
};
fetchThread.start();
}
}
/**
* <p>访问服务<p>
*
*/
public boolean accessService(ServiceRecord sr){
boolean result = false;
try{
String url = sr.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
StreamConnection sconn = (StreamConnection) Connector.open(url);
DataOutputStream dos = sconn.openDataOutputStream();
dos.writeUTF(input.getString());
dos.close();
DataInputStream dis = sconn.openDataInputStream();
String echo = dis.readUTF();
dis.close();
showInfo("反馈结果是:"+echo);
result = true;
}catch(IOException e){
}
return result;
}
/**
* <p>显示消息<p>
* @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());
}
/**
* <p>客户端线程主体方法<p>
*
*/
public synchronized void run() {
//发现设备和服务的过程中,给用户以Gauge
Gauge gauge = new Gauge(null,false,Gauge.INDEFINITE,Gauge.CONTINUOUS_RUNNING);
this.append(gauge);
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 bse){
}
try{
//阻塞,由inquiryCompleted()回调方法唤醒
wait();
}catch(InterruptedException ie){
ie.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()+"个服务,准备发送请求");
// ServiceRecord rs = (ServiceRecord) records.firstElement();
// showInfo("rs.getHostDevice()是:"+rs.getHostDevice());
// showInfo("rs.getAttributeIDs()是:"+rs.getAttributeIDs());
if(records.size()>0){
this.append(input);
this.addCommand(CMD_OK);
}
//删除Gauge
this.delete(1);
}
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod)
{
if (devices.indexOf(btDevice) == -1)
{
devices.addElement(btDevice);
// System.out.println(btDevice.getBluetoothAddress());
}
}
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 + -