📄 blueserverbt.java
字号:
/* * Bluetooth Multiplayer Games Framework * Author: Francesco Panciroli (email fif0302@iperbole.bologna.it) * Copyright (C) 2006 Francesco Panciroli * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */package newpackage;import java.io.IOException;import java.util.Vector;import javax.bluetooth.DiscoveryAgent;import javax.bluetooth.L2CAPConnectionNotifier;import javax.bluetooth.LocalDevice;import javax.bluetooth.RemoteDevice;import javax.bluetooth.UUID;import javax.microedition.io.Connection;import javax.microedition.io.Connector;import javax.microedition.io.StreamConnectionNotifier;import org.apache.log4j.Category;/** * Establishes the BT service and accepts connections */final class BlueServerBT implements Runnable { static Category log = Category.getInstance("panci.bluemgf.BlueServerBT"); /** Describes this service */ private UUID serviceUUID; /** Keeps the local device reference. */ private LocalDevice localDevice; /** Connection notifier. */ private Connection notifier; /** Keeps the parent reference to process specific actions. */ private BlueServerGUI parent; /** Becomes 'true' when this component is finilized. */ private boolean isClosed; /** Main server thread: creates notifier and accepts clients to be processed. */ private Thread serverThread; /** Connections established with clients */ public Vector connections; /** Connection protocol (SPP o L2CAP) */ protected int connectionProtocol; /** * Constructs the bluetooth server, but it is initialized * in the main server thread to "avoid dead lock". */ BlueServerBT(BlueServerGUI parent, int connectionProtocol) { log.debug("Constructor!"); this.parent = parent; this.connectionProtocol = connectionProtocol; serviceUUID = parent.getServiceUUID(); connections = new Vector(); serverThread = new Thread(this); serverThread.start(); } /** * Creates notifier and accepts clients to be processed. */ public void run() { boolean isBTReady = false; try { // create/get a local device localDevice = LocalDevice.getLocalDevice(); // set we are discoverable if (!localDevice.setDiscoverable(DiscoveryAgent.GIAC)) { // Some implementations always return false, even if // setDiscoverable successful // throw new IOException("Can't set discoverable mode..."); } // prepare a URL to create a notifier StringBuffer url; if (connectionProtocol == BlueConfig.PROTOCOL_L2CAP) url = new StringBuffer("btl2cap://"); else url = new StringBuffer("btspp://"); // indicate this is a server url.append("localhost").append(':'); // add the UUID to identify this service url.append(serviceUUID.toString()); // add the name for our service url.append(";name=BlueMIDletServer"); // request all of the client not to be authorized // some devices fail on authorize=true url.append(";authorize=false"); // create notifier now notifier = Connector.open(url.toString()); // remember we've reached this point. isBTReady = true; } catch (Exception e) { log.error("Can't initialize bluetooth: " + e.getMessage()); } parent.completeServerInitialization(isBTReady); // nothing to do if no bluetooth available if (!isBTReady) { return; } // ok, start accepting connections then while (!isClosed) { //StreamConnection conn = null; Connection conn = null; log.debug("Waiting... "); try { if (connectionProtocol == BlueConfig.PROTOCOL_L2CAP) conn = ((L2CAPConnectionNotifier) notifier).acceptAndOpen(); else conn = ((StreamConnectionNotifier) notifier).acceptAndOpen(); } catch (IOException e) { // wrong client or interrupted - continue anyway log.debug("Waiting interrupted: " + e.getMessage()); continue; } if (conn != null) { addNewConnection(conn); } else { log.debug("Exit with null connection!"); } } log.debug("Closed: exit from waiting!"); } /** * Check if the connection belongs to a device we are already connected to. * In that case, skip this connection. * Note: this method works with either SPP and L2CAP! */ private void addNewConnection(Connection conn) { if (!connections.contains(conn)) { try { RemoteDevice connDevice = RemoteDevice.getRemoteDevice(conn); for (int i = 0; i < connections.size(); i++) { Connection connIdx = (Connection) connections.elementAt(i); RemoteDevice connIdxDevice = RemoteDevice.getRemoteDevice(connIdx); if (connDevice.getFriendlyName(false).equals(connIdxDevice.getFriendlyName(false))) { log.debug("Connection to a device already connected: skipping the connection."); return; } } log.debug("Add new connection!"); connections.addElement(conn); parent.connectionReceived(conn); } catch (IOException e) { log.error("Exception in addNewConnection: " + e.getMessage()); } } else { log.debug("Connection already established!"); } } /** * Destroy a work with bluetooth - exits the accepting * thread and close notifier. */ void destroy() { isClosed = true; // finilize notifier work if (notifier != null) { try { notifier.close(); } catch (IOException e) {} // ignore } // wait for server thread is done try { serverThread.join(); } catch (InterruptedException e) {} // ignore }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -