java.io.IOException: Connection reset by peer
<p>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:</p>

<p>First I find the Bluetooth device and I create the socket:</p>

<pre><code>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);
            }
       }
}
</code></pre>

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

<pre><code>String[] parts = mensaje.split(" ");
String res = "";
if(parts != null &amp;&amp; parts.length &gt; 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 &lt;&lt; 4) + der);
        res += (char)aux2;
    }
}
</code></pre>

<p>And then I send the data to the bluetooth device:</p>

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

<p>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:</p>

<pre><code>//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);
}
</code></pre>

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

<pre><code>java.io.IOException: Connection reset by peer
</code></pre>

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

<pre><code>DataInputStream dIn = new DataInputStream(socket.getInputStream());
</code></pre>

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

<p>Thank you so much for your help!</p>
