5

I'm trying to manage a connection between my phone and another bluetooth device. I'm doing all this using java Android. This is the code I used for connecting the socket using my device:

First I find the Bluetooth device and I create the socket:

BluetoothDevice btNonin = null;
for (BluetoothDevice device : pairedDevices)
{
        if (device.getName().contains("Nonin")) 
        {           
            // We found the device      
            exito = true;
            try
            {
                // We create the socket
                Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
                socket = (BluetoothSocket) m.invoke(device, 1);
                socket.connect();
            }
            catch (Exception e)
            {
                Dialogs.showInfoDialog(NONIN_OFF, this);
            }
       }
}

Afther that I create the data bytes I want the remote bluetooth to receive using some code to convert ASCII to byte:

String[] parts = mensaje.split(" ");
String res = "";
if(parts != null && parts.length > 0)
{
    for (String s : parts)
    {
        if (s.length() != 2)
           break;
            byte izq, der;
        izq = (byte)char2ascii(s.charAt(0));
        der = (byte)char2ascii(s.charAt(1));
        byte aux2 = (byte)((izq << 4) + der);
        res += (char)aux2;
    }
}

And then I send the data to the bluetooth device:

// We send the data bytes
DataOutputStream dOut = new DataOutputStream(socket.getOutputStream());
dOut.writeBytes(res);
dOut.flush();

Until here it works fine. It sends the data bytes to my device. But then I want to wait for any response from my device, and then I try this:

//Waiting for response
DataInputStream dIn = new DataInputStream(socket.getInputStream());
try
{
    byte response = '\u0000';
    while (dIn.readByte() == '\u0000')
    {
        response = dIn.readByte();
    }
    Dialogs.showInfoDialog("Response: " + response, this);
}
catch (Exception e)
{
    Dialogs.showInfoDialog("No se ha recibido respuesta: " + e.toString(), this);
}

But then, on the line where I make dIn.readByte it shows the message Error:

java.io.IOException: Connection reset by peer

And I don't know why the connection is reset or what happens, as I can debug the line:

DataInputStream dIn = new DataInputStream(socket.getInputStream());

With no mistakes, so I guess the socket is still opened... What is happening here?

Thank you so much for your help!

Sonhja
  • 8,230
  • 20
  • 73
  • 131

3 Answers3

8

There are several causes of this problem. The typical cause is that you have written to a connection which has already been closed by the peer. In other words, an application protocol error.

Also your exception handling needs work. If you get any IOException on a socket other than a timeout you must close it, it is dead.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    I don't know what's wrong, as I've got the socket openned for writing, but when I try to read from it, it crashes. Maybe I need another socket for reading? – Sonhja Oct 17 '11 at 11:10
  • @Sonhja having it open for writing doesn't mean that the peer will read everything you write. You have to make sure you are sending the correct stuff. The exception can occur after the write that caused it. – user207421 Oct 17 '11 at 11:22
  • The message is the corect. It still doesn't solve my problem. – Sonhja Oct 17 '11 at 11:40
-1

Ok, I tried adding some wait() functions and it seems it works... As it was a problem between timeouts of reading and writing. I think this post should work...

Community
  • 1
  • 1
Sonhja
  • 8,230
  • 20
  • 73
  • 131
-2

remove the line dOut.flush(); this is causing the connection reset.

Anantha Sharma
  • 9,920
  • 4
  • 33
  • 35
  • 1
    This answer is completely incorrect. Flushing does not cause this exception. *Writing* causes this exception. Flushing can make it show up at a different time, but in fact this particular flush() does nothing at all, DataOutputStream.flush() being a no-op. – user207421 Oct 17 '11 at 11:16