×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    1

    Question j2se server j2me client bluetooth connection problem

    Hello

    I am trying to make a simple hello world application between j2se server and j2me client which is my mobile phone nokia 6600.

    The problem is when I install the client program to mobile then it cant find the service running on j2se.

    Here is the code for server :

    Code:
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
     
    import javax.bluetooth.UUID;
    import javax.microedition.io.Connector;
    import javax.microedition.io.StreamConnection;
    import javax.microedition.io.StreamConnectionNotifier;
     
    public class Server {
    	static String ServiceUrl = "btspp://localhost:10203040607040A1B1C1DE100;name=SPP";
    	public static void main(String[] args) {
    		
    		System.out.println("server is running...");
    		
    		try {
    		    // create a server connection
    		    StreamConnectionNotifier notifier =(StreamConnectionNotifier) Connector.open(ServiceUrl);
    		    // accept client connections
    		    StreamConnection connection = notifier.acceptAndOpen();
    		    // prepare to send/receive data
    		    byte buffer[] = new byte[100];
    		    String msg = "hello there, client";
    		    InputStream is = connection.openInputStream();
    		    OutputStream os = connection.openOutputStream();
    		    // send data to the client
    		    os.write(msg.getBytes());
    		    // read data from client
    		    is.read(buffer);
    		    connection.close();
    		} catch(IOException e) {
    		  e.printStackTrace();
    		}
     
    	}
    	
    	
     
     
    }
    Before starting client I start this server and it waits for connections.


    Here is the client code :

    Code:
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
     
    import javax.bluetooth.BluetoothStateException;
    import javax.bluetooth.DiscoveryAgent;
    import javax.bluetooth.LocalDevice;
    import javax.bluetooth.ServiceRecord;
    import javax.bluetooth.UUID;
    import javax.microedition.io.Connector;
    import javax.microedition.io.StreamConnection;
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Form;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;
     
     
    public class Client extends MIDlet implements Runnable, CommandListener {
    	StreamConnection conn;
     
    	
    	public void run(){
    		
    		//make a form and show the user "Locating the server"
    		Form form = new Form("Client");
    		Display.getDisplay(this).setCurrent(form);
    		form.append("Locating server...");
    		
    		
     
    		try{
    			
    			
    			
    			
    			//set up the bluetooth connection:
    			LocalDevice local = LocalDevice.getLocalDevice();
    			
    			
    			DiscoveryAgent agent = local.getDiscoveryAgent();
    			String connString = agent.selectService(
    					new UUID("10203040607040A1B1C1DE100", false), ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
    			
    			
    			System.out.println(connString);
    			if(connString != null){
    				
    					try {
    						conn = (StreamConnection) Connector.open(connString);
    					} catch (IOException e1) {
    						// TODO Auto-generated catch block
    						e1.printStackTrace();
    					}
    					
    					try {
    					    byte buffer[] = new byte[100];
    					    String msg = "hello there, server";
    					    InputStream is = conn.openInputStream();
    					    OutputStream os = conn.openOutputStream();
    					    // send data to the server
    					    os.write(msg.getBytes());
    					    // read data from the server
    					    is.read(buffer);
    					    conn.close();
    					} catch(IOException e) {
    					  e.printStackTrace();
    					}
    					
    					
    					
    				
    			} else {
    				//show the user an error if it occurs
    				form.append("Unable to locate service");			
    			}
    		} catch (BluetoothStateException e){
    			//show the user an error if it occurs
    			form.append("Problem in the connection");
    		}
    	}
     
    	public void startApp()
    		throws MIDletStateChangeException{
    		new Thread(this).start();
    	}
    	
    	public void pauseApp(){}
    	
    	public void destroyApp(boolean unconditional){}
    	
    	public void commandAction(Command c, Displayable d){
    		notifyDestroyed();
    	}
    	void destroyImpl() {
     
    	}
     
    }

    When I start client then I see the message locating server on phone screen... And then Unable to locate service... It seems that mobile cant find the service. Why can it be ? Any ideas ?

    I am using an external bluetooth device Creative CB2436. I have installed its driver. I am using bluecove version 2.0.1 which also supports WIDCOMM in server side. I have put the bluecove.dll into WINDOWS\SYSTEM32 folder and I am using windows xp sp2...

    Any help would be appreciated

    Thanks
    Last edited by bepolat; 2007-11-02 at 19:09.

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    19

    Re: j2se server j2me client bluetooth connection problem

    Hi,

    i m not aware of selectService method but u can try out in traditional way like first deviceInquiry and then searchServices method.
    may be u can able to do things wht u want..

    and try ur program on other mobile also..

    With Regards,
    Rohit Kotecha.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    3

    Re: j2se server j2me client bluetooth connection problem

    Hi all,
    I am also trying to make a simple chat application between mobile and computer. i have used the conventional device and service discovery functions. i am using StreamConnectionNotifier class to start input and output stream. I am able to send the message to mobile from computer but i am not able to receive message back from mobile to computer. I am using Nokia E63 and BlueSoleil 2.1. Please help !!!
    Thanks

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

    Re: j2se server j2me client bluetooth connection problem

    vikrantchaplot, flush not close. Did that help?

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    3

    Re: j2se server j2me client bluetooth connection problem

    Hi traud .. I could not get you .. i first send the message from PC to Mobile using :
    opStream.write(text.length());
    opStream.write(text.getBytes());
    opStream.flush();

    i had also used opStream.close(); but that did not helped.

    (here opStream is an object of - OutputStream)

    am using the same code to receive msg on PC which i used on mobile.. Can you please explain a little more..
    Thanks
    Last edited by vikrantchaplot; 2010-07-14 at 05:39. Reason: more details

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

    Re: j2se server j2me client bluetooth connection problem

    Well, you do what I intended already. How does your server code looks like. Are you I/O stream savvy? I recommend to use Data(Input/Output)Streams and their special methods (like writeUTF), if both sides are Java. Those methods are much easier to use and avoid several pitfalls (wrong character encoding, too short length, blocking read, …)

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    3

    Re: j2se server j2me client bluetooth connection problem

    well i am very new to java programming .. i have no prior exp in J2ME or bluetooth coding .. i am posting the code below ... may be u can find the problem here .. thnks for ur support

    public static void SendMessages(String text) throws IOException // on mobile - E63
    {

    form.append("Sending handshake");

    try {
    streamConn = (StreamConnection) Connector.open(connUrl); // this is what i have in mobile for sending data
    opStream = streamConn.openOutputStream();

    opStream.write(text.length());
    opStream.write(text.getBytes());


    form.append("message sent"); // am actually getting this message printed on my mobile screen .. am guessing this code is correct

    } catch (IOException ex) {
    form.append(ex.getMessage());

    }


    this is the code running on PC to receive data:

    public static String RecieveMessages() // on PC - BlueSoleil 2.6.0.8
    {

    System.out.println("here");
    byte[] data = null;

    try
    {
    if(ipStream == null){
    System.out.println("ipStream ==== null"); // this is not getting printed .. so the object is created
    }
    int length = ipStream.read();
    System.out.println(length + " length"); // the code is actually not getting here
    /* when i close the application on mobile .. this gets executed and i get length as -1
    data= new byte[length];
    length = 0;

    while (length != data.length)

    {
    System.out.println("marker1"); // not printed at all ..
    int ch = ipStream.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);
    }
    String str = new String (data); // code nvr reach till here
    System.out.println(str);
    return str;


    }

Similar Threads

  1. Question about bluetooth application (one client and server in the same device)
    By gonzi in forum Mobile Java Networking & Messaging & Security
    Replies: 3
    Last Post: 2008-07-24, 12:01
  2. client server connection problem
    By jeff4yee in forum Symbian Networking & Messaging (Closed)
    Replies: 2
    Last Post: 2006-09-28, 11:11
  3. Send multiple message from client to server
    By Palermo4Ever in forum Mobile Java Networking & Messaging & Security
    Replies: 2
    Last Post: 2006-06-19, 11:49
  4. Bluetooth client problem on games!
    By wmchan78 in forum Mobile Java Networking & Messaging & Security
    Replies: 6
    Last Post: 2006-03-22, 06:15
  5. Suggestion for resolving PC suite connection via bluetooth
    By Kazi in forum Bluetooth Technology
    Replies: 0
    Last Post: 2003-03-14, 19:35

Posting Permissions

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