2012-07-12 27 views
2

我终于设法从我的android手机连接到我的设备,当我尝试从我的蓝牙套接字读取时出现问题。无法读取来自蓝牙套接字的输入

所以这是我建立连接到我的设备,其扩展AsyncTask

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<String, Void, String> { 

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(); 
    } 

} 
} 

正如你可以看到,下面tmp.connect();行我创建了一个新类的新对象的类代码,这是这是我要处理的inputStream所以这里的阅读类是类代码:

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)); 

    } 
} 

} 

我在过去的两个代码Log.i方法,这个输出正确的信息,以说明logcat中WHE重新在我的代码。但它不会将流的内容输出到LogCat。我在这里做错了什么?是的,我查看了BluetoothChat示例。

在此先感谢!

编辑1

我在ReadInput类的构造函数做了一些研究。

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

这个片段将只输出到的logcat即返回0这意味着InputStream为不可用。有什么建议么?

回答

2

我发现使用缓冲式阅读器可以很好地使用blietooth设备。然后,我在一段时间内用br.isReady使用了一段while.loop。基本上做一个“听众”

+0

你能提供一个这个实现的例子吗?谢谢! – 2012-07-13 08:54:46

+1

当我醒来xD时,我会在大约12个小时后发布一些东西 – FabianCook 2012-07-13 09:10:10