3

Just to make it short. I want to connect to three devices and read the data from all the three at the same time. I know how to connect to one and i can read the data from that But What i want is to connect the other two and read from them.Any suggestions , or other example i can follow.

Here is part of the code which is similar to mine but shorter . This code can connect to one device using the mac. So How to modify this to connect to three devices at the same time and display the data they are sending?

Please help i really need it cause am stuck at this part of my project. Thanks for the help.

private boolean connected = false;
private BluetoothSocket sock;
private InputStream in;
public void test() throws Exception {
    if (connected) {
        return;
    }
    BluetoothDevice zee = BluetoothAdapter.getDefaultAdapter().
        getRemoteDevice("00:14:C5:A1:02:67");
    Method m = zee.getClass().getMethod("createRfcommSocket",
        new Class[] { int.class });
    sock = (BluetoothSocket)m.invoke(zee, Integer.valueOf(1));
    devicecon.setText("Connecting......");
    sock.connect();
    devicecon.setText("Connected");
    in = sock.getInputStream();
    byte[] buffer = new byte[50];
    int read = 0;
    Log.d("ZeeTest", "++++ Listening...");
    try {
        while (true) {
            read = in.read(buffer);
            connected = true;
            StringBuilder buf = new StringBuilder();
            for (int i = 0; i < read; i++) {
                int b = buffer[i] & 0xff;
                if (b < 0x10) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(b)).append(" ");
            }
            Log.d("ZeeTest", "++++ Read "+ read +" bytes: "+ buf.toString());
        }
    } catch (IOException e) {}
    Log.d("ZeeTest", "++++ Done: test()");
  • Did you see https://groups.google.com/forum/?fromgroups#!topic/android-developers/adeBD275u30 ? – Jack May 11 '12 at 02:49

1 Answers1

0

All you need to do is create a new thread (I suggest using an instance of AsyncTask for this rather than an actual Thread) for each device and call the connection code snippet

BluetoothDevice zee = BluetoothAdapter.getDefaultAdapter().
    getRemoteDevice("00:14:C5:A1:02:67");
Method m = zee.getClass().getMethod("createRfcommSocket",
    new Class[] { int.class });
sock = (BluetoothSocket)m.invoke(zee, Integer.valueOf(1));
devicecon.setText("Connecting......");
sock.connect();

from inside that new thread.

chandsie
  • 2,865
  • 3
  • 27
  • 37
  • I don't know Alot about ASync:( could you please Modify the code or just direct me to any similar program that tries to connect more than one device at the same time..post the link..i would follow and modify my code. Thanks Alot – user1013008 May 11 '12 at 19:33
  • If you read through the documentation on AsyncTask that will definitely help you. You should put the connection code in the doInBackground method of your AsyncTask subclass, and then perhaps in a separate AyncTask you can put all your communication code (again within doInBackground). Also please read through the AsyncTask docs, they are very helpful. – chandsie May 11 '12 at 22:08