📄 irobex.java
字号:
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.obex.ClientSession;
import javax.obex.HeaderSet;
import javax.obex.Operation;
import javax.obex.ResponseCodes;
public class IRObex extends MIDlet
implements Runnable, CommandListener{
private Display display = null;
private Form form = new Form("红外连接对象交换");
//定义连接的会话对象
private ClientSession session = null;
//定义传输对象的描述信息类
private HeaderSet head=null;
//定义操作对象
private Operation op=null;
private OutputStream out=null;
//定义OBEX通信的服务器线程对象
IRObexServerThread server = null;
public void startApp() {
display = Display.getDisplay(this);
form.setCommandListener(this);
form.addCommand(new Command("客户端", Command.SCREEN, 1));
form.addCommand(new Command("服务器", Command.SCREEN, 1)) ;
form.addCommand(new Command("退出", Command.EXIT, 1));
form.append("请首先运行服务器程序,再运行客户端程序\n");
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command command,
Displayable displayable) {
if (command.getLabel().equals("退出")) {
if (server != null) {
server.stop();
server = null;
}
destroyApp(true);
notifyDestroyed();
} else if (command.getLabel().equals("客户端")) {
//启动客户端线程
new Thread(this).start();
} else if (command.getLabel().equals("服务器")) {
//启动服务器线程
server = new IRObexServerThread(this);
new Thread(server).start();
}
}
//定义客户端线程的执行方法
public synchronized void run() {
String url = "irdaobex://discover";
String msg = "Hello World";
try {
//建立并打开连接
session = (ClientSession)Connector.open(url);
debug("连接打开");
//创建传输对象的描述头信息集合对象
head = session.createHeaderSet();
head = session.connect(head);
debug("已连接");
//检查返回码
int responseCode = head.getResponseCode();
debug("获得响应码:" + String.valueOf(responseCode));
if (responseCode != ResponseCodes.OBEX_HTTP_OK) {
debug("错误: 响应码错误");
if (session != null)
try {
closeSession();
} catch (Exception ioe) {}
return;
}
head = session.createHeaderSet();
//设置头部信息
head.setHeader(head.LENGTH, new Long(msg.length()));
debug("设置信息长度...OK");
//包含文件的名字,数据应该以这个名字保存
head.setHeader(HeaderSet.NAME, "hello.txt");
debug("设置文件名...OK");
head.setHeader(HeaderSet.TYPE, "text/plain");
debug("设置信息类型...OK");
//初始化PUT请求
op = session.put(head);
debug("初始化PUT请求");
out = op.openDataOutputStream();
debug("获得输出缓冲");
output(msg.getBytes());
debug("发送数据:" + msg);
}catch (Exception e) {
debug("红外传输错误: " + e.toString());
} finally{
closeSession();
}
}
private void output(byte[] b) {
try {
out.write(b);
} catch (Exception e) {}
}
//关闭会话并断开与服务器的连接
private void closeSession() {
if (session != null) {
debug("关闭连接");
try {
out.close();
op.close();
out= null;
op= null;
session.disconnect(null);
session.close();
} catch (Exception e) {
debug("关闭连接时错误:"+e.toString());
}
session = null;
}
}
//向屏幕添加信息的方法
void debug(String msg) {
form.append(msg + "\n");
System.out.println(msg);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -