| Reply | « Previous Thread | Next Thread » |
|
Hi there,
hmm acceptAndOpen is blocking, obviously. But is there any clean way of cancelling it? Thanks in advance |
|
Hi!
I've been trying to do the same thing. I read that the way to do it is to call close() on the StreamConnectionNotifier which should remove the service record from the SDDB. However I haven't had much success doing this as it causes a RuntimeException saying "Connection Closed during acceptAndOpen". I heard that this might be an emulator problem (I'm using Series 60 MIDP Concept SDK Beta 0.3.1) but I haven't been able to confirm this. If you would like to have a go yourself I've included a section of my code below - but as I said it's not working for me but if you (or anyone else!) make any progress I would very much like to hear about it. Good luck! public void run() { try { // Create the server connection object service = (StreamConnectionNotifier)Connector .open("btspp://localhost:" + uuid.toString() +";name=" + serviceName); // Retrive the service record template rec = localDevice.getRecord(service); } catch (java.io.IOException ioe) { System.out.println(ioe.getMessage()); } while (running) { try { // Open the service ready for accepting connections conn = service.acceptAndOpen(); } catch (java.io.IOException e1) { System.out.println(e1.getMessage()); if (conn != null) try { conn.close(); } catch (java.io.IOException e2) { System.out.println(e2.getMessage()); } } } // while running } // end run() public void stopService() { running = false; try { service.close(); // Close the StreamConnectionNotifier } catch (Exception ex) { System.out.println(ex.getMessage()); } } |
|
Well, actually i managed to solve it using this technique, and try catch most of the exception.. it works well on 6600/6620/6230
|
|
Interesting... I guess the problems I was having might well be to do with the emulator then. Thanks for letting me know!
|
|
hi all,
I have done a simple example but its not even getting started when tested on 3660 and 7650 as both are bluetooth enabled. though on emulator (Nokia series 20 SDK) working perfectly. what can be the problem. I hope u can help me to solve it. regard dotc |
|
Hi,
dotc - you didn't give much detail in your post so it's difficult to tell what the problem is. However, if you are writing your application in Java... neither of the phones you mention support the Java Bluetooth APIs (JSR-82) so Java Bluetooth apps will not work on these phones. If possible try running your application on a 6600, a 6230 or one of the other phones which support JSR-82. By the way - with regard to the original acceptAndOpen question... I have now had the chance to try the technique I mentioned above on a 6600 and, as tOlkIEn said, it appears to be working fine so the problem does appear to be another(!!!) emulator problem. |
|
yeah bodhix, u r right, later I came to know this fact that these device must be JSR 82 enable in order to run bluetooth app.
thanX for ur kind response. but my example problem persists. Its very simple. It has just two classes. 1) BTMIDlet 2) MainForm there respective code is here // ---------BTMIDlet----------------- //----------end of BTMIDlet------------- MainForm is next post I am eagerly waiting to see my example running. but client/slave thread get stucked. Can u be online on yahoo msn. My id is dotcdotc@yahoo.com ..........MainForm code is next post |
|
sorry forgot to copy BTMIDlet code to copy
its here ///////////////// /* * BTMIDlet.java * * Created on July 24, 2004, 11:04 AM * * @author dotcdotc * @version */ import javax.microedition.midlet.*; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; public class BTMIDlet extends javax.microedition.midlet.MIDlet { MainForm main ; Display display; public void startApp() { display = Display.getDisplay(this); main = new MainForm("Main Interfave",this); setDisplayable(main); } public void pauseApp() { } public void destroyApp(boolean unconditional) { display = null; main = null; } public void setDisplayable(Object displayable) { display.setCurrent((Displayable)displayable); } } //////////////// |
|
//MainForm code is here
//MainForm code is here import javax.microedition.lcdui.*; import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; import javax.microedition.io.*; import javax.bluetooth.*; import java.util.*; public class MainForm extends Form implements ommandListener, Runnable, DiscoveryListener { BTMIDlet parent; Command client, server, exit, send; List option; String uuidStr = "A55665EE9F9146109085C2055C888B39"; UUID uuid; String url="btspp://localhost:"+ uuidStr +";authenticate=false;authorize=false;encrypt=false;name=BTMIDlet"; RemoteDevice remoteDevice; Vector remoteDevices = new Vector(); LocalDevice localDevice; StreamConnection con; StreamConnectionNotifier notifier; TextField text; int discoverymode; String user; DiscoveryAgent agent; ServiceRecord rec; boolean isServiceDiscovered = false, isDeviceDiscovered = false, isClient = false; int serverState = 0; //(0-devicesearch, 1-service serach, 2-con, 3-conversation) Thread thread; MainForm(String title, BTMIDlet parent) { super("MainForm"); this.parent = parent; init(); } public void run() { int k = 0; if (isClient) { try { notifier = (StreamConnectionNotifier) Connector.open(url); con = notifier.acceptAndOpen(); p("client con"); }catch(Exception _ex) { p(user+"_ex in run:"+ _ex); } while (true) { try { p("slave-1: "); con = notifier.acceptAndOpen(); InputStream in = con.openInputStream(); int i = in.available(); p("slave0: "+i); if (i > 0) { do { i=in.read(); String s = (char)i+""; System.out.print(s); append(s); }while (i != -1); } in.close(); con.close(); thread.sleep(100); p("slave 1"); }catch(Exception _ex) { p("slave run:"+_ex); } } } else //if serever { while(true) { try { switch (serverState) { case 0: // device discovery break; case 1: if (!isServiceDiscovered && isDeviceDiscovered ) { remoteDevice = (RemoteDevice) remoteDevices.elementAt(0); UUID uuids[] = new UUID[1]; uuids[0] = uuid; int attr[] = null; int transId = agent.searchServices(attr, uuids, remoteDevice, this); p("serverState1"); k =1; } break; case 2: if (isServiceDiscovered) { int requiredSecurity = 0; String url = rec.getConnectionURL(requiredSecurity, false); con =(StreamConnection) Connector.open(url); p("6 url::"+url); serverState = 3; } break; case 3: try { // getting message from client InputStream is = con.openInputStream(); int i = is.available(); if (i > 0 ) do { i = is.read(); String s = (char)i+""; append(s); p("server:"+s); }while( i != -1 ); is.close(); p("3rd state"); }catch(Exception _ex) { p(serverState+" master run: "+_ex); } break; } Thread.sleep(1000); }catch(Exception _ex) { p("run(server):"+ _ex); } } } } public void commandAction(Command command, javax.microedition.lcdui.Displayable displayable) { try { if (command == client) { user = "client"; isClient = true; localDevice.setDiscoverable(DiscoveryAgent.GIAC); append(user+":"+localDevice.getBluetoothAddress()); removeCommand(client); removeCommand(server); addCommand(send); append(text); thread.start(); } else if (command == server) { isClient = false; user = "server"; localDevice.setDiscoverable(DiscoveryAgent.NOT_DISCOVERABLE); uuid = new UUID(uuidStr, false); agent = localDevice.getDiscoveryAgent(); agent.startInquiry(DiscoveryAgent.GIAC, this); append(user+":"+localDevice.getBluetoothAddress()); removeCommand(server); removeCommand(client); addCommand(send); append(text); thread.start(); } else if (command == send) { try { OutputStream os = con.openOutputStream(); String s = text.getString(); if (s.trim().length() == 0) s = "hi!!"; byte b[] = s.getBytes(); os.write(b); os.close(); if (user.equals("server")) { // for master p(user+" sending to " + remoteDevice.getBluetoothAddress()+":"+s); } else { // for slave p(user+" sending:"+s); } }catch(IOException _ioe) { p(user+" commandAction:"+ _ioe); } } else { parent.destroyApp(false); p("exit"); } }catch(BluetoothStateException _bse) { p("commandException:"+_bse); }catch(IOException _ioe) { p("commandException:"+_ioe); } } public void deviceDiscovered(javax.bluetooth.RemoteDevice remoteDevice, javax.bluetooth.DeviceClass deviceClass) { p(" deviceDiscovered:"+remoteDevice.getBluetoothAddress()); remoteDevices.addElement(remoteDevice); isDeviceDiscovered = true; } public void inquiryCompleted(int param) { p("inquiryCompleted"); serverState = 1; } public void serviceSearchCompleted(int param, int param1) { p("serviceSearchCompleted"); serverState = 2; isServiceDiscovered = true; } public void servicesDiscovered(int param, javax.bluetooth.ServiceRecord[] serviceRecord) { p("...Service Discovered......."+serviceRecord.length); if (serviceRecord.length == 1) { int attr[] = serviceRecord[0].getAttributeIDs(); rec = serviceRecord[0]; } } public void init() { setCommandListener(this); client = new Command("Client", Command.OK, 0); server = new Command("Server", Command.OK, 0); exit = new Command("exit", Command.EXIT, 0); send = new Command("Send", Command.OK, 0); text = new TextField("enter message:", "hi!!", 50, TextField.ANY); addCommand(client); addCommand(server); addCommand(exit); thread = new Thread(this); try { localDevice = LocalDevice.getLocalDevice(); }catch(BluetoothStateException _bse) { p("in init"+_bse); } } public void show(String s) { System.out.println(s); } } regards dotc |
|
Hi dotc,
Sorry for the delayed response. I've just had a quick look through your code. I'm afraid there's a number of problems in there. Rather than pick through them all it would probably better for you to have a look at an example - I'd recommend you have a look through the L2CAPChat example on the Symbian site http://www.symbian.com/developer/tec...ava.html#l2cap This example is of a simple chat application which is similar to the kind of thing you are trying to do by the looks of it. The L2CAPChat example uses a L2CAP connection (if you hadn't already guessed) but it is a trivial matter to adapt the code to use serial port profile. There is also a useful PDF that guides you through the code and explains everything quite nicely - hopefully this should help you out. |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|