BluetoothConnection.java
上传用户:kyckim
上传日期:2007-12-11
资源大小:332k
文件大小:6k
源码类别:

通讯/手机编程

开发平台:

Java

  1. package Hiisi;
  2. import java.io.IOException;
  3. import java.util.Vector;
  4. import javax.bluetooth.DeviceClass;
  5. import javax.bluetooth.DiscoveryAgent;
  6. import javax.bluetooth.DiscoveryListener;
  7. import javax.bluetooth.LocalDevice;
  8. import javax.bluetooth.RemoteDevice;
  9. import javax.bluetooth.ServiceRecord;
  10. import javax.bluetooth.UUID;
  11. import javax.bluetooth.BluetoothStateException;
  12. import javax.microedition.lcdui.Command;
  13. import javax.microedition.lcdui.CommandListener;
  14. import javax.microedition.lcdui.Display;
  15. import javax.microedition.lcdui.Displayable;
  16. import javax.microedition.lcdui.List;
  17. public class BluetoothConnection implements DiscoveryListener {
  18. private UUID uuid = new UUID("1101", true);
  19. private int inquiryMode = DiscoveryAgent.GIAC;
  20. private int connectionOptions = ServiceRecord.NOAUTHENTICATE_NOENCRYPT;
  21. private Command[] command = new Command[2];
  22. private Vector deviceList;
  23. private Vector serviceList;
  24. private String url = null;
  25. private RemoteDevice selectedDevice = null;
  26. String getUrl() {
  27. return url;
  28. }
  29. String setUrl(String s) {
  30. url = s;
  31. return url;
  32. }
  33. String getSelectedDevice() {
  34. if(selectedDevice == null) return null;
  35. else return getDeviceStr(selectedDevice);
  36. }
  37. BluetoothConnection() {
  38. }
  39. public void startDeviceInquiry() {
  40. deviceList = new Vector();
  41. selectedDevice = null;
  42. serviceList = new Vector();
  43. url = null;
  44. try {
  45. HiisiMIDlet.mainForm.log("Inquiring bluetooth device...");
  46. DiscoveryAgent agent = getAgent();
  47. agent.startInquiry(inquiryMode, this);
  48. } catch (Exception e) {
  49. HiisiMIDlet.mainForm.log(e);
  50. }
  51. }
  52. public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
  53. deviceList.addElement(btDevice);
  54. }
  55. public void inquiryCompleted(int discType) {
  56. makeDeviceSelectionGUI();
  57. }
  58. private void startServiceSearch(RemoteDevice device) {
  59. serviceList = new Vector();
  60. url = null;
  61. try {
  62. HiisiMIDlet.mainForm.log("Searching Serial Port Profile service...");
  63. UUID uuids[] = new UUID[] {
  64. uuid
  65. };
  66. getAgent().searchServices(null, uuids, device, this);
  67. } catch (Exception e) {
  68. HiisiMIDlet.mainForm.log(e);
  69. }
  70. }
  71. public void servicesDiscovered(int transId, ServiceRecord[] records) {
  72. for (int i = 0; i < records.length; i++) {
  73. ServiceRecord rec = records[i];
  74. serviceList.addElement(rec.getConnectionURL(connectionOptions, false));
  75. }
  76. }
  77. public void serviceSearchCompleted(int transID, int respCode) {
  78. makeServiceSelectionGUI();
  79. }
  80. private void makeDeviceSelectionGUI() {
  81. final List devices;
  82. devices = new List("Select a device", List.IMPLICIT);
  83. for (int i = 0; i < deviceList.size(); i++) devices.append(getDeviceStr((RemoteDevice) deviceList.elementAt(i)), null);
  84. devices.append("Inquire device again", null);
  85. devices.append("Abort bluetooth setting", null);
  86. devices.setCommandListener(
  87. new CommandListener() {
  88. public void commandAction(Command c, Displayable d) {
  89. if(devices.getSelectedIndex() < deviceList.size()) {
  90. Display.getDisplay(HiisiMIDlet.hiisiMIDlet).setCurrent(HiisiMIDlet.mainForm);
  91. selectedDevice = (RemoteDevice)deviceList.elementAt(devices.getSelectedIndex());
  92. startServiceSearch(selectedDevice);
  93. } else if(devices.getSelectedIndex() == deviceList.size()) {
  94. Display.getDisplay(HiisiMIDlet.hiisiMIDlet).setCurrent(HiisiMIDlet.mainForm);
  95. startDeviceInquiry();
  96. } else if(devices.getSelectedIndex() == deviceList.size() + 1) {
  97. Display.getDisplay(HiisiMIDlet.hiisiMIDlet).setCurrent(HiisiMIDlet.mainForm);
  98. HiisiMIDlet.settingForm.setGatewayMode(HiisiMIDlet.settingForm.WAP_MODE);
  99. HiisiMIDlet.mainForm.log("Hiisi Proxy " + HiisiMIDlet.hiisiMIDlet.settingForm.getGatewayModeString()
  100. + " is idling...");
  101. }
  102. }
  103. }
  104. );
  105. Display.getDisplay(HiisiMIDlet.hiisiMIDlet).setCurrent(devices);
  106. }
  107. private void makeServiceSelectionGUI() {
  108. final List services;
  109. services = new List("Select a service", List.IMPLICIT);
  110. for (int i = 0; i < serviceList.size(); i++) services.append((String)serviceList.elementAt(i), null);
  111. services.append("Search service again", null);
  112. services.append("Abort bluetooth setting", null);
  113. services.setCommandListener(
  114. new CommandListener() {
  115. public void commandAction(Command c, Displayable d) {
  116. if(services.getSelectedIndex() < serviceList.size()) {
  117. Display.getDisplay(HiisiMIDlet.hiisiMIDlet).setCurrent(HiisiMIDlet.mainForm);
  118. setUrl((String)serviceList.elementAt(services.getSelectedIndex()));
  119. HiisiMIDlet.mainForm.log("Hiisi Proxy " + HiisiMIDlet.hiisiMIDlet.settingForm.getGatewayModeString()
  120. + " is idling...");
  121. } else if(services.getSelectedIndex() == serviceList.size()) {
  122. Display.getDisplay(HiisiMIDlet.hiisiMIDlet).setCurrent(HiisiMIDlet.mainForm);
  123. startServiceSearch(selectedDevice);
  124. } else if(services.getSelectedIndex() == serviceList.size() + 1) {
  125. Display.getDisplay(HiisiMIDlet.hiisiMIDlet).setCurrent(HiisiMIDlet.mainForm);
  126. HiisiMIDlet.settingForm.setGatewayMode(HiisiMIDlet.settingForm.WAP_MODE);
  127. HiisiMIDlet.mainForm.log("Hiisi Proxy " + HiisiMIDlet.hiisiMIDlet.settingForm.getGatewayModeString()
  128. + " is idling...");
  129. }
  130. }
  131. }
  132. );
  133. Display.getDisplay(HiisiMIDlet.hiisiMIDlet).setCurrent(services);
  134. }
  135. private DiscoveryAgent getAgent() {
  136. try {
  137. return LocalDevice.getLocalDevice().getDiscoveryAgent();
  138. } catch (BluetoothStateException e) {
  139. throw new Error(e.getMessage());
  140. }
  141. }
  142. private String getDeviceStr(RemoteDevice btDevice) {
  143. return getFriendlyName(btDevice) + " - 0x"
  144. + btDevice.getBluetoothAddress();
  145. }
  146. private String getFriendlyName(RemoteDevice btDevice) {
  147. try {
  148. return btDevice.getFriendlyName(false);
  149. } catch (IOException e) {
  150. return "No name available";
  151. }
  152. }
  153. }