×

Discussion Board

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

    Bluetooth ( searching devices)

    I tested a bluetooth program that search the devices nearby.
    I use the startInquiry() method, then I sleep the tread for 1 min and then called cancelInquiry() method and displayed the devices list.

    But Inquiry process may not have finished within one min.
    What is the better way?

    Here is my program.

    Code:
    public class ClientServer implements DiscoveryListener {
    	LocalDevice localdevice;
    	DiscoveryAgent discoveryagent;
    	StringBuffer sbuffer;
    	Alert alert;
    	StreamConnectionNotifier notifier;
    	StreamConnection streamconnection;
    	InputStream inputstream;
    	OutputStream outputstream;
    	UUID RFCOMM_UUID = new UUID(0x0003);
    	StringBuffer buffer=new StringBuffer();
    	
    
    	public ClientServer(boolean isServer) {
    		if(isServer==true)
    		{
    			InitServer();
    			System.out.println("**********");
    		}
    		else InitClient();
    	}
    
    	private void InitClient() {
    		try {
    			localdevice=LocalDevice.getLocalDevice();
    			discoveryagent=localdevice.getDiscoveryAgent();
    			discoveryagent.startInquiry(DiscoveryAgent.GIAC, this);
    			try {
    				BluetoothMIDlet.bluetoothmidlet.mainform.t.sleep(1000*60);
    				discoveryagent.startInquiry(this);
    				receivemessage();
    				//sendmessage("This is client");
    				Alert alert=new Alert("",buffer.toString(),null,AlertType.INFO);
    				BluetoothMIDlet.bluetoothmidlet.display.setCurrent(alert);
    				alert.setTimeout(Alert.FOREVER);
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			
    		} catch (BluetoothStateException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    	}
    
    	private void sendmessage(String string) {
    		try {
    			outputstream.write(string.getBytes());
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    	}
    
    	private void InitServer() {
    		try {
    			localdevice=LocalDevice.getLocalDevice();
    			sbuffer=new StringBuffer();
    			sbuffer.append("Name:"+localdevice.getFriendlyName()+"\n");
    			System.out.println(localdevice.getFriendlyName().toString());
    			sbuffer.append("Address"+localdevice.getBluetoothAddress()+'\n');
    			sbuffer.append("Class"+localdevice.getClass().toString());
    			alert=new Alert("",sbuffer.toString(),null,AlertType.INFO);
    			BluetoothMIDlet.bluetoothmidlet.display.setCurrent(alert);
    			alert.setTimeout(Alert.FOREVER);
    			
    			localdevice.setDiscoverable(DiscoveryAgent.GIAC);
    			String url="btspp://localhost:"+RFCOMM_UUID;
    			try {
    				notifier=(StreamConnectionNotifier) Connector.open(url);
    				streamconnection=notifier.acceptAndOpen();
    				inputstream=streamconnection.openInputStream();
    				outputstream=streamconnection.openOutputStream();
    				sendmessage("This is server");
    				
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			
    		} catch (BluetoothStateException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    		
    	}
    
    	public void deviceDiscovered(RemoteDevice btdevice, DeviceClass deviceclass) {
    		
    		//StringBuffer buffer=new StringBuffer();
    		try {
    			buffer.append(btdevice.getFriendlyName(true)+"\n");
    			buffer.append(btdevice.getBluetoothAddress());
    			//Alert alert=new Alert("",buffer.toString(),null,AlertType.INFO);
    			//BluetoothMIDlet.bluetoothmidlet.display.setCurrent(alert);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    
    	public void inquiryCompleted(int arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	public void serviceSearchCompleted(int arg0, int arg1) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	public void servicesDiscovered(int arg0, ServiceRecord[] arg1) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	public void receivemessage() {
    		byte[] data = null;   
    	       
    	       try 
    	       {       	      	
    	        
    	          int length = inputstream.read();       
    	          data= new byte[length];
    	          length = 0;
    	      
    	          while (length != data.length) 
    	          {     
    	                  int ch = inputstream.read(data, length, data.length - length);
    
    	                  if (ch == -1)
    	                  {
    	                    throw new IOException("Can't read data");
    	                  }
    	                  length += ch;
    	          }      
    
    	    
    	       }
    	       catch (IOException e) 
    	       {
    	             System.err.println(e);
    	       } 
    	       
    	    
    	       buffer.append(data);
    		
    	}
    
    }

    Code:
    public class Mainform extends Form implements CommandListener, Runnable {
    	ChoiceGroup cg;
    	Command cmdstart;
    	boolean isServer;
    	ClientServer clientserver;
    	Thread t=new Thread(this);
    
    	public Mainform(String arg0) {
    		super(arg0);
    		cg=new ChoiceGroup("",ChoiceGroup.EXCLUSIVE);
    		cg.append("Server", null);
    		cg.append("Client", null);
    		append(cg);
    		cmdstart=new Command("Start",Command.SCREEN,1);
    		addCommand(cmdstart);
    		setCommandListener(this);
    		
    	}
    
    	public void commandAction(Command c, Displayable d) {
    		if(c==cmdstart)
    		{
    			switch(cg.getSelectedIndex())
    			{
    			case 0: 
    				{
    					isServer=true;				
    				//Thread t=new Thread(this);
    				t.start();
    				}
    				break;
    				
    			case 1:
    				
    				{
    					isServer=false;
    					//Thread t=new Thread(this);
    					t.start();
    				}
    				break;
    				
    			
    			}
    		}
    		
    	}
    
    	public void run() {
    		clientserver=new ClientServer(isServer);
    		System.out.println("*****");
    		
    		
    	}
    
    }

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    4,105

    Re: Bluetooth ( searching devices)

    You do not have to cancel at all. After around ten seconds the inquiry will end with inquiryCompleted. Forget Timers, think event based! If you go for endless inquiries, I am quite happy with calling cancelInquiry from within deviceDiscovered. However, I call it indirectly from a thread not to block that event and getting into synchronisation trouble. The thread was started from deviceDiscovered.

Similar Threads

  1. searching for bluetooth devices around you using kunerilite
    By fincode in forum [Archived] Flash Lite on Nokia Devices
    Replies: 6
    Last Post: 2009-05-05, 19:37
  2. Midlet not searching the Bluetooth active devices in S60 mobiles
    By rameshpauldurai in forum Mobile Java Games
    Replies: 2
    Last Post: 2009-04-09, 17:29
  3. a question about searching bluetooth
    By punding in forum Mobile Java General
    Replies: 0
    Last Post: 2007-10-05, 18:24
  4. Bluetooth Searching Icon
    By ronald29 in forum Symbian
    Replies: 0
    Last Post: 2007-04-27, 05:02
  5. searching for bluetooth devices which has a specified application
    By tserivdd in forum Bluetooth Technology
    Replies: 3
    Last Post: 2004-02-05, 09:15

Posting Permissions

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