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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brum2008
    New Member
    • Oct 2008
    • 1

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

    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 setupConnection s() 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

    Code:
    import java.io.*;
    
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.io.*;
    import javax.bluetooth.*;
    
    /**
     * @author Brum
     */
    public class prjMIDlet 
            extends MIDlet implements javax.microedition.lcdui.CommandListener
    {
        private Alert safetyAlert = null;
        private Command mExitCommand = new Command("Exit", Command.EXIT,1);
        
        StreamConnectionNotifier notifier = null;
        LocalDevice prjCellPhone = null;
        DataInputStream input;
        UUID myService = new UUID("1101",true);
        String myMIDlet = this.getClass().getName();
        String filter = "*";        
    
        public void startApp() {
            initialize();			// Initialize the safetyAlert interface 
            
            try {
                input = setupConnections(); // Connect the cell phone to the eb506 (create input stream from other device)
            } catch (BluetoothStateException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            Display.getDisplay(this).setCurrent(safetyAlert);   // display the Alert
    
            
            try {
                do {
                    safetyAlert.setString(getMessage(input)); // get incoming message and put it into safetyAlert
                    Display.getDisplay(this).setCurrent(safetyAlert);   // display the Alert with the new message
                } while ((safetyAlert.getString()).length() > 1); // with only a carraige return, no more messages
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            
            
            try {
                PushRegistry.registerConnection(new String("btspp://localhost:" 
                        + myService.toString()), myMIDlet, filter); // Register this connection in the PushRegistry for auto start-up
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            
            // This next section will set the Alert to display the registered PushRegistry connections
            String[] connectionList = PushRegistry.listConnections(false);
            int listcount = connectionList.length; 
            StringBuffer connlist = new StringBuffer("Connections (# = "+listcount+"):");
            while(listcount > 0){
                connlist.ensureCapacity(connlist.length()+30);
                connlist.append(connectionList[listcount-1]);
                listcount--; 
            }
            safetyAlert.setString(new String(connlist));
            Display.getDisplay(this).setCurrent(safetyAlert);   // display the Alert
    
        }
        
        private void initialize() {
    	        
    	safetyAlert = new Alert("Incoming message: ", "Please Wait...",null,null);
    	safetyAlert.setTimeout(Alert.FOREVER);
    	safetyAlert.addCommand(mExitCommand);
    	safetyAlert.setCommandListener(this);
    	return;
        }
        
        public DataInputStream setupConnections() throws BluetoothStateException, IOException, ClassNotFoundException {
                
            prjCellPhone = LocalDevice.getLocalDevice();
            
            prjCellPhone.setDiscoverable(DiscoveryAgent.GIAC);
            notifier = (StreamConnectionNotifier)Connector.open("btspp://localhost:" + myService.toString()); 
    
            safetyAlert.setString("waiting for connection");
            Display.getDisplay(this).setCurrent(safetyAlert);   // display the Alert
            
            /* This next line is where the application stalls, waiting for a client
             * to connect.  I connect with my other device and it is able to connect
             * to the phone, but is not linked to this application */
            StreamConnection sconn = notifier.acceptAndOpen();
            
            /* If I connect with this section of code instead, (connect from phone 
             * directly to my other device), there is no problem with transmitting 
             * data (But the application does not auto-start when the incoming SPP
             * connection occurs.
            DiscoveryAgent prjAgent = prjCellPhone.getDiscoveryAgent();
            String connString = prjAgent.selectService(myService, ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);                    
            StreamConnection sconn = (StreamConnection)Connector.open(connString);
            input = sconn.openDataInputStream();*/
            
            
            safetyAlert.setString("client device connected");
            Display.getDisplay(this).setCurrent(safetyAlert);   // display the Alert
            return sconn.openDataInputStream();                
        }
        
        
        /* This method gets an ASCII message from the input stream and converts it 
         * to a UniCode string and returns the Unicode string */
        public String getMessage(DataInputStream d) throws IOException {
            
            StringBuffer strBuff = new StringBuffer();
            char store = 0;
            int count = 0;
            
            while (count < 32) {        // maximum message length is 32 characters
                count++;
                store = (char) d.readByte();  // read the next byte of the stream and store as a char
                if (store == 0x000D)
                    break;
                strBuff.append(store);
            }
            return strBuff.toString();
        }
    
        
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
        }
        
        public void commandAction(javax.microedition.lcdui.Command command, javax.microedition.lcdui.Displayable displayable) {
            if (displayable == safetyAlert) {
                if (command == mExitCommand) {
                    Display.getDisplay(this).setCurrent(null);
                    destroyApp(true);
                    notifyDestroyed();
                }
            }
        }
    }
Working...