×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    1

    Bluetooth Connection woes

    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(){
    
    }
    }

  2. #2
    Registered User
    Join Date
    Aug 2003
    Location
    South Africa
    Posts
    189

    Re: Bluetooth Connection woes

    Hi

    I am having the same problem, what I have found is that the reason for the Error is that you are trying to discover a devices Services before the inquiry is complete i.e. you are doing it in deviceDiscovered if you search for services for all devices found in inquiryCompleted you will not get this error.

    What I need to know is, is there a way around the problem i.e is it possible to detect the services as soon as a device is found.

    I have been testing with Nokia N70, E61 and N95 and all these devices give the same error

    Regards
    Steve

    Anyone ???
    Last edited by stevejanko; 2007-12-03 at 18:20.

Similar Threads

  1. Send SMS using a Bluetooth connection????
    By ankitshrivastav in forum General Messaging
    Replies: 4
    Last Post: 2006-10-11, 15:52
  2. Internet connection over Bluetooth (6600<->PC)
    By cbruegg in forum Bluetooth Technology
    Replies: 0
    Last Post: 2003-11-12, 19:28
  3. Ipaq 5450 and Nokia 8910/i bluetooth connection
    By ferceska in forum Bluetooth Technology
    Replies: 0
    Last Post: 2003-07-07, 14:23
  4. Replies: 0
    Last Post: 2003-07-06, 11:58
  5. GPRS Connection from Laptop over Bluetooth
    By ChicMac in forum Bluetooth Technology
    Replies: 1
    Last Post: 2003-05-19, 18:22

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×