×

Discussion Board

Results 1 to 10 of 10
  1. #1
    Regular Contributor
    Join Date
    Dec 2009
    Posts
    76

    Bluetooth between 4 devices

    Hi
    I am building an application allowing communication between 4 Nokia N97 devices, using Bluetooth. The simple configuration needs to be such that one of the devices is a server and other 3 are clients, and message sent from any client needs to be broadcasted by the server. I found this code: http://wiki.forum.nokia.com/index.ph...r_more_devices, and trying to modify it to suit my purpose.

    I am able to connect three devices with the server over bluetooth, and each client can send message to server but only in round-robin fashion, as the readMessage by the server blocks over each client and waits for a particular client to respond.

    Hence to read any message from any client instantaneously, I tried to run one thread for each client in the server part of the program (so three threads in server). But the server part of this program always gives error as "Array Index Out of Bounds java.lang.ArrayIndexOutOfBoundsException Array index out of range: 2". The same program without calling the thread thrice, never gave such error.

    Please let me know what's the reason for this error (is there any upper limit on the number of threads?), or let me know if there is some other way to allow such communication.

    Thanks

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    4,105
    Because I do not know your whole code, it is difficult to answer your question. Because it works here, I can make my answer short: You have several bugs in your code. I just tested four devices with one Nokia N97 as master (and server): Works like a charm. I recommend to go through this and that: Add
    Code:
    ;master=true
    in BluetoothServer.java at this line (?) of code
    Code:
    con = (StreamConnectionNotifier) Connector
    .open("btspp://localhost:" + uuid
    + ";name=batalha_emulator;authorize=false");
    And please, do me a favour: Never ever again start with a code example but a book.
    Last edited by traud; 2010-05-17 at 14:28.

  3. #3
    Regular Contributor
    Join Date
    Dec 2009
    Posts
    76

    Re: Bluetooth between 4 devices

    Thanks traud for pointing to the pdf.
    I read the pdf, and in the pdf the "btspp://localhost" line is in the BluetoothClient (or Slave) code, rather than the server-side. Hence the "master=true" isn't required.
    Its kinda contradictory that the book and the wiki article varies, as the book correctly points out that all the connection needs to be started by the server/master, and hence the slave/client just waits for it (earlier I was doing it wrongly, by asking each of the client to query the server).

    Still I am unable to do a bluetooth point-to-multipoint communication. I don't know the problem with my code, and I have been playing it for around a week now. Could you please post your working code somewhere, or as a Nokia wiki article?

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    4,105
    That PDF discusses a complete different role scenario, however, should give a clue about the problem.

    Okay, forget my explanations in that other threads. Just have a look at that PDF. Then work through your wiki-article again. Identify which part is the slave and which is the server. Who is the master? Who is the client? There are four roles. There are different strategies to assign them. Furthermore, some roles can change while creating the connection. A device with ‘localhost’ in its connection URL is a server, because it provides a service to another device. Everyone else is a client. The one who starts the connection has to do that as master. The trick is to have only one master at the end. The PDF has a different strategy than that book. Your wiki-article has … that patch was intended for that (not yet reviewed) wiki-article. Did you try that master=true patch?
    Quote Originally Posted by mohitrb1 View Post
    Could you please post your working code somewhere, or as a Nokia wiki article?
    Nokia offers paid technical support for everyone …

  5. #5
    Regular Contributor
    Join Date
    Dec 2009
    Posts
    76

    Re: Bluetooth between 4 devices

    I tried using the patch master=true but nothing good came out of it.
    As a student, paying for technical help is kinda out of question. Hence I can only give you a warm thanks for the help.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    4,105
    I am not employed by Nokia. I am a contributor. If master=true did not the trick alone, have a look at the UUID as well. I recommend a 128 bit UUID made of your own. However, all that is just guessing in the wild. Could be anything. Go for on-device debugging. I learned a lot from that, too.

  7. #7
    Regular Contributor
    Join Date
    Dec 2009
    Posts
    76

    Re: Bluetooth between 4 devices

    Here is my code:
    Server side (querying and asking the clients to connect):
    Code:
    public class BluetoothServer implements Runnable, DiscoveryListener {
        private LocalDevice local_device;
        private UUID uuid;
        private DiscoveryAgent disc_agent;
        private Vector devices;
        private List deviceList;
        private ServiceRecord[] service;
        private RemoteDevice[] otherDevice;
        DataOutputStream[] output;
        DataInputStream[] input;
        private boolean deviceChosen;
        private boolean connect;
        Thread t;
        String url;
        StreamConnection[] conn;
        public boolean connected;
        public int numberOfClients;
        int globalIndex;
    
        public BluetoothServer(int num) {
            this.numberOfClients = num;
            this.connected = false;
            this.otherDevice = new RemoteDevice[this.numberOfClients];
            this.output = new DataOutputStream[this.numberOfClients];
            this.input = new DataInputStream[this.numberOfClients];
            this.service = new ServiceRecord[this.numberOfClients];
            this.conn = new StreamConnection[this.numberOfClients];
            globalIndex = 0;
            devices = new Vector();
            deviceChosen = false;
            connect = false;
            deviceList = new List("Devices discovered", List.EXCLUSIVE);
            t = new Thread(this);
            t.start();
        }
    
        public void run() {
            try {
                local_device = LocalDevice.getLocalDevice();
                disc_agent = local_device.getDiscoveryAgent();
                local_device.setDiscoverable(DiscoveryAgent.GIAC);
                disc_agent.startInquiry(DiscoveryAgent.GIAC, this);
                uuid = new UUID(0x0003);
                while (!deviceChosen) {
                }
                for (int i = 0; i < numberOfClients; i++) {
                    if (connect) {
                        int transationID = disc_agent.searchServices(new int[]{0x0100}, new UUID[]{uuid}, otherDevice[i], this);
                    }
                    while (globalIndex != i + 1) {
                    }
                }
            } catch (BluetoothStateException e) {
            } catch (IOException e) {
            }
        }
    
        public void deviceDiscovered(RemoteDevice arg0, DeviceClass arg1) {
            try {
                devices.addElement(arg0);
            } catch (Exception ex) {
            }
        }
    
        public void inquiryCompleted(int discType) {
            if(discType ==INQUIRY_COMPLETED){
                if (devices.size() > 0) {
                    deviceList.deleteAll();
                    int index = 0;
                    for (int i = 0; i < devices.size(); i++) {
                        try {
                            RemoteDevice rd = (RemoteDevice) devices.elementAt(i);
                            //Assuming the devices that we need to connect have their names beginning with "Dev"
                            if (rd.getFriendlyName(false).toString().substring(0, 3).equals("Dev")) {
                                otherDevice[index] = rd;
                                index++;
                                if (index >= numberOfClients) {
                                    deviceChosen = true;
                                    connect = true;
                                    break;
                                }
                            }
                        } catch (IOException e) {
                        }
                    }
                } else {
                    deviceChosen = true;
                }
            }
        }
    
        public void serviceSearchCompleted(int transID, int respCode) {
            if (respCode == SERVICE_SEARCH_COMPLETED) {
                try {
                    createConnection();
                } catch (Exception e) {
                }
            }
        }
    
        private void createConnection() {
            try {
                url = service[globalIndex].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
                conn[globalIndex] = (StreamConnection) Connector.open(url);
                output[globalIndex] = conn[globalIndex].openDataOutputStream();
                input[globalIndex] = conn[globalIndex].openDataInputStream();
                globalIndex++;
                if (globalIndex >= numberOfClients) {
                    connected = true;
                }
            } catch (Exception e) {
            }
        }
    
        public void servicesDiscovered(int arg0, ServiceRecord[] servRecord) {
            service[globalIndex] = servRecord[0];
        }
    }
    Client Side (waiting for the Server to query and connect):
    Code:
    public class BluetoothClient implements Runnable {
        private StreamConnectionNotifier con;
        Thread t;
        public boolean connected;
        StreamConnection conn;
        DataInputStream in;
        DataOutputStream out;
        
        public BluetoothClient() {
            this.connected = false;
            t = new Thread(this);
            t.start();
        }
    
        public void run() {
            createConnections();
        }
    
        private void createConnections() {
            UUID uuid = new UUID(0x0009);
            LocalDevice localDevice;
            try {
                localDevice = LocalDevice.getLocalDevice();
                localDevice.setDiscoverable(DiscoveryAgent.GIAC);
                con = (StreamConnectionNotifier) Connector.open("btspp://localhost:" + uuid + ";name=batalha_emulator;authorize=false");
                conn = con.acceptAndOpen();
                in = conn.openDataInputStream();
                out = conn.openDataOutputStream();
                connected = true;
            } catch (Exception e) {
            }
        }
    }
    Can someone please help?

  8. #8
    Regular Contributor
    Join Date
    Dec 2009
    Posts
    76

    Re: Bluetooth between 4 devices

    The posted code works fine. The 3 clients are able to connect to a server, and both server and client are able to send and receive messages.
    Now I am facing another problem, as the server has 3 input and output connections (one for each client), so the server needs to have 3 threads waiting to read from each of the client. Could somebody help me how to acheive this?

  9. #9
    Regular Contributor
    Join Date
    Dec 2009
    Posts
    76

    Re: Bluetooth between 4 devices

    Ok all is working fine now
    Thanks

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    4,105
    Did you went through the PDF coming with JSR-82 and/or the mentioned book?
    Quote Originally Posted by mohitrb1 View Post
    uuid = new UUID(0x0003);
    This is the RFComm UUID which is the base for all serial-port (SPP) based service. Consequence: You will get a lot of services returned, when searching for this UUID. Not only your intended one.
    Quote Originally Posted by mohitrb1 View Post
    UUID uuid = new UUID(0x0009);
    That is the UUID for IP. Again a very common UUID. Furthermore, the client finds your server just because the URL begins with btspp – which includes a RFComm UUID in the service record automatically. One way is to use a more appropriate UUID. If there is no UUID which fits your protocol, than you do not use 0x1101L (SPP) but your very own 128bit UUID.
    Quote Originally Posted by mohitrb1 View Post
    //Assuming the devices that we need to connect have their names beginning with "Dev"
    if (rd.getFriendlyName(false).toString().substring(0, 3).equals("Dev"))
    In Bluetooth, while your local device discovers services, profiles of the RemoteDevice are checked for UUID(s). Such a UUID allows to identify whether a remote device offers the service (and protocol) your local device can talk to/with. This UUID concepts allows to avoid to look for a certain name of an UUID. Furthermore, just looking for your own 128bit UUID avoids to look at the friendly name as well.
    Quote Originally Posted by mohitrb1 View Post
    int transationID = disc_agent.searchServices(new int[]{0x0100}, new UUID[]{uuid}, otherDevice[i], this);
    Here, you ask the remote device to send the name of the service as well. Do you need that? Otherwise, I would specify null for the parameter attrSet.
    Quote Originally Posted by mohitrb1 View Post
    localDevice.setDiscoverable(DiscoveryAgent.GIAC);
    Be careful to change the visibility state of your device. First of all, not all JSR-82 allow this and you will get an exception. Some ask the user, again an exception is possible. Furthermore, only the device publishing the (your) service (profile) might be required to be discoverable. Furthermore, consider to use LIAC, if possible in your situation, because it will return less devices in crowded scenarios.

Similar Threads

  1. Bluesoleil Software
    By kencooper in forum Bluetooth Technology
    Replies: 9
    Last Post: 2008-08-29, 19:46
  2. [Moved] bluetooth application for nokia bluetooth capable devices
    By cutescar2nis in forum Bluetooth Technology
    Replies: 2
    Last Post: 2008-08-05, 09:23
  3. How to read remote device's bluetooth address
    By liuvictor2005 in forum Symbian Networking & Messaging (Closed)
    Replies: 4
    Last Post: 2008-03-17, 06:03
  4. Bluetooth devices information
    By merchan in forum Symbian Networking & Messaging (Closed)
    Replies: 2
    Last Post: 2007-11-06, 12:00
  5. Bluetooth devices in a listbox
    By vinsofts in forum Symbian Networking & Messaging (Closed)
    Replies: 2
    Last Post: 2005-06-20, 13:27

Posting Permissions

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