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

📄 btuuidtool.java

📁 BTBrowser,用JAVA API实现蓝牙通信.
💻 JAVA
字号:
package org.klings.wireless.BluetoothNumbers;

import javax.bluetooth.UUID;

/**
 * Translates 128-bit UUIDs to short UUIDs, returns the hex value of a short
 * UUID as String.
 */
public class BTUUIDTool {

	private static final String BTBase = "00001000800000805F9B34FB";
	private static String preHex = "0x0000000";

	/**
	 * Returns the short UUID value of a long UUID.
	 * 
	 * @param uuid
	 *            UUID to be converted to short UUID
	 * @return Short UUID. -1 if the UUID is not a valid short UUID, meaning it
	 *         is not based on the Bluetooth base UUID,
	 *         00000000-0000-1000-8000-00805F9B34FB.
	 */
	public static int shortUUID(UUID uuid) {
		if (uuid == null)
			return -1;
		String id = uuid.toString();
		int offset = id.length() - 24;
		if (id.substring(offset).equals(BTBase)) {
			id = id.substring(0, offset);
		} else
			return -1;
		int result = -1;
		try {
			result = Integer.parseInt(id, 16);
		} catch (NumberFormatException nfe) {
			return -1;
		}
		return result;
	}

	/**
	 * Returns a zero padded hex value representing the short UUID. If you
	 * supply e.g. 1, you will get: 0x0001.
	 * 
	 * @param shortUUID
	 *            The short UUID to be represented as a <code>String</code>
	 * @return The short UUID hex value as <code>String</code>.
	 */
	public static String toHexString(int shortUUID) {
		if (shortUUID < 0xFFFF) {
			String hex = Integer.toHexString(shortUUID);
			hex = preHex.substring(0, 6 - hex.length()) + hex;
			return hex;
		} else {
			String hex = Integer.toHexString(shortUUID);
			hex = preHex.substring(0, 10 - hex.length()) + hex;
			return hex;
		}
	}

}

⌨️ 快捷键说明

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