×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Regular Contributor
    Join Date
    Dec 2009
    Posts
    79

    Question Bluetooth problem

    Hi all,

    I'm new in J2me and I need help. I've programmed a little chat but it doesn't work. Maybe it's because of the mobile phones I used. I've already searched in the Internet but I haven't found anything. Here is the code of the searcher:

    Code:
    public class Searcher extends TimerTask implements DiscoveryListener
        {
            private LocalDevice localDevice = null;
            private DiscoveryAgent discoveryAgent = null;
            private String connectionURL = null;
            private RemoteDevice[] device = null;
            private ServiceRecord[] records = null;
            private boolean inquiryCompl = false;
            int count = 0;
    
            public void run() {
                try {
                    
                    StringItem sirun = new StringItem("Searcher: gestartet", null);
                    results1.append(sirun);
                    localDevice = LocalDevice.getLocalDevice();
                    discoveryAgent = localDevice.getDiscoveryAgent();
                    device = new RemoteDevice[10];
                    discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
                    count = 0;
                } catch (BluetoothStateException ex) {
                    ex.printStackTrace();
                }
            }
    
            public void deviceDiscovered(RemoteDevice btDevice, DeviceClass arg1) {
                 StringItem sirun = new StringItem("Searcher: Device gefunden", null);
                 results1.append(sirun);
                device[count++] = btDevice;
            }
    
            public void inquiryCompleted(int arg0) {
                 StringItem sirun = new StringItem("Searcher: Inquiry beendet", null);
                 results1.append(sirun);
                for (int i = 0; i < count; i++)
                {
                    searchforservices(i);
                }
            }
    
            public void serviceSearchCompleted(int arg0, int arg1) {
                StringItem sirun = new StringItem("Searcher: Servicesuche beendet", null);
                results1.append(sirun);
            }
    
            public void servicesDiscovered(int arg0, ServiceRecord[] servRecords) {
                StringItem sirun = new StringItem("Searcher: Services gefunden", null);
                 results1.append(sirun);
                   for(int i=0;i<servRecords.length;i++) {
                           int[] atrids = servRecords[i].getAttributeIDs();
                           connectionURL = servRecords[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT,true);
                           clients[clientscounter] = new Client();
                           clients[clientscounter].connectionString = connectionURL;
                           clients[clientscounter].run();
                           StringItem siruncl = new StringItem("Searcher: Neuer Client", null);
                           results1.append(siruncl);
                     }
            }
    And the Server Code:

    Code:
    package hello;
    
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import javax.bluetooth.UUID;
    import javax.microedition.io.*;
    
    public class Server implements Runnable{
    
       StreamConnection con = null;
       StreamConnectionNotifier service= null;
       InputStream ip  = null;
       OutputStream op = null;
       String namer = "";
       String name = "";
       public boolean connected;
    
       public void send(String message)
        {
            try
            {
                op.write(message.getBytes());
                op.flush();
            }
            catch (IOException ex)
            {
                connected = false;
            }
        }
    
        public String getinput()
        {
             String s = "";
            try {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                int data;
                while ((data = ip.read()) != -1) {
                    out.write(data);
                }
                s = out.toString();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            return s;
        }
    
        public void run() {
            try {
    
                UUID uuid = new UUID("1111",false);
                //Extends a stream for client to connect
                service = (StreamConnectionNotifier) Connector.open("btspp://localhost:" +  uuid.toString() + ";authenticate=false;encrypt=false;name=" + name);
                //Server waiting for client to connect
                con = service.acceptAndOpen();
                //Open streams for two way communication.
                ip = con.openInputStream();
                op = con.openOutputStream();
                connected = true;
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    And the Client Code

    Code:
    package hello;
    
    import java.io.*;
    import javax.microedition.io.*;
    
    public class Client implements Runnable
    {
        public String connectionString;
        public StreamConnection connection;
        public InputStream in;
         public OutputStream out;
        public boolean goodadress;
        public boolean asked;
        public String name;
    
    
    
        public void run()
        {
            try
            {
                connection = (StreamConnection) Connector.open(connectionString);
                out = connection.openOutputStream();
                in = connection.openInputStream();
            }
            catch (IOException ex)
            {
                goodadress = false;
    
            }
        }
    
           public void send(String message)
        {
            try
            {
                out.write(message.getBytes());
                out.flush();
            }
            catch (IOException ex)
            {
                goodadress = false;
            }
        }
    
        public String getinput()
        {
            String s = "";
            try {
                ByteArrayOutputStream outer = new ByteArrayOutputStream();
                int data;
                while ((data = in.read()) != -1) {
                    outer.write(data);
                }
                s = outer.toString();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            return s;
        }
    }
    ... And yes I've already defined a server that runs in the beginning.

    Is the code right or wrong or is it because of my mobile phones? I used an Nokia 5800 Xpress music and a N82. I wasn't aible to test it in the emulator. It's not the whole code but the important part.

    Sorry for your nice help.
    Benni

  2. #2
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    88

    Re: Bluetooth problem

    Hello,
    are u catching any kind of exception? Try to find where is "the point" by catching exceptions. Why were u not able to excute in the emulator?!

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    4,105
    Quote Originally Posted by benni-europe View Post
    Code:
    UUID uuid = new UUID("1111", false);
    Although it should not make a change, please use a private (long) UUID generated with the rules for UUIDs.

  4. #4
    Regular Contributor
    Join Date
    Dec 2009
    Posts
    79

    Re: Bluetooth problem

    Thanks a lot for your answer. Sorry that I' ve taken it into two blogs. It wasn't deliberatly. I've used it for the first time. The Emulator didn't simulate two handys. That was what I meant. (I use the JDK 3.0 of Sun) And I've got a question: What are the rules for an UUID? There is nothing in my book (J2ME - Anwendungsentwicklung für Handys PDA und CO). Sorry for all my English mistakes. Please send me the rules for an UUID or an example. And another question: Is there a differency between the Nokia development SDK(Java) and Sun's SDK? Could it be the reason for the bad mistake. Do the programs of the Nokia SDK also run in a sandbox? I hope you were aible to read the text because I'm form Germany. Thanks for your answers. And to answer your question: I don't get any exception but they don't find themselves. Is it because they run in different threads? - I don't think so.

    Benni

Similar Threads

  1. bluetooth passkey problem
    By Lizard_hun in forum Symbian Networking & Messaging (Closed)
    Replies: 3
    Last Post: 2009-07-22, 00:15
  2. Q: Problem with Nokia PC Suite and Bluetooth
    By Juha_74 in forum General Development Questions
    Replies: 0
    Last Post: 2007-03-13, 13:32
  3. Bluetooth serviceDiscover problem Device Not Reachable
    By kashifqazi in forum Bluetooth Technology
    Replies: 1
    Last Post: 2006-10-13, 12:31
  4. Nokia SDK Bluetooth service re-discovery problem, Help Me!:(
    By tomsonxu in forum Bluetooth Technology
    Replies: 0
    Last Post: 2005-11-08, 07:51
  5. Replies: 5
    Last Post: 2003-02-07, 11:25

Posting Permissions

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