00001 import java.io.IOException;
00002
00003 import javax.bluetooth.BluetoothStateException;
00004 import javax.bluetooth.DeviceClass;
00005 import javax.bluetooth.DiscoveryAgent;
00006 import javax.bluetooth.LocalDevice;
00007 import javax.bluetooth.RemoteDevice;
00008 import javax.bluetooth.ServiceRecord;
00009 import javax.bluetooth.DiscoveryListener;
00010 import javax.bluetooth.UUID;
00011 import javax.microedition.io.StreamConnection;
00012 import javax.microedition.io.Connector;
00013
00014
00015
00016
00017
00018
00019
00026 public class TicTacComClient extends TicTacCom implements DiscoveryListener {
00028 private DiscoveryAgent discoveryAgent;
00030 private RemoteDevice[] remoteDevices;
00031 private ServiceRecord serviceRecord;
00032
00033 private UUID[] uuidSet;
00034
00036 private String serviceUrl;
00037 int clientResponseCode = -1 ;
00038
00039
00044 public TicTacComClient(TicTacMain p, int r) {
00045 super(p, r);
00046
00047 }
00048
00049 void initCom() throws IOException{
00050 conn = (StreamConnection)Connector.open(serviceUrl);
00051 }
00052
00054 void initBeforeThread(){
00055 try{
00056 initClient();
00057
00058 }catch (BluetoothStateException e) {
00059 System.err.println( "Init "+role+": BluetoothStateException: " + e.getMessage() );
00060 } catch (IOException e) {
00061 System.err.println( "Init "+role+": IOException: " + e.getMessage() );
00062 }catch (Exception e) {
00063 System.err.println( "Init "+role+": Exception: " + e.getMessage() );
00064 }
00065
00066 }
00067
00069 void initClient() throws Exception {
00070 localDevice = LocalDevice.getLocalDevice();
00071 discoveryAgent = localDevice.getDiscoveryAgent();
00072 debug ("startInquiry(DiscoveryAgent.GIAC, this);") ;
00073
00074 discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
00075
00076 }
00077
00079
00081 public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
00082 debug("deviceDiscovered") ;
00083 try {
00084
00085 System.out.println("Device Discovered");
00086 System.out.println("Major Device Class: " + cod.getMajorDeviceClass() + " Minor Device Class: " + cod.getMinorDeviceClass());
00087 System.out.println("Bluetooth Address: " + btDevice.getBluetoothAddress());
00088 System.out.println("Bluetooth Friendly Name: " + btDevice.getFriendlyName(true));
00089
00090
00091 uuidSet = new UUID[1];
00092 uuidSet[0] = RFCOMM_UUID;
00093 int searchID = discoveryAgent.searchServices(null,uuidSet,btDevice,this);
00094 } catch (Exception e) {
00095 System.out.println("Device Discovered Error: " + e);
00096 }
00097 }
00098
00099 public void inquiryCompleted(int discType) {
00100 debug("inquiryCompleted") ;
00101
00102 }
00103
00105 public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
00106 debug("servicesDiscovered") ;
00107
00108
00109 for(int i=0;i<servRecord.length;i++) {
00110 serviceUrl = servRecord[i].getConnectionURL(0,false);
00111 }
00112 }
00113
00115 public void serviceSearchCompleted(int transID, int responseCode) {
00116 debug("serviceSearchCompleted") ;
00117
00118
00119
00120 if(responseCode == SERVICE_SEARCH_COMPLETED) {
00121 System.out.println("SERVICE_SEARCH_COMPLETED\n");
00122 System.out.println("Service URL: " + serviceUrl);
00123 clientResponseCode = responseCode ;
00124
00125 initOK=true;
00126 }else{
00127 if(responseCode == SERVICE_SEARCH_ERROR)
00128 System.out.println("SERVICE_SEARCH_ERROR\n");
00129
00130 if(responseCode == SERVICE_SEARCH_TERMINATED)
00131 System.out.println("SERVICE_SEARCH_TERMINATED\n");
00132
00133 if(responseCode == SERVICE_SEARCH_DEVICE_NOT_REACHABLE)
00134 System.out.println("SERVICE_SEARCH_DEVICE_NOT_REACHABLE\n");
00135
00136 if(responseCode == SERVICE_SEARCH_NO_RECORDS){
00137 System.out.println("SERVICE_SEARCH_NO_RECORDS\n");
00138
00139 }
00140 }
00141
00142
00143 }
00144
00145 }