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

📄 utility.java

📁 欢迎使用蓝牙联网坦克大战
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * 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 javax.bluetooth.*;

import org.apache.log4j.Category;

import java.util.Enumeration;
import java.io.IOException;
import java.util.*;

public class Utility {

	static Category log = Category.getInstance("panci.basicmidlet.Utility");

	private static boolean debugMode = true;
	
	private Utility() {
	}

	public static boolean isDebugMode() {
		return debugMode;
	}

	public static void setDebugMode(boolean debugMode) {
		Utility.debugMode = debugMode;
	}

	public static void log(String s) {
		if (debugMode)
			System.out.println(s);
	}

	public static void error(String s) {
		System.err.println(s);
	}

	public static void printRemoteDevice(RemoteDevice dev, DeviceClass devClass) {
		try {
			System.out.println("Print Remote Device "
					+ dev.getBluetoothAddress());
			System.out.println("Name: " + dev.getFriendlyName(false));
			System.out.println("Auth: " + dev.isAuthenticated()
					+ " Encrypted: " + dev.isEncrypted() + " Trusted: "
					+ dev.isTrustedDevice());

			if (devClass != null) {
				System.out.println("MajorDevice:"
						+ majorToName(devClass.getMajorDeviceClass()));
				System.out.println("MinorDevice:"
						+ minorToName(devClass.getMajorDeviceClass(), devClass
								.getMinorDeviceClass()));
				System.out.println("ServiceClass:");
				String[] str = Utility.majorServiceToName(devClass
						.getServiceClasses());
				for (int i = 0; i < str.length; i++) {
					System.out.println("  " + str[i]);
				}
			}
		} catch (IOException e) {
		}
	}

	public static void printLocalDevice(LocalDevice dev) {
		System.out.println("Print Local Device " + dev.getBluetoothAddress());
		System.out.println("Name: " + dev.getFriendlyName());
		DeviceClass devClass = dev.getDeviceClass();
		if (devClass != null) {
			System.out.println("MajorDevice:"
					+ majorToName(devClass.getMajorDeviceClass()));
			System.out.println("MinorDevice:"
					+ minorToName(devClass.getMajorDeviceClass(), devClass
							.getMinorDeviceClass()));
			System.out.println("ServiceClass:");
			String[] str = Utility.majorServiceToName(devClass
					.getServiceClasses());
			for (int i = 0; i < str.length; i++) {
				System.out.println("  " + str[i]);
			}
		}

	}

	/*
	 public static void printDeviceClass( DeviceClass d )
	 {
	 System.out.println("Print Device Class "+d.toString());
	 }
	 */
	public static void printServiceRecord(ServiceRecord r) {
		int[] ids = r.getAttributeIDs();
		System.out.println("Print Service Record (# of element: " + ids.length
				+ ")");
		System.out.println("Print Service Record URL "
				+ r.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT,
						false));

		for (int i = 0; i < ids.length; i++) {
			DataElement el = r.getAttributeValue(ids[i]);
			printDataElement(el, ids[i], "");
		}
	}

	public static void printDataElement(DataElement e, int id, String indent) {
		int type = e.getDataType();
		if (type == DataElement.DATALT || type == DataElement.DATSEQ) {
			Enumeration enumer = (Enumeration) e.getValue();
			System.out.println(indent + "DataElement[" + idToName(id) + "] "
					+ type + " (# of element: " + e.getSize() + ")");
			while (enumer.hasMoreElements()) {
				DataElement e2 = (DataElement) enumer.nextElement();
				printDataElement(e2, id, indent + "  ");
			}
		} else if (type == DataElement.U_INT_1 || type == DataElement.U_INT_2
				|| type == DataElement.U_INT_4 || type == DataElement.INT_1
				|| type == DataElement.INT_2 || type == DataElement.INT_4
				|| type == DataElement.INT_8) {
			long v = e.getLong();
			System.out.println(indent + "DataElement[" + idToName(id) + "] "
					+ v);
		} else if (type == DataElement.UUID) {
			UUID uuid = (UUID) e.getValue();
			System.out.println(indent + "DataElement[" + idToName(id) + "] "
					+ uuidToName(uuid));
		} else if (type == DataElement.U_INT_8 || type == DataElement.U_INT_16
				|| type == DataElement.INT_16) {
			byte[] v = (byte[]) e.getValue();
			String s = "";
			for (int i = 0; i < v.length; i++) {
				s += Integer.toHexString((int) v[i]);
			}
			System.out.println(indent + "DataElement[" + idToName(id) + "] "
					+ s);

		} else if (type == DataElement.STRING || type == DataElement.URL) {
			String v = (String) e.getValue();
			System.out.println(indent + "DataElement[" + idToName(id) + "] "
					+ v);

		} else if (type == DataElement.BOOL) {
			boolean v = e.getBoolean();
			System.out.println(indent + "DataElement[" + idToName(id) + "] "
					+ String.valueOf(v));

		} else if (type == DataElement.NULL) {
			System.out.println(indent + "DataElement[" + idToName(id)
					+ "] NULL");

		}

	}

	// convert Attribute  ID to human friendly name
	public static String idToName(int id) {
		if (id == 0x0000) {
			return "ServiceRecordHandle";
		} else if (id == 0x0001) {
			return "ServiceClassIDList";

		} else if (id == 0x0002) {
			return "ServiceRecordState";
		} else if (id == 0x0003) {
			return "ServiceID";
		} else if (id == 0x0004) {
			return "ProtocolDescriptorList";
		} else if (id == 0x0005) {
			return "BrowseGroupList";
		} else if (id == 0x0006) {
			return "LanguageBasedAttributeIDList";
		} else if (id == 0x0007) {
			return "ServiceInfoTimeToLive";
		} else if (id == 0x0008) {
			return "ServiceAvailability";
		} else if (id == 0x0009) {
			return "BluetoothProfileDescriptorList";
		} else if (id == 0x000A) {
			return "DocumentationURL";
		} else if (id == 0x000B) {
			return "ClientExecutableURL";
		} else if (id == 0x000C) {
			return "IconURL";
		} else if (id == 0x000D) {
			return "AdditionalProtocol";
		} else if (id == 0x0100) {
			return "ServiceName";
		} else if (id == 0x0101) {
			return "ServiceDescription";
		} else if (id == 0x0102) {
			return "ProviderName";
		} else if (id == 0x0200) {
			/** @todo why the spec say it is GroupID, IpSubnet and VersionNumberList as well? */
			return "GroupID";
		} else if (id == 0x0201) {
			return "ServiceDatabaseState";

		} else if (id == 0x0300) {
			return "ServiceVersion";
		} else if (id == 0x0301) {
			return "ExternalNetwork";
		} else if (id == 0x0302) {
			// @todo or FaxClass1Support in case of Fax Profile
			return "RemoteAudioVolumeControl";
		} else if (id == 0x0303) {
			// @todo or FaxClass2Support in case of Fax Profile
			return "SupportedFormatList";
		} else if (id == 0x0304) {
			return "FaxClass2Support";
		} else if (id == 0x0305) {
			return "AudioFeedbackSupport";
		} else if (id == 0x0306) {
			return "NetworkAddress";
		} else if (id == 0x0307) {
			return "WAPGateway";
		} else if (id == 0x0308) {
			return "HomePageURL";
		} else if (id == 0x0309) {
			return "WAPStackType";
		} else if (id == 0x030A) {
			return "SecurityDescription";
		} else if (id == 0x030B) {
			return "NetAccessType";
		} else if (id == 0x030C) {
			return "MaxNetAccessrate";
		} else if (id == 0x030D) {
			return "IPv4Subnet";
		} else if (id == 0x030E) {
			return "IPv6Subnet";
		} else if (id == 0x0310) {
			return "SupportedCapabalities";
		} else if (id == 0x0311) {
			return "SupportedFeatures";
		} else if (id == 0x0312) {
			return "SupportedFunctions";
		} else if (id == 0x0313) {
			return "TotalImagingDataCapacity";
		} else {
			return "UnknownAttribute(" + id + ")";
		}
	}

	public static String uuidToName(UUID u) {
		if (u.equals(new UUID(0x0001)))
			return "SDP";
		else if (u.equals(new UUID(0x0003)))
			return "RFCOMM";
		else if (u.equals(new UUID(0x0008)))
			return "OBEX";
		else if (u.equals(new UUID(0x000C)))
			return "HTTP";
		else if (u.equals(new UUID(0x0100)))
			return "L2CAP";
		else if (u.equals(new UUID(0x000F)))
			return "BNEP";
		else if (u.equals(new UUID(0x1000)))
			return "ServiceDiscoveryServerServiceClassID";
		else if (u.equals(new UUID(0x1001)))
			return "BrowseGroupDescriptorCerviceClassID";
		else if (u.equals(new UUID(0x1002)))
			return "PublicBrowseGroup";
		else if (u.equals(new UUID(0x1101)))
			return "SerialPort";
		else if (u.equals(new UUID(0x1102)))
			return "LANAccessUsingPPP";
		else if (u.equals(new UUID(0x1103)))
			return "DialupNetworking";
		else if (u.equals(new UUID(0x1104)))
			return "IrMCSync";
		else if (u.equals(new UUID(0x1105)))
			return "OBEX ObjectPushProfile";
		else if (u.equals(new UUID(0x1106)))
			return "OBEX FileTrasnferProfile";
		else if (u.equals(new UUID(0x1107)))
			return "IrMCSyncCommand";
		else if (u.equals(new UUID(0x1108)))
			return "Headset";
		else if (u.equals(new UUID(0x1109)))
			return "CordlessTelephony";
		else if (u.equals(new UUID(0x110A)))
			return "AudioSource";
		else if (u.equals(new UUID(0x1111)))
			return "Fax";
		else if (u.equals(new UUID(0x1112)))
			return "HeadsetAudioGateway";
		else if (u.equals(new UUID(0x1115)))
			return "PersonalAreaNetworkingUser";
		else if (u.equals(new UUID(0x1116)))
			return "NetworkAccessPoint";
		else if (u.equals(new UUID(0x1117)))
			return "GroupNetwork";
		else if (u.equals(new UUID(0x111E)))
			return "Handsfree";
		else if (u.equals(new UUID(0x111F)))
			return "HandsfreeAudioGateway";
		else if (u.equals(new UUID(0x1201)))
			return "GenericNetworking";
		else if (u.equals(new UUID(0x1202)))
			return "GenericFileTransfer";
		else if (u.equals(new UUID(0x1203)))
			return "GenericAudio";
		else if (u.equals(new UUID(0x1204)))
			return "GenericTelephony";
		else
			return u.toString();
	}

	public static String majorToName(int d) {
		if (d == 0x0000)
			return "Miscellaneous";
		else if (d == 0x0100)
			return "Computer";
		else if (d == 0x0200)
			return "Phone";
		else if (d == 0x0300)
			return "LANAccessPoint";
		else if (d == 0x0400)
			return "AudioVideo";
		else if (d == 0x0500)
			return "Peripheral";
		else if (d == 0x0600)
			return "Imaging";
		else if (d == 0x1F00)
			return "Uncategorized";
		else
			return "UnknownMajorDevice(" + d + ")";
	}

	/**
	 *
	 * @param d major device class
	 * @param m minor device class
	 * @return

⌨️ 快捷键说明

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