簡體   English   中英

對於Android平台上的藍牙套接字,無法使用InputStream創建ObjectInputStream

[英]Cannot create ObjectInputStream with InputStream for a Bluetooth Socket on the Android Platform

我正在為Android手機編寫多人游戲。 通過藍牙進行通信。 我已設法使用輸入/輸出流將字節從一個電話發送到另一個電話。 因為我需要能夠傳輸我想要對象流的對象。 但是,當我嘗試使用我的流創建一個Objectstream時,我的程序掛起了指令。

public class ConnectedThread extends Thread {
private static final String TAG = "Connected Thread";
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
private Handler mHandler;
private ObjectInputStream ois;
private ObjectOutputStream oos;

public ConnectedThread(BluetoothSocket socket,Handler h) {
    mmSocket = socket;
    mHandler = h;

    InputStream tmpIn = null;
    OutputStream tmpOut = null;

    // Get the input and output streams, using temp objects because
    // member streams are final
    try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
    } catch (IOException e) { }

    mmInStream = tmpIn;
    mmOutStream = tmpOut;
    Log.d(TAG,"attempting to create OIS");

    try {
    ois = new ObjectInputStream(mmInStream);

// 指令new ObjectInputStream(mmInStream)永遠不會完成執行 它似乎沒有拋出錯誤,因為我抓住了它。 它只是掛在這個指令。 此行下面的代碼都沒有執行過。

    } catch (Exception e) {

        Log.e(TAG,"Error");
        Log.d(TAG,e.getMessage());
        e.printStackTrace();
    } 

    Log.d(TAG,"attempting to create OOS");
    try {
        oos = new ObjectOutputStream(mmOutStream);
    } catch (IOException e) {
        Log.e(TAG,"IO exception for Output Stream, I have no idea what caused this");
        Log.d(TAG,e.getMessage());
    }

}

public void run() {.....}

我究竟做錯了什么?

構造ObjectInputStream. 之前 ObjectOutputStream,只需構造ObjectOutputStream,並在兩端flush() ObjectInputStream. 您不必編寫自己的任何數據。

正如EJP建議的......

        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        oos.flush();
        ObjectInputStream is = new ObjectInputStream(socket.getInputStream());

好吧,我想我知道我做錯了什么。 對象流更復雜,似乎ObjectInputStream構造函數需要在創建流之前處理數據。 我解決了這個問題

  1. 創建OOS。
  2. 在單獨的線程中啟動OIS的構造函數。
  3. 將一些數據寫入OOS並沖洗它。
  4. 在讀取之前等待OIS初始化。

這就是我現在正在使用的(注意我還添加了一個緩沖區):

public ConnectedThread(BluetoothSocket socket,Handler h) {
    mmSocket = socket;
    mHandler = h;

    InputStream tmpIn = null;
    OutputStream tmpOut = null;

    // Get the input and output streams, using temp objects because
    // member streams are final
    try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
    } catch (Exception e) { 
        Log.d(TAG,"Error in getting Input Streams");
        Log.w(TAG,e);

    }

    mmInStream = tmpIn;
    mmOutStream = tmpOut;


    Log.d(TAG,"The socket is: " + mmSocket);
    Log.d(TAG,"The streams are: " + mmInStream + mmOutStream);
    Log.d(TAG,"attempting to create BufStreams");

    final BufferedOutputStream bufo = new BufferedOutputStream(mmOutStream);
    final BufferedInputStream bufi = new BufferedInputStream(mmInStream);

    Log.d(TAG,"attempting to create OOS");

    try {
        oos = new ObjectOutputStream(bufo);



    } catch (StreamCorruptedException e) {
        Log.d(TAG,"Caught Corrupted Stream Exception");
        Log.w(TAG,e);

    } catch (IOException e) {
        Log.d(TAG,"Caught IOException");
        Log.w(TAG,e);
    }

    Log.d(TAG,"done OOS");

    if(oos==null)
      {
           Log.d(TAG,"oos is null!!!!");
      }


    Thread s = new Thread(){
        public void run(){
            Log.d(TAG,"attempting to create OIS");
            try {
                ois = new ObjectInputStream(bufi);
            } catch (StreamCorruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.d(TAG,"completed OIS");
            if(ois == null)
            {
                Log.d(TAG,"OIS is null");
            }
        }




    };
    s.start();
    try {
        Log.d(TAG,"writing and flushing 1");
        oos.write(1);
        oos.flush();
    } catch (IOException e1) {
        Log.d(TAG,"CaugtIOexception");
        Log.w(TAG,e1);
    }
    Log.d(TAG,"sleeping to make sure stream is set up");
    while (ois == null) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }


    }
    Log.d(TAG, "done Sleeping");

    // Read out the 1 to make sure everything is okay 

    int i = 0;

    try {
        i = ois.read();
    } catch (IOException e) {
        Log.d(TAG, "error reading");
        e.printStackTrace();
    }


    Log.d(TAG,"I received an i of: " + i);
    Log.d(TAG,"OO Streams set up");




}

public void run() {...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM