Unable to read input from a bluetoothsocket
<p>I've finally managed to connect from my android phone to my device, put I have a problem when I try to read from my bluetooth socket. </p>

<p>So here is my code for establishing connecting to my device, its a class that extends <code>AsyncTask</code> </p>

<pre><code>import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;

public class Connect extends AsyncTask&lt;String, Void, String&gt; {

private final static String TAG = "+++CONNECT THREAD+++";

ProgressDialog progressDialog; 

Context context;

BluetoothSocket tmp; 
BluetoothDevice device; 
BluetoothAdapter ba; 
Button connect;
int bt_port_to_connect; 

ReadInput ri; 

InputStream is; 
byte[] test; 

public Connect(Context context, BluetoothDevice device, BluetoothAdapter ba, Button connect) {

    this.ba = ba; 
    this.context = context; 
    this.device = device; 
    this.connect = connect; 
    bt_port_to_connect = 9; 
}


 protected void onPreExecute() {

     progressDialog=ProgressDialog.show(context,"Please Wait..","Connecting to device",false);
  }


@Override
protected String doInBackground(String... arg0) {

        Method m = null;

        try {
            m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
        } catch (NoSuchMethodException e1) {

            e1.printStackTrace();
    }

    try {
        tmp = (BluetoothSocket) m.invoke(device, bt_port_to_connect);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



try {
    ba.cancelDiscovery(); 
    tmp.connect();

    ri = new ReadInput(tmp); 
    ri.start(); 

} catch (IOException e) {
    Log.e("+++CONNECT1+++","EXCEPTION:  " + e.getMessage());

    try {
        tmp.close();
    } catch (IOException e2) {
        Log.e(TAG, "unable to close() " + " insecure socket type" +
                " socket during connection failure", e2);
    }


    Log.e("+++CONNECT2+++", e.getLocalizedMessage());

}

boolean isConnected = tmp.isConnected();

if(isConnected) {

    return "connected";
}

else {
    return "notConnected";
}
}

protected void onPostExecute(String result) {
    progressDialog.dismiss(); 

    if(result.equals("connected")) { 
        connect.setEnabled(false); 
        Toast.makeText(context, "Connected to device: "+device.getName().toString(), Toast.LENGTH_LONG).show(); 

        //new ReadIn(context, tmp).execute(""); 

    }
    else if(result.equals("notConnected")) {
        Toast.makeText(context, "Can`t reach host", Toast.LENGTH_LONG).show(); 
    }

}
}
</code></pre>

<p>As you can see, the line below <code>tmp.connect();</code> I create a new object of a new class, this is the class which I want to handle the reading of the <code>inputStream</code> So here is the code for that class:</p>

<pre><code>import java.io.IOException;
import java.io.InputStream;

import android.bluetooth.BluetoothSocket;
import android.util.Log;

public class ReadInput extends Thread {

BluetoothSocket socket; 
private InputStream is; 

public ReadInput(BluetoothSocket socket) {


    Log.i("READINPUT", "INSIDE READ INPUT THREAD CONSTRUCTOR!!!");
    InputStream tmpIn = null;
    this.socket = socket; 
    is = null; 



    try {
        tmpIn = socket.getInputStream();
    } catch (IOException e) {
        Log.e("READINPUT", "Temp socket in created: " + e.getMessage()); 
    }

    is = tmpIn; 
}

public void run() {
    Log.i("READINPUT", "INSIDE READ INPUT THREAD RUN METHOD!!!");

    byte[] buffer = new byte[1024]; 
    int bytes = 0; 

    while(true) {


        try {
            bytes = is.read(buffer);
        } catch (IOException e) {
            Log.e("FROM RUN METHOD: ", e.getMessage()); 
        }

        Log.i("INPUTSTREAM GOT:  ", Integer.toString(bytes)); 

    }
}

}
</code></pre>

<p>I have two <code>Log.i</code> methods in my last code, this outputs the correct info to LogCat stating where in the code I am. But it doesnt output the content of the stream to LogCat. What am I doing wrong here? Yes, I've looked into the BluetoothChat example. </p>

<p>Thanks in advance!</p>

<p><strong>EDIT 1</strong></p>

<p>I've done some research in the constructor of the <code>ReadInput</code> Class. </p>

<pre><code>is = tmpIn; 
    try {
        Log.i("InputStream: ", Integer.toString(is.available()));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
</code></pre>

<p>This snippet will only output to logcat that is returns <code>0</code> which means that the InputStream is not available. Any suggestions? </p>
