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

📄 btbrowsermidlet.java

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

import java.io.IOException;
import java.util.Vector;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DataElement;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Ticker;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import org.klings.wireless.j2me.*;
import org.klings.wireless.BluetoothNumbers.*;

public class BTBrowserMIDlet extends MIDlet implements CommandListener,
		DiscoveryListener {

	/* Reference to the Display object to do anything useful with UI */
	private Display display = null;
	/* Reference to LocalDevice to do anything concerning Bluetooth */
	private LocalDevice local = null;
	/* Reference to a DiscoveryAgent to do inquiry or service searches */
	private DiscoveryAgent agent = null;
	/* Globally available Vectors for devices and services found */
	private Vector deviceVector = null;
	private Vector serviceVector = null;
	/* Global Ticker used for most screens in this application 滚动条 */
	private Ticker tic = null;
	/*
	 * Global UI list accessible to deviceDiscovered(...) so we can show devices
	 * and services as they are discovered, giving the user a feeling of
	 * progress in the program
	 */
	private List deviceList = null;
	private List serviceList = null;
	private String clientExecutableURL = null;
	private String documentationURL = null;

	private BluetoothServiceRecordCanvas rCanvas = null;

	/*
	 * Global list of interesting service attributes used when populating and
	 * showing service records
	 */
	int[] attrs = { BTServiceAttributeId.SDP_SERVICENAME,
			BTServiceAttributeId.SDP_SERVICEDESCRIPTION,
			BTServiceAttributeId.SDP_PROVIDERNAME,
			BTServiceAttributeId.SDP_SERVICEINFOTIMETOLIVE,
			BTServiceAttributeId.SDP_SERVICEAVAILABILITY,
			BTServiceAttributeId.SDP_BLUETOOTHPROFILEDESCRIPTORLIST,
			BTServiceAttributeId.SDP_DOCUMENTATIONURL,
			BTServiceAttributeId.SDP_CLIENTEEXECUTABLEURL,
			BTServiceAttributeId.SDP_ICONURL, };
	/* Globally available commands used for most menus */
	Command exitCommand = new Command("Exit", Command.EXIT, 2);
	Command searchCommand = new Command("New Search", Command.SCREEN, 1);
	Command backCommand = new Command("Back", Command.BACK, 1);
	Command cancelCommand = new Command("Cancel", Command.CANCEL, 1);
	Command openURL = new Command("Open URL", Command.CANCEL, 2);
	/* Integer to keep track of which menu is active */
	int currentMenu = 0;
	/* Boolean stating if an inquriy is in progress or not */
	boolean inquiring = false;
	/*
	 * Integer to keep track of service searches. Needed to cancel an ongoing
	 * service search
	 */
	int serviceSearch = 0;

	public BTBrowserMIDlet() {
		// TODO Auto-generated constructor stub
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub
		notifyDestroyed();
	}

	protected void pauseApp() {
		// TODO Auto-generated method stub
		display = null;
		local = null;
		// attrs = null;
		tic = null;
	}

	protected void startApp() throws MIDletStateChangeException {
		// TODO Auto-generated method stub
		display = Display.getDisplay(this);
		Alert a = null;
		try {
			local = LocalDevice.getLocalDevice();
		} catch (BluetoothStateException bse) {
			a = new Alert("Bluetooth error",
					"Either Bluetooth must be turned on, or your "
							+ "device does not support JABWT", null,
					AlertType.ERROR);
			a.setTimeout(Alert.FOREVER);
			a.addCommand(exitCommand);
			a.setCommandListener(this);
		}
		tic = new Ticker("By Klings, www.klings.org/nowires");
		mainMenu(a);
	}

	/*
	 * mainMenu() will present the user a list of cached and preknown devices,
	 * and provide a menu to initiate device discovery (inquiry) in order to
	 * find nearby Bluetooth devices
	 */
	private void mainMenu(Alert a) {
		// TODO Auto-generated method stub
		List knownDevices = new List("Cached/known devices", List.IMPLICIT);
		if (deviceVector == null)
			deviceVector = new Vector();
		if (agent == null)
			agent = local.getDiscoveryAgent();

		/* Retrieve PREKNOWN devices and add them to our Vector */
		RemoteDevice[] devices = agent.retrieveDevices(DiscoveryAgent.PREKNOWN);
		String name = null;

		if (devices != null) {
			/*
			 * Synchronize on vector to obtain object lock before loop. Else,
			 * object lock will be obtained every iteration.
			 */
			synchronized (deviceVector) {
				for (int i = devices.length - 1; i >= 0; i--) {
					deviceVector.addElement(devices[i]);
					try {
						name = devices[i].getFriendlyName(false);
					} catch (IOException ioe) {
						name = devices[i].getBluetoothAddress();
					}
					if (name.equals(""))
						name = devices[i].getBluetoothAddress();
					knownDevices.insert(0, name, null);
				}
			} // End synchronized
		}

		/* Retrieve cached devices and add them to our Vector */
		devices = null;
		devices = agent.retrieveDevices(DiscoveryAgent.CACHED);
		if (devices != null) {
			synchronized (deviceVector) {
				for (int i = devices.length - 1; i >= 0; i--) {
					deviceVector.addElement(devices[i]);
					try {
						name = devices[i].getFriendlyName(false);
					} catch (IOException ioe) {
						name = devices[i].getBluetoothAddress();
					}
					if (name.equals(""))
						name = devices[i].getBluetoothAddress();
					knownDevices.insert(0, name, null);
				}
			}
		}

		/* Indicate to user if the Vector is empty */
		if (deviceVector.isEmpty()) {
			knownDevices.append("Empty", null);
		}
		knownDevices.setTicker(tic);
		knownDevices.addCommand(exitCommand);
		knownDevices.addCommand(searchCommand);
		knownDevices.setCommandListener(this);
		/* If we have an Alert, show it... */
		if (a == null)
			display.setCurrent(knownDevices);
		else
			display.setCurrent(a, knownDevices);
		currentMenu = 1;
	}

	/*
	 * deviceScreen() will show the user a list of devices found during device
	 * discovery (inquiry)
	 */
	private void deviceScreen(Alert a) {
		/*
		 * if currentmenu < 3 we are in screen with known/cached devices or
		 * screen with discovered devices and have issued a device search
		 * deviceList is then reinitialized by startInquiry(), hence we must add
		 * these commands again
		 */
		if (currentMenu < 3) {
			deviceList.setTicker(tic);
			if (inquiring) {
				deviceList.setTitle("Please wait...");
				deviceList.addCommand(cancelCommand);
			} else {
				deviceList.setTitle("Devices:");
				deviceList.removeCommand(cancelCommand);
				deviceList.addCommand(exitCommand);
				deviceList.addCommand(searchCommand);
				deviceList.addCommand(backCommand);
			}
			deviceList.setCommandListener(this);
		}
		/* Display Alert if any... */
		if (a == null)
			display.setCurrent(deviceList);
		else
			display.setCurrent(a, deviceList);
		currentMenu = 2;
	}

	/*
	 * startInquiry() will reinitialize the list of devices, and initiate a
	 * device discovery (inquiry) repopulating the list of devices
	 */
	private void startInquiry() {
		Alert a = new Alert("Inquiry status", null, null, AlertType.INFO);
		if (agent == null)
			agent = local.getDiscoveryAgent();
		/* Get rid of old search results in vector and deviceList */
		deviceVector.removeAllElements();
		deviceList = null;
		deviceList = new List("Devices", List.IMPLICIT);
		/* Start the actual inquiry */
		try {
			inquiring = agent.startInquiry(DiscoveryAgent.GIAC, this);
		} catch (BluetoothStateException bse) {
			a.setType(AlertType.ERROR);
			a.setString("Bluetooth error while starting inquiry");
			mainMenu(a);
			return;
		}
		if (inquiring) {
			a.setString("Inquiry started");
			deviceScreen(a);
		} else {
			a.setType(AlertType.ERROR);
			a.setString("Error starting inquiry");
			// With no Inquiry we have no need for this any more.
			deviceList = null;
			mainMenu(a);
		}
	}

	/*
	 * getFriendlyNames() will contact all devices in the deviceVector and
	 * retrieve their friendly names, if available
	 */
	private void getFriendlyNames() {
		String name = null;
		for (int i = deviceVector.size() - 1; i >= 0; i--) {
			try {
				name = ((RemoteDevice) deviceVector.elementAt(i))
						.getFriendlyName(false);
			} catch (IOException ioe) {
				/*
				 * An IOException may occur if the remote device can not be
				 * contacted or the remote device could not provide its name. In
				 * that case we leave the Bluetooth address in the list, and
				 * move on to the next device found.
				 */
				continue;
			}
			if (!name.equals("")) {
				deviceList.set(i, name, null);
			}
		}
	}

	/*
	 * startServiceSearch() will initiate a service search on the supplied
	 * remote device.
	 */
	private void startServiceSearch(RemoteDevice rDevice) {
		Alert a = null;
		// Prepare serviceVector
		if (serviceVector == null)
			serviceVector = new Vector();
		else
			serviceVector.removeAllElements();
		serviceList = null;
		serviceList = new List("", List.IMPLICIT);
		try {
			serviceList.setTitle(rDevice.getFriendlyName(false));
		} catch (IOException ioe) {
			serviceList.setTitle(rDevice.getBluetoothAddress());
		}
		/*
		 * Search for services containing the PublicBrowseRoot UUID (0x1002)
		 * Should give us all public browseable services
		 */
		UUID[] uuids = new UUID[1];
		uuids[0] = new UUID(0x1002);
		/*
		 * Start the actual service search, using the attrs array initialized
		 * earlier, the UUID array and the remote device we want to do a service
		 * search on
		 */
		try {
			serviceSearch = agent.searchServices(attrs, uuids, rDevice, this);
		} catch (BluetoothStateException bse) {
			a = new Alert("Bluetooth error", "Error starting service search",
					null, AlertType.ERROR);
			deviceScreen(a);
			return;
		}
		a = new Alert("Status", "Service search started. Please wait!", null,
				AlertType.INFO);

⌨️ 快捷键说明

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