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("*****"); } }

Reply With Quote

