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

📄 l2capserver.java

📁 一个关于蓝牙L2CAP通信代码
💻 JAVA
字号:
import java.io.IOException;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.L2CAPConnection;
import javax.bluetooth.L2CAPConnectionNotifier;
import javax.bluetooth.LocalDevice;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class L2CAPServer extends MIDlet 
			implements  Runnable, CommandListener {
    private Display display = null;
    
    private Form form = new Form("蓝牙 - L2CAP服务器演示");
    
    //定义使用的命令按钮
    private Command cmdExit = new Command("退出", Command.EXIT, 0);
    
    //线程运行标志
    private boolean isRunning = true;
    
   
    
    public L2CAPServer() {
        super();

        form.addCommand(cmdExit);
        form.setCommandListener(this);
    }

    protected void startApp() throws MIDletStateChangeException {
        display = Display.getDisplay(this);
        display.setCurrent(form);
        
        //开始服务器线程
        new Thread(this).start();
    }

    protected void pauseApp() {
        //暂停时要关闭Socket连接
    	isRunning = false;
    }

    protected void destroyApp(boolean arg0)
        throws MIDletStateChangeException {
    	isRunning = false;
    }
    
    /**
     * 处理命令按钮事件
     */
    public void commandAction(Command cmd, Displayable d) {
        if (cmd == cmdExit) {
            isRunning = false;
            notifyDestroyed();
        }
    }
    
    /**
     * 服务器线程
     */
    public void run() {
        isRunning = true;
        try {
            LocalDevice local = LocalDevice.getLocalDevice();
            local.setDiscoverable(DiscoveryAgent.GIAC);
        } catch (BluetoothStateException e) {
            System.err.println("不能开始服务\n");
            System.err.println("BluetoothStateException: " + e.getMessage());
            return;
        }

        L2CAPConnectionNotifier server = null;
    	try {
    		server = (L2CAPConnectionNotifier)Connector.open(
    		"btl2cap://localhost:11111111111111111111111111111111");
    	} catch (IOException e) {
    		e.printStackTrace();
    		return;
    	}
        while(isRunning) {   
        	 L2CAPConnection con = null;
        	try {
        		con = server.acceptAndOpen();
        		form.append("与客户端建立连接\n");
        		
        		/*
        		 * 接收数据
        		 */
        		String msg = "";
                int length = con.getReceiveMTU();
                byte[] data = new byte[length];

                if (con.ready()) {
                	length = con.receive(data);
                	
                	while (length != -1) {
                		msg += new String(data, 0, length);
                		try {
                			length = con.receive(data);
                		} catch (IOException e) {
                			break;
                		}
                	}
                	form.append("[R]  " + msg + "\n");
                }
        	} catch (Exception e) {
        		e.printStackTrace();
        	} finally {
                if (con != null) {
                	try {
						con.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
                	con = null;
                }
        		form.append("关闭与客户端的连接\n");
        	}
        }
        
        try {
			server.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -