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

📄 bluetoothservicerecordcanvas.java

📁 BTBrowser,用JAVA API实现蓝牙通信.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.klings.wireless.j2me;

import java.util.Enumeration;

import javax.bluetooth.DataElement;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Ticker;

import org.klings.wireless.BluetoothNumbers.BTProtocol;
import org.klings.wireless.BluetoothNumbers.BTServiceAttributeId;
import org.klings.wireless.BluetoothNumbers.BTServiceClass;
import org.klings.wireless.BluetoothNumbers.BTUUIDTool;

/**
 * BluetoothServiceRecordCanvas prints the attributes of a Bluetooth
 * <code>ServiceRecord</code>. Only attributes which are set in the
 * <code>ServiceRecord</code> will be printed. The user may scroll up and down
 * in order to see the details for all attributes. The most common attributes
 * related to the Bluetooth Service Discovery Profile (SDP) are shown. These are
 * (with attribute IDs, in the order they are printed by
 * BluetoothServiceRecordCanvas):
 * <ul>
 * <li>0x0100, ServiceName</li>
 * <li>0x0101, ServiceDescription</li>
 * <li>0x0102, ProviderName</li>
 * <li>0x0000, ServiceRecordHandle</li>
 * <li>0x0003, ServiceID</li>
 * <li>0x0001, ServiceClassIDList</li>
 * <li>0x0004, ProtocolDescriptorList</li>
 * <li>0x0009, BluetoothProfileDescriptorList</li>
 * <li>0x0007, ServiceInfoTimeToLive</li>
 * <li>0x0008, ServiceAvailability</li>
 * <li>0x000A, DocumentationURL</li>
 * <li>0x000B, ClientExecutableURL</li>
 * <li>0x000C, IconURL</li>
 * </ul>
 */
public class BluetoothServiceRecordCanvas extends Canvas {

	/* service record to display */
	ServiceRecord sr = null;
	/* Keep track of where we are in the canvas */
	int y = 0;
	/* Different x values to indent lines */
	final int X1 = 2;
	final int X2 = 5;
	final int X3 = 8;
	/* Anchor for our text */
	final int anchor = Graphics.LEFT | Graphics.TOP;
	/* Different fonts for different types of text */
	Font plain, bold;
	/* Height of fonts */
	int plainHeight, boldHeight;
	/* Dimensions of canvas */
	int canvasHeight, canvasWidth;
	/* Offset used when there are more attributes than space in the canvas */
	int attrOffset = 0;

	/* Keycodes. The user can scroll up and down in the canvas */
	int upKey = getKeyCode(UP);
	int downKey = getKeyCode(DOWN);
	/*
	 * The MIDlet can retrieve these URLs and open them in a wap browser.
	 */
	String clientExecutableURL = null;
	String documentationURL = null;

	public BluetoothServiceRecordCanvas(ServiceRecord s) {
		// TODO Auto-generated constructor stub
		super();
		this.sr = s;
		/* Fonts for Bold and Plain text */
		plain = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
				Font.SIZE_MEDIUM);
		bold = Font
				.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
		/* font heights to compute where to draw. */
		plainHeight = plain.getHeight();
		boldHeight = bold.getHeight();
		/* Canvas dimensions */
		canvasHeight = getHeight();
		canvasWidth = getWidth();
		setTicker(new Ticker("Service Record Info Canvas, by Klings @ "
				+ "http://www.klings.org/nowires --- "));
	}

	protected void paint(Graphics g) {
		/* Initialize the canvas */
		g.setColor(0xffffff);
		g.fillRect(0, 0, getWidth(), getHeight());
		/* We want black text */
		g.setColor(0x000000);
		g.setFont(plain);
		/* Start drawing two pixels from top of screen */
		y = 2;
		DataElement elm = null;
		// temp String
		String out = null;
		int shortUUID = 0;
		int offset = attrOffset;
		/* Get the serviceName */
		elm = (DataElement) sr
				.getAttributeValue(BTServiceAttributeId.SDP_SERVICENAME);
		if (elm != null && elm.getDataType() == DataElement.STRING
				&& offset++ >= 0) {
			out = (String) elm.getValue();
			/* Print servicename */
			y += CanvasHelper.printString("Service name:", X1, y, anchor, bold,
					canvasWidth - X1, g);
			y += CanvasHelper.printString(out, X2, y, anchor, plain,
					canvasWidth - X2, g);
		}
		if (y > canvasHeight)
			return;
		/* Get the serviceDescription */
		elm = (DataElement) sr
				.getAttributeValue(BTServiceAttributeId.SDP_SERVICEDESCRIPTION);
		if (elm != null && elm.getDataType() == DataElement.STRING
				&& offset++ >= 0) {
			out = (String) elm.getValue();
			/* Print serviceDescription */
			y += CanvasHelper.printString("Service description:", X1, y,
					anchor, bold, canvasWidth - X1, g);
			y += CanvasHelper.printString(out, X2, y, anchor, plain,
					canvasWidth - X2, g);
		}
		if (y > canvasHeight)
			return;
		/* Get the providerName */
		elm = (DataElement) sr
				.getAttributeValue(BTServiceAttributeId.SDP_PROVIDERNAME);
		if (elm != null && elm.getDataType() == DataElement.STRING
				&& offset++ >= 0) {
			out = (String) elm.getValue();
			/* Print providerName */
			y += CanvasHelper.printString("Provider name:", X1, y, anchor,
					bold, canvasWidth - X1, g);
			y += CanvasHelper.printString(out, X2, y, anchor, plain,
					canvasWidth - X2, g);
		}
		if (y > canvasHeight)
			return;
		/* Get the serviceRecordHandle */
		elm = (DataElement) sr
				.getAttributeValue(BTServiceAttributeId.SDP_SERVICERECORDHANDLE);
		if (elm != null && elm.getDataType() == DataElement.U_INT_4
				&& offset++ >= 0) {
			long var = elm.getLong();
			out = "0x" + Long.toString(var, 16);
			/* Print serviceRecordHandle */
			y += CanvasHelper.printString("ServiceRecordHandle:", X1, y,
					anchor, bold, canvasWidth - X1, g);
			y += CanvasHelper.printString(out, X2, y, anchor, plain,
					canvasWidth - X2, g);
		}
		if (y > canvasHeight)
			return;
		/* Get the serviceId */
		elm = (DataElement) sr
				.getAttributeValue(BTServiceAttributeId.SDP_SERVICEID);
		if (elm != null && elm.getDataType() == DataElement.UUID
				&& offset++ >= 0) {
			UUID var = (UUID) elm.getValue();
			out = "0x" + var.toString();
			/* Print serviceId */
			y += CanvasHelper.printString("ServiceId:", X1, y, anchor, bold,
					canvasWidth - X1, g);
			y += CanvasHelper.printString(out, X2, y, anchor, plain,
					canvasWidth - X2, g);
		}
		if (y > canvasHeight)
			return;
		/* Get the serviceClassIdList */
		elm = (DataElement) sr
				.getAttributeValue(BTServiceAttributeId.SDP_SERVICECLASSIDLIST);
		if (elm != null && elm.getDataType() == DataElement.DATSEQ
				&& offset++ >= 0) {
			y += CanvasHelper.printString("ServiceClassIdList:", X1, y, anchor,
					bold, canvasWidth - X1, g);
			/* elm should be a DATSEQ of UUIDs */
			DataElement elm2 = null;
			UUID uuid = null;
			try {
				Enumeration e = (Enumeration) elm.getValue();
				while (e.hasMoreElements()) {
					elm2 = (DataElement) e.nextElement();
					if (elm2.getDataType() == DataElement.UUID) {
						uuid = (UUID) elm2.getValue();
						shortUUID = BTUUIDTool.shortUUID(uuid);
						if (shortUUID != -1) {
							out = BTUUIDTool.toHexString(shortUUID)
									+ ", "
									+ BTServiceClass
											.serviceClassName(shortUUID);
						} else {
							out = "0x" + uuid.toString();
						}
						y += CanvasHelper.printString(out, X2, y, anchor,
								plain, canvasWidth - X2, g);
					}
				}
			} catch (ClassCastException cce) {
				y += CanvasHelper.printString("Unpredicted object", X2, y,
						anchor, plain, canvasWidth - X2, g);
			}
		}
		if (y > canvasHeight)
			return;
		/* Get the protocolDescriptorList */
		elm = (DataElement) sr
				.getAttributeValue(BTServiceAttributeId.SDP_PROTOCOLDESCRIPTORLIST);
		if (elm != null && elm.getDataType() == DataElement.DATSEQ
				&& offset++ >= 0) {
			y += CanvasHelper.printString("ProtocolDescriptorList:", X1, y,
					anchor, bold, canvasWidth - X1, g);
			/*
			 * elm should be a DATSEQ of DATSEQ of UUID and optional parameters
			 */
			DataElement elm2 = null;
			DataElement elm3 = null;
			UUID uuid = null;
			try {
				/* Get enumeration to the "outer" DATSEQ */
				Enumeration e = (Enumeration) elm.getValue();
				/* Iterate through the "outer" DATSEQ */
				while (e.hasMoreElements()) {

⌨️ 快捷键说明

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