2

我终于设法从我的安卓手机连接到我的设备,当我尝试从我的蓝牙插座读取数据时遇到了问题。

所以这是我建立连接到我的设备的代码,它是一个扩展的类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 输出正确的信息,说明我在代码中的位置。但它不会将流的内容输出到 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 不可用。有什么建议么?

4

1 回答 1

1

我发现使用缓冲阅读器可以很好地与蓝牙设备配合使用。然后我只是在一个真正的监听器中使用了一个带有 br.isReady 的 while.loop。基本上做一个“听众”

于 2012-07-12T11:41:04.823 回答