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

📄 btbrowsermidlet.java

📁 BTBrowser,用JAVA API实现蓝牙通信.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		deviceScreen(a);
	}

	/*
	 * serviceScreen() will present a list of discovered services to the user
	 */
	private void serviceScreen(Alert a) {
		// Make sure we only add these commands once
		if (currentMenu <= 2) {
			serviceList.setTicker(tic);
			if (serviceSearch == 0) {
				serviceList.addCommand(exitCommand);
				serviceList.addCommand(backCommand);
				serviceList.removeCommand(cancelCommand);
			} else {
				serviceList.addCommand(cancelCommand);
			}
			serviceList.setCommandListener(this);
		}
		if (a == null)
			display.setCurrent(serviceList);
		else
			display.setCurrent(a, serviceList);
		currentMenu = 3;
	}

	/*
	 * showService() will create a recordCanvas showing service attributes to
	 * the user
	 */
	private void showService(int index) {
		ServiceRecord s = (ServiceRecord) serviceVector.elementAt(index);
		if (rCanvas == null) {
			rCanvas = new BluetoothServiceRecordCanvas(s);
			rCanvas.addCommand(exitCommand);
			rCanvas.addCommand(backCommand);
			rCanvas.setCommandListener(this);
		} else {
			rCanvas.setServiceRecord(s);
			rCanvas.removeCommand(openURL);
		}
		clientExecutableURL = rCanvas.getClientExecutableURL();
		documentationURL = rCanvas.getDocumentationURL();
		if (clientExecutableURL != null || documentationURL != null)
			rCanvas.addCommand(openURL);
		// rCanvas.setTicker(tic);
		display.setCurrent(rCanvas);
		currentMenu = 4;
	}

	private void openURLScreen() {
		List list = new List("Open URL", List.IMPLICIT);
		list.append("Documentation", null);
		list.append("Client Executable", null);
		list.addCommand(backCommand);
		list.setCommandListener(this);
		currentMenu = 5;
	}

	private void openURL(String url) {
		boolean mustExit = false;
		Alert a = new Alert("Error", "", null, AlertType.ERROR);
		try {
			mustExit = platformRequest(url);
		} catch (ConnectionNotFoundException e) {
			a.setString("This device cannot open URLs requested by Java "
					+ "programs. See http://www.klings.org/nowires "
					+ "for information.");
			display.setCurrent(a, rCanvas);
			currentMenu = 4;
			return;
		}
		if (mustExit) {
			a.setType(AlertType.INFO);
			a.setTitle("Notification");
			a.setString("The URL MAY be opened when this application exits."
					+ " Choose exit to open URL now.");
			display.setCurrent(a, rCanvas);
			currentMenu = 4;
		}
	}

	public void commandAction(Command arg0, Displayable arg1) {
		// TODO Auto-generated method stub

		if (arg0 == exitCommand)
			try {
				destroyApp(true);
			} catch (MIDletStateChangeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		else if (arg0 == searchCommand) {
			startInquiry();
		} else if (arg0 == backCommand) {
			/*
			 * User wants to go back, check where we are and fulfill the
			 * request.
			 */
			switch (currentMenu) {
			case 2:
				mainMenu(null);
				break;
			case 3:
				deviceScreen(null);
				break;
			case 4:
				serviceScreen(null);
				break;
			case 5:
				display.setCurrent(rCanvas);
				currentMenu = 4;
				break;
			}
		} else if (arg0 == cancelCommand) {
			/*
			 * The user wants to cancel either a device discovery or a service
			 * search. Check where we are, and fulfill the request.
			 */
			switch (currentMenu) {
			case 2:
				if (inquiring) {
					inquiring = !agent.cancelInquiry(this);
					if (inquiring) {
						deviceScreen(new Alert(
								"Error",
								"Could not stop inquiry or inquiry not started",
								null, AlertType.ERROR));
						return;
					}
				} else if (serviceSearch > 0
						&& agent.cancelServiceSearch(serviceSearch)) {
					deviceScreen(new Alert("Status",
							"Service search terminated", null, AlertType.INFO));
				}
				serviceSearch = 0;
				break;
			case 3:
				if (!agent.cancelServiceSearch(serviceSearch)) {
					serviceScreen(new Alert("Error",
							"No active service search", null, AlertType.ERROR));
				}
				break;
			}
		} else if (arg0 == openURL) {
			openURLScreen();
		} else if (arg0 == List.SELECT_COMMAND) {
			/*
			 * The user has selected something from a list. Find out where we
			 * are, get the List currently displayed and act according to this.
			 */
			List list = (List) display.getCurrent();
			int index = list.getSelectedIndex();
			switch (currentMenu) {
			case 1: // Main list of known devices
			case 2: // List of newly found devices
				if (serviceSearch == 0 && !deviceVector.isEmpty())
					startServiceSearch((RemoteDevice) deviceVector
							.elementAt(index));
				break;
			case 3: // Browse service
				if (!serviceVector.isEmpty())
					showService(index);
				break;
			case 5: // The user wants to open an URL
				switch (index) {
				case 0:
					openURL(documentationURL);
					break;
				case 1:
					openURL(clientExecutableURL);
					break;
				}
			}// End switch for list-index
		}// End if for List-command

	}

	public void deviceDiscovered(RemoteDevice arg0, DeviceClass arg1) {
		// TODO Auto-generated method stub

		// Add device to vector in case of further use
		deviceVector.addElement(arg0);

		/*
		 * Add vector to active list, making devices show up as they are added
		 * Add only BT address, since getting the name requires going on air
		 * Will get friendly name later, device is probably quite busy now
		 */
		deviceList.append(arg0.getBluetoothAddress(), null);

	}

	public void inquiryCompleted(int arg0) {
		// TODO Auto-generated method stub

		inquiring = false;
		Alert a = new Alert("Inquiry status", null, null, AlertType.INFO);
		switch (arg0) {
		/*
		 * If inquiry completed normally, give the user an alert stating that no
		 * devices were found, or the number of devices discovered. Also,
		 * retrive friendly names, if devices were discoverd.
		 */
		case DiscoveryListener.INQUIRY_COMPLETED:
			if (deviceVector.size() == 0) {
				a.setString("No devices found!");
				deviceList.append("Empty", null);
			} else {
				getFriendlyNames();
				a.setString(deviceVector.size() + " devices found!");
			}
			deviceScreen(a);
			break;
		/*
		 * Alert the user if an error occured during device discovery (inquiry).
		 * Show list of devices found before error occured.
		 */
		case DiscoveryListener.INQUIRY_ERROR:
			a.setType(AlertType.ERROR);
			if (deviceVector.size() > 0) {
				a.setString("Error occured, but " + deviceVector.size()
						+ " devices found anyway!");
				getFriendlyNames();
				deviceScreen(a);
			} else {
				a.setString("Error occured during inquiry.");
				mainMenu(a);
			}
			break;
		/*
		 * If the user requests termination of the device discovery (inquiry),
		 * alert the user that the process is actually terminated.
		 */
		case DiscoveryListener.INQUIRY_TERMINATED:
			a.setString("Search terminated");
			if (deviceVector.size() > 0) {
				getFriendlyNames();
				deviceScreen(a);
			} else {
				mainMenu(a);
			}
			break;
		}
	}

	/*
	 * the arg0 parameter identifies a particular service search, the arg1
	 * indicates why the service search is ended.
	 */
	public void serviceSearchCompleted(int arg0, int arg1) {
		// TODO Auto-generated method stub

		/*
		 * serviceSearch is a handle to the active service search. Set this to 0
		 * since the search is ended.
		 */
		serviceSearch = 0;
		Alert a = new Alert("Search status", null, null, AlertType.INFO);
		switch (arg1) {
		/*
		 * Service search completed normally. Show the user the results.
		 */
		case DiscoveryListener.SERVICE_SEARCH_COMPLETED:
			a.setString("Service search complete");
			serviceScreen(a);
			break;
		/*
		 * The remote device was not reachable, making it really hard to search
		 * for services.
		 */
		case DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE:
			a.setString("Device not reachable");
			deviceScreen(a);
			break;
		/*
		 * Some error occured during service search, alert the user.
		 */
		case DiscoveryListener.SERVICE_SEARCH_ERROR:
			a.setType(AlertType.ERROR);
			a.setString("Error during service search");
			deviceScreen(a);
			break;
		/*
		 * No service were returned by the remote device, alert the user.
		 */
		case DiscoveryListener.SERVICE_SEARCH_NO_RECORDS:
			a.setString("No services found");
			deviceScreen(a);
			break;
		/*
		 * Service search termination requested by user, alert the user that the
		 * search is indeed terminated.
		 */
		case DiscoveryListener.SERVICE_SEARCH_TERMINATED:
			a.setString("Search terminated");
			deviceScreen(a);
			break;
		}

	}

	public void servicesDiscovered(int arg0, ServiceRecord[] arg1) {
		// TODO Auto-generated method stub

		DataElement nameElement = null;
		synchronized (serviceVector) {
			for (int i = 0; i < arg1.length; i++) {
				nameElement = (DataElement) arg1[i].getAttributeValue(0x100);
				if (nameElement != null
						&& nameElement.getDataType() == DataElement.STRING) {
					serviceList.append((String) nameElement.getValue(), null);
					serviceVector.addElement(arg1[i]);
				}
			}
		}
	}

}

⌨️ 快捷键说明

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