对于Android平台上的蓝牙套接字,无法使用InputStream创建ObjectInputStream

时间:2011-04-09 12:56:40

标签: java android bluetooth objectoutputstream objectinputstream

我正在为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)NEVER FINISHES EXECUTING 。它似乎没有抛出错误,因为我抓住了它。它只是挂在这个指令。此行下面的代码都没有执行过。

    } 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() {.....}

我做错了什么?

3 个答案:

答案 0 :(得分:8)

在构建ObjectOutputStream,之前,只需构建flush()ObjectInputStream.两端,您不必编写自己的任何数据。

答案 1 :(得分:3)

正如EJP建议的那样......

image()

答案 2 :(得分:1)

好吧,我想我知道自己做错了什么。对象流更复杂,似乎ObjectInputStream构造函数需要在创建流之前处理数据。

我解决了这个问题
  1. 创建OOS。
  2. 以单独的线程启动OIS的构造函数。
  3. 将一些数据写入OOS并冲洗它。
  4. 在阅读之前等待OIS初始化。
  5. 这就是我现在正在使用的(注意我还添加了一个缓冲区):

    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() {...