BluetoothConnection.java
上传用户:kyckim
上传日期:2007-12-11
资源大小:332k
文件大小:6k
- package Hiisi;
- import java.io.IOException;
- import java.util.Vector;
- 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.bluetooth.BluetoothStateException;
- 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;
- public class BluetoothConnection implements DiscoveryListener {
- private UUID uuid = new UUID("1101", true);
- private int inquiryMode = DiscoveryAgent.GIAC;
- private int connectionOptions = ServiceRecord.NOAUTHENTICATE_NOENCRYPT;
-
- private Command[] command = new Command[2];
-
- private Vector deviceList;
- private Vector serviceList;
- private String url = null;
-
- private RemoteDevice selectedDevice = null;
-
- String getUrl() {
- return url;
- }
- String setUrl(String s) {
- url = s;
- return url;
- }
- String getSelectedDevice() {
- if(selectedDevice == null) return null;
- else return getDeviceStr(selectedDevice);
- }
-
- BluetoothConnection() {
- }
-
- public void startDeviceInquiry() {
- deviceList = new Vector();
- selectedDevice = null;
- serviceList = new Vector();
- url = null;
- try {
- HiisiMIDlet.mainForm.log("Inquiring bluetooth device...");
- DiscoveryAgent agent = getAgent();
- agent.startInquiry(inquiryMode, this);
- } catch (Exception e) {
- HiisiMIDlet.mainForm.log(e);
- }
- }
-
- public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
- deviceList.addElement(btDevice);
- }
- public void inquiryCompleted(int discType) {
- makeDeviceSelectionGUI();
- }
-
- private void startServiceSearch(RemoteDevice device) {
- serviceList = new Vector();
- url = null;
- try {
- HiisiMIDlet.mainForm.log("Searching Serial Port Profile service...");
- UUID uuids[] = new UUID[] {
- uuid
- };
- getAgent().searchServices(null, uuids, device, this);
- } catch (Exception e) {
- HiisiMIDlet.mainForm.log(e);
- }
- }
- public void servicesDiscovered(int transId, ServiceRecord[] records) {
- for (int i = 0; i < records.length; i++) {
- ServiceRecord rec = records[i];
- serviceList.addElement(rec.getConnectionURL(connectionOptions, false));
- }
- }
-
- public void serviceSearchCompleted(int transID, int respCode) {
- makeServiceSelectionGUI();
- }
-
-
- private void makeDeviceSelectionGUI() {
- final List devices;
- devices = new List("Select a device", List.IMPLICIT);
- for (int i = 0; i < deviceList.size(); i++) devices.append(getDeviceStr((RemoteDevice) deviceList.elementAt(i)), null);
- devices.append("Inquire device again", null);
- devices.append("Abort bluetooth setting", null);
- devices.setCommandListener(
- new CommandListener() {
- public void commandAction(Command c, Displayable d) {
- if(devices.getSelectedIndex() < deviceList.size()) {
- Display.getDisplay(HiisiMIDlet.hiisiMIDlet).setCurrent(HiisiMIDlet.mainForm);
- selectedDevice = (RemoteDevice)deviceList.elementAt(devices.getSelectedIndex());
- startServiceSearch(selectedDevice);
- } else if(devices.getSelectedIndex() == deviceList.size()) {
- Display.getDisplay(HiisiMIDlet.hiisiMIDlet).setCurrent(HiisiMIDlet.mainForm);
- startDeviceInquiry();
- } else if(devices.getSelectedIndex() == deviceList.size() + 1) {
- Display.getDisplay(HiisiMIDlet.hiisiMIDlet).setCurrent(HiisiMIDlet.mainForm);
- HiisiMIDlet.settingForm.setGatewayMode(HiisiMIDlet.settingForm.WAP_MODE);
- HiisiMIDlet.mainForm.log("Hiisi Proxy " + HiisiMIDlet.hiisiMIDlet.settingForm.getGatewayModeString()
- + " is idling...");
- }
- }
- }
- );
- Display.getDisplay(HiisiMIDlet.hiisiMIDlet).setCurrent(devices);
- }
-
- private void makeServiceSelectionGUI() {
- final List services;
- services = new List("Select a service", List.IMPLICIT);
- for (int i = 0; i < serviceList.size(); i++) services.append((String)serviceList.elementAt(i), null);
- services.append("Search service again", null);
- services.append("Abort bluetooth setting", null);
- services.setCommandListener(
- new CommandListener() {
- public void commandAction(Command c, Displayable d) {
- if(services.getSelectedIndex() < serviceList.size()) {
- Display.getDisplay(HiisiMIDlet.hiisiMIDlet).setCurrent(HiisiMIDlet.mainForm);
- setUrl((String)serviceList.elementAt(services.getSelectedIndex()));
- HiisiMIDlet.mainForm.log("Hiisi Proxy " + HiisiMIDlet.hiisiMIDlet.settingForm.getGatewayModeString()
- + " is idling...");
- } else if(services.getSelectedIndex() == serviceList.size()) {
- Display.getDisplay(HiisiMIDlet.hiisiMIDlet).setCurrent(HiisiMIDlet.mainForm);
- startServiceSearch(selectedDevice);
- } else if(services.getSelectedIndex() == serviceList.size() + 1) {
- Display.getDisplay(HiisiMIDlet.hiisiMIDlet).setCurrent(HiisiMIDlet.mainForm);
- HiisiMIDlet.settingForm.setGatewayMode(HiisiMIDlet.settingForm.WAP_MODE);
- HiisiMIDlet.mainForm.log("Hiisi Proxy " + HiisiMIDlet.hiisiMIDlet.settingForm.getGatewayModeString()
- + " is idling...");
- }
- }
- }
- );
- Display.getDisplay(HiisiMIDlet.hiisiMIDlet).setCurrent(services);
- }
-
- private DiscoveryAgent getAgent() {
- try {
- return LocalDevice.getLocalDevice().getDiscoveryAgent();
- } catch (BluetoothStateException e) {
- throw new Error(e.getMessage());
- }
- }
-
- private String getDeviceStr(RemoteDevice btDevice) {
- return getFriendlyName(btDevice) + " - 0x"
- + btDevice.getBluetoothAddress();
- }
- private String getFriendlyName(RemoteDevice btDevice) {
- try {
- return btDevice.getFriendlyName(false);
- } catch (IOException e) {
- return "No name available";
- }
- }
- }