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

📄 irobex.java

📁 人民邮电出版社的《J2ME手机开发入门》全部源代码
💻 JAVA
字号:
/*
 * IRObexClient.java
 *
 * Created on 2006年1月3日, 上午2:40
 */

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;

/**
 *
 * @author  Liu Bin
 * @version
 */
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;
    
    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 + -