2

ようやく Android フォンからデバイスに接続できました。Bluetooth ソケットから読み取ろうとすると問題が発生します。

これが私のデバイスへの接続を確立するための私のコードです。これは拡張するクラスです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)); 

    }
}

}

最後のコードには 2 つの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

バッファ付きリーダーを使用すると、blietooth デバイスでうまく機能することがわかりました。そして、while true リスナーで br.isReady を使用して while.loop を使用しました。基本的に「リスナー」を作成します

于 2012-07-12T11:41:04.823 に答える