无法从读取的BluetoothSocket输入 [英] Unable to read input from a bluetoothsocket

查看:110
本文介绍了无法从读取的BluetoothSocket输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于成功地从我的Andr​​oid手机连接到我的设备,把我有一个问题,当我尝试从我的蓝牙套接字读取。

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.

因此​​,这里是我的code建立连接到我的设备,它的扩展类的AsyncTask

So here is my code for establishing connecting to my device, its a class that extends 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 所以这里的读数为code该类:

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

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两个 Log.i 方法,此输出正确的信息,以说明logcat中凡在code我。但它不输出流以LogCat中的内容。我在做什么错在这里?是的,我进去看了BluetoothChat例子。

I have two Log.i 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.

在此先感谢!

修改1

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

I've done some research in the constructor of the ReadInput Class.

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

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

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

推荐答案

我发现使用缓冲读者一个blietooth设备运行良好。然后,我只是用同一个br.isReady在while.loop一阵真正的监听器。基本上使监听器​​

I found using a buffered reader works well with a blietooth device. And then I just used a while.loop with br.isReady in a while true listener. Basically makes a "listener"

这篇关于无法从读取的BluetoothSocket输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆