By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,027 Members | 1,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,027 IT Pros & Developers. It's quick & easy.

trouble connecting client to J2ME server application (JSR-82)

P: 1
Hello!

Below is the Netbeans Mobility source code for my application (The application offers SPP service, and then just hangs waiting for a client to connect (using acceptAndOpen()). The other device (A7 Engineering's eb506 Bluetooth serial adapter connected to a microcontroller) connects to the phone, but is not linked to my application).

If I make the connection directly from the phone (Nokia 6085) to the eb506, there is no problem connecting and transmitting data - the problem arises when trying to connect from the eb506 to the phone. It is necessary for my application that I connect from the eb506 to the Java server application. Can anyone offer any insight as to why my application is not connecting to the incoming SPP connection from the other device (this is handled in the setupConnections() method). I've been trying down the path of modifying the ServiceRecord with no positive results.

Thanks for your help (If you can help).

Sincerely,

Brum

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. import javax.microedition.midlet.*;
  4. import javax.microedition.lcdui.*;
  5. import javax.microedition.io.*;
  6. import javax.bluetooth.*;
  7.  
  8. /**
  9.  * @author Brum
  10.  */
  11. public class prjMIDlet 
  12.         extends MIDlet implements javax.microedition.lcdui.CommandListener
  13. {
  14.     private Alert safetyAlert = null;
  15.     private Command mExitCommand = new Command("Exit", Command.EXIT,1);
  16.  
  17.     StreamConnectionNotifier notifier = null;
  18.     LocalDevice prjCellPhone = null;
  19.     DataInputStream input;
  20.     UUID myService = new UUID("1101",true);
  21.     String myMIDlet = this.getClass().getName();
  22.     String filter = "*";        
  23.  
  24.     public void startApp() {
  25.         initialize();            // Initialize the safetyAlert interface 
  26.  
  27.         try {
  28.             input = setupConnections(); // Connect the cell phone to the eb506 (create input stream from other device)
  29.         } catch (BluetoothStateException ex) {
  30.             ex.printStackTrace();
  31.         } catch (ClassNotFoundException ex) {
  32.             ex.printStackTrace();
  33.         } catch (IOException ex) {
  34.             ex.printStackTrace();
  35.         }
  36.         Display.getDisplay(this).setCurrent(safetyAlert);   // display the Alert
  37.  
  38.  
  39.         try {
  40.             do {
  41.                 safetyAlert.setString(getMessage(input)); // get incoming message and put it into safetyAlert
  42.                 Display.getDisplay(this).setCurrent(safetyAlert);   // display the Alert with the new message
  43.             } while ((safetyAlert.getString()).length() > 1); // with only a carraige return, no more messages
  44.         } catch (IOException ex) {
  45.             ex.printStackTrace();
  46.         }
  47.  
  48.  
  49.         try {
  50.             PushRegistry.registerConnection(new String("btspp://localhost:" 
  51.                     + myService.toString()), myMIDlet, filter); // Register this connection in the PushRegistry for auto start-up
  52.         } catch (ClassNotFoundException ex) {
  53.             ex.printStackTrace();
  54.         } catch (IOException ex) {
  55.             ex.printStackTrace();
  56.         }
  57.  
  58.         // This next section will set the Alert to display the registered PushRegistry connections
  59.         String[] connectionList = PushRegistry.listConnections(false);
  60.         int listcount = connectionList.length; 
  61.         StringBuffer connlist = new StringBuffer("Connections (# = "+listcount+"):");
  62.         while(listcount > 0){
  63.             connlist.ensureCapacity(connlist.length()+30);
  64.             connlist.append(connectionList[listcount-1]);
  65.             listcount--; 
  66.         }
  67.         safetyAlert.setString(new String(connlist));
  68.         Display.getDisplay(this).setCurrent(safetyAlert);   // display the Alert
  69.  
  70.     }
  71.  
  72.     private void initialize() {
  73.  
  74.     safetyAlert = new Alert("Incoming message: ", "Please Wait...",null,null);
  75.     safetyAlert.setTimeout(Alert.FOREVER);
  76.     safetyAlert.addCommand(mExitCommand);
  77.     safetyAlert.setCommandListener(this);
  78.     return;
  79.     }
  80.  
  81.     public DataInputStream setupConnections() throws BluetoothStateException, IOException, ClassNotFoundException {
  82.  
  83.         prjCellPhone = LocalDevice.getLocalDevice();
  84.  
  85.         prjCellPhone.setDiscoverable(DiscoveryAgent.GIAC);
  86.         notifier = (StreamConnectionNotifier)Connector.open("btspp://localhost:" + myService.toString()); 
  87.  
  88.         safetyAlert.setString("waiting for connection");
  89.         Display.getDisplay(this).setCurrent(safetyAlert);   // display the Alert
  90.  
  91.         /* This next line is where the application stalls, waiting for a client
  92.          * to connect.  I connect with my other device and it is able to connect
  93.          * to the phone, but is not linked to this application */
  94.         StreamConnection sconn = notifier.acceptAndOpen();
  95.  
  96.         /* If I connect with this section of code instead, (connect from phone 
  97.          * directly to my other device), there is no problem with transmitting 
  98.          * data (But the application does not auto-start when the incoming SPP
  99.          * connection occurs.
  100.         DiscoveryAgent prjAgent = prjCellPhone.getDiscoveryAgent();
  101.         String connString = prjAgent.selectService(myService, ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);                    
  102.         StreamConnection sconn = (StreamConnection)Connector.open(connString);
  103.         input = sconn.openDataInputStream();*/
  104.  
  105.  
  106.         safetyAlert.setString("client device connected");
  107.         Display.getDisplay(this).setCurrent(safetyAlert);   // display the Alert
  108.         return sconn.openDataInputStream();                
  109.     }
  110.  
  111.  
  112.     /* This method gets an ASCII message from the input stream and converts it 
  113.      * to a UniCode string and returns the Unicode string */
  114.     public String getMessage(DataInputStream d) throws IOException {
  115.  
  116.         StringBuffer strBuff = new StringBuffer();
  117.         char store = 0;
  118.         int count = 0;
  119.  
  120.         while (count < 32) {        // maximum message length is 32 characters
  121.             count++;
  122.             store = (char) d.readByte();  // read the next byte of the stream and store as a char
  123.             if (store == 0x000D)
  124.                 break;
  125.             strBuff.append(store);
  126.         }
  127.         return strBuff.toString();
  128.     }
  129.  
  130.  
  131.     public void pauseApp() {
  132.     }
  133.  
  134.     public void destroyApp(boolean unconditional) {
  135.     }
  136.  
  137.     public void commandAction(javax.microedition.lcdui.Command command, javax.microedition.lcdui.Displayable displayable) {
  138.         if (displayable == safetyAlert) {
  139.             if (command == mExitCommand) {
  140.                 Display.getDisplay(this).setCurrent(null);
  141.                 destroyApp(true);
  142.                 notifyDestroyed();
  143.             }
  144.         }
  145.     }
  146. }
  147.  
Oct 3 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.