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

📄 bluepadserver.java

📁 E-WhiteBoard with Bluetooth
💻 JAVA
字号:
//------------------------------------------------------------------
//  ____                      _____      _                           
// / ___|  ___  _ __  _   _  | ____|_ __(_) ___ ___ ___  ___  _ __   
// \___ \ / _ \| '_ \| | | | |  _| | '__| |/ __/ __/ __|/ _ \| '_ \  
//  ___) | (_) | | | | |_| | | |___| |  | | (__\__ \__ \ (_) | | | | 
// |____/ \___/|_| |_|\__, | |_____|_|  |_|\___|___/___/\___/|_| |_| 
//                    |___/                                          
//------------------------------------------------------------------
// Copyright (c) 2003 Sony Ericsson Mobile Communications AB
//                    Research Trinagle Park, NC 27709
//  
// This software is provided "AS IS," without a warranty of any kind. 
// ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, 
// INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A 
// PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
//
//------------------------------------------------------------------
package example.bluetooth;

import javax.bluetooth.*;  
import java.io.IOException;
import javax.microedition.io.*;

/**
 * The BluePadServer starts a BlueTooth server and waits for a client
 * connection to arrive. Handles the server-specific funcionality.
 *
 * @author Paul H. Nichols
 * @version 1.0
 */
class BluePadServer extends Thread {

    private BluePad bluePad;
    private L2CAPConnection conn = null;
    L2CAPConnectionNotifier server = null;


    public BluePadServer(BluePad pad) {
        this.bluePad = pad;
        this.start();
    }

    public void run() {
        try {
            String message = "";
            byte[] data = null;
            int length;

            LocalDevice local = LocalDevice.getLocalDevice();
            local.setDiscoverable(DiscoveryAgent.GIAC);
            server = (L2CAPConnectionNotifier)Connector.open("btl2cap://localhost:" + BluePad.MY_SERVICE_NUMBER );

            bluePad.printString("Starting Server");

            try {
                conn = server.acceptAndOpen();
                length = conn.getReceiveMTU();

                // Set state to OPEN
                bluePad.setStateConnected(conn);

                data = new byte[length];

                length = conn.receive(data);

                while (length != -1) {
                    bluePad.receiveData(data, 0, length);

                    try {
                        length = conn.receive(data);
                    }
                    catch (IOException e) {
                        break;
                    }
                }
            }
            catch (IOException e) {
                bluePad.printString("IOException: " + e.getMessage());
            }
            finally {
                close();
            }   
        }
        catch (Exception e) {
            bluePad.printString("Exception: " + e);
        }
    } // run  


    /**
     * Close down the server and notify the bluePad that the
     * state is now disconnected.
     */
    public void close() {
        if (conn != null) {

            try {
                conn.close();
            }
            catch (IOException e) {
            }
        }

        if (server != null) {
            try {
                server.close();
            }
            catch (IOException e) {
            }
        }

        conn = null;
        server = null;

        bluePad.setStateDisconnected();
    }

}

⌨️ 快捷键说明

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