Hi,
I'm trying to get a client talking to a server on bluetooth, however the client keeps giving me the 'busy' error. I've tried making a thread to wait, and also the 2 different ways of getting a connection URL, but neither seems to work. I've also tired to pause and notify the thread, but I get an error for that, any help would be much appreciated.
Code:import javax.bluetooth.*; import javax.microedition.io.*; import java.io.*; class EchoClient implements DiscoveryListener, Runnable { private DiscoveryAgent discoveryAgent; private RemoteDevice[] remoteDevices; private UUID[] uuidSet; private String serviceUrl; public String errors = ""; Thread t = new Thread(this); int search = 0; public EchoClient() { try { t = new Thread(this); LocalDevice localDevice = LocalDevice.getLocalDevice(); discoveryAgent = localDevice.getDiscoveryAgent(); discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this); t.wait(); errors = errors + " " + localDevice.getProperty("bluetooth.sd.trans.max").toString() + " MARKER "; } catch (Exception e) { errors = errors + e.getMessage() + "here2"; System.out.println(e); } } public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) { if(search ==0){ try { // Get Device Info search++; // Search for Services uuidSet = new UUID[1]; uuidSet[0] = new UUID("1234", true); // serviceUrl = discoveryAgent.selectService(uuidSet[0], 0, true); int searchID = discoveryAgent.searchServices(null,uuidSet,btDevice,this); errors = errors + " waiting "; errors = errors + "getstodone"; } catch (Exception e) { errors = errors + e.getMessage() + "here "; } } else{ errors = errors + "??? "; } } public void inquiryCompleted(int discType) { errors = errors + "ENQDONE" ; t.notify(); StreamConnection conn = null; try { int[] values = new int[3]; //Some example values to test out: values[0] = 4; values[1] = 9; values[2] = 22; System.out.println("HELLO YOU SO AND SO"); conn = (StreamConnection)Connector.open(serviceUrl); DataOutputStream output = conn.openDataOutputStream(); output.write(3); for(int i = 0; i < 3; i++){ output.write(values[i]); System.out.println(values[i]); } int[] receive = new int[3]; receive = BluetoothEchoDemo.readData(conn); for(int i = 0; i < 3; i++){ System.out.println("Received Message from Client: " + receive[i]); } output.flush(); output.close(); } catch (Exception ex) { errors = errors + ex.getMessage() + "here3"; System.out.println(ex); } finally { try { conn.close(); } catch (IOException ioe) { errors = errors + ioe.getMessage() + "here4"; System.out.println("Error Closing connection " + ioe); } } } public void servicesDiscovered(int transID, ServiceRecord[] servRecord) { errors = errors + "SD" ; System.out.println("ServicesDiscovered"); // in this example there is only one service for(int i=0;i<servRecord.length;i++) { serviceUrl = servRecord[i].getConnectionURL(0,false); } } public void serviceSearchCompleted(int transID, int responseCode) { errors = errors + "SSC" ; if(responseCode == SERVICE_SEARCH_ERROR) System.out.println("SERVICE_SEARCH_ERROR\n"); if(responseCode == SERVICE_SEARCH_COMPLETED) { System.out.println("SERVICE_SEARCH_COMPLETED\n"); System.out.println("Service URL: " + serviceUrl); StreamConnection conn = null; try { int[] values = new int[3]; //Some example values to test out: values[0] = 4; values[1] = 9; values[2] = 22; System.out.println("HELLO YOU SO AND SO"); conn = (StreamConnection)Connector.open(serviceUrl); DataOutputStream output = conn.openDataOutputStream(); output.write(3); for(int i = 0; i < 3; i++){ output.write(values[i]); System.out.println(values[i]); } int[] receive = new int[3]; receive = BluetoothEchoDemo.readData(conn); for(int i = 0; i < 3; i++){ System.out.println("Received Message from Client: " + receive[i]); } output.flush(); output.close(); } catch (Exception ex) { errors = errors + ex.getMessage() + "here3"; System.out.println(ex); } finally { try { conn.close(); } catch (IOException ioe) { errors = errors + ioe.getMessage() + "here4"; System.out.println("Error Closing connection " + ioe); } } } if(responseCode == SERVICE_SEARCH_TERMINATED) System.out.println("SERVICE_SEARCH_TERMINATED\n"); if(responseCode == SERVICE_SEARCH_NO_RECORDS) System.out.println("SERVICE_SEARCH_NO_RECORDS\n"); if(responseCode == SERVICE_SEARCH_DEVICE_NOT_REACHABLE) System.out.println("SERVICE_SEARCH_DEVICE_NOT_REACHABLE\n"); } public void run(){ } }

Reply With Quote

