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

📄 btbrowsermidlet.java

📁 提供蓝牙所有服务
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			//  UUID类在蓝牙中,每个服务和服务属性都唯一地由"全球唯一标识符"(UUID)来校验。正如它的名字所暗示的,每一个这样的标识符都要在时空上保证唯一。UUID类可表现为短整形(16或32位)和长整形(128位)UUID。
			//  他提供了分别利用String和16位或32位数值来创建类的构造函数,提供了一个可以比较两个UUID(如果两个都是128位)的方法,还有一个可以转换一个UUID为一个字符串的方法。UUID实例是不可改变的(immutable),只有被UUID标示的服务可以被发现。
			// 在Linux下你用一个命令uuidgen -t可以生成一个UUID值;在Windows下则执行命令uuidgen 。UUID看起来就像如下的这个形式:2d266186-01fb-47c2-8d9f-10b8ec891363。当使用生成的UUID去创建一个UUID对象,你可以去掉连字符。
			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);
		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 {
			//Requests that the device handle (for example, display or install) the indicated URL.
			//使用platformRequest()自动更新MIDlet套件
			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为false代表取消搜索到设备
					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
						//cancelServiceSearch()取消在正在进行中的任何的服务发现搜索操作。
						//蓝牙发现代理在服务发现阶段的不同时候会分别调用DiscoveryListener的服务发现回调方法:
						&& 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 + -