7

我正在为Android手机编写多人游戏。通信是通过蓝牙。我设法使用输入/输出流将字节从一部电话发送到另一部电话。因为我需要能够传输对象我想要objectstreams。但是,当我尝试用我的流创建Objectstream时,我的程序挂在指令上。无法使用InputStream为Android平台上的蓝牙套接字创建ObjectInputStream

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); 

// 指令新ObjectInputStream的(mmInStream)NEVER完成执行。它似乎没有抛出错误,因为我会抓住它。它只是挂在这条指令上。这行下面的代码都没有执行过。

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

我在做什么错?

回答

8

只是构建ObjectOutputStream,flush()它,两端之前构建ObjectInputStream.你不不必写任何你自己的数据。

+0

你能举一个例子吗?我有同样的问题,我不太明白你的解决方案... – jpmastermind 2013-03-31 06:00:00

+0

你能否请详细一点。我没有得到你的解决方案。 – 2014-01-16 06:47:36

+0

@Kake请参阅编辑。我不明白这比它更清楚。 – EJP 2014-01-16 06:54:05

1

好吧,我想我知道我做错了什么。对象流更复杂,看起来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() {... 
+0

这全是废话。创建ObjectOutputStream时不会引发StreamCorruptedException。你所要做的就是首先在两端创建ObjectOutputStream。 – EJP 2011-08-09 11:52:01

+0

我正在做的ObjectOutputStream第一次,刷新它,然后创建输入流。写入后也会清除输出流。我仍然遇到“悬而未决”的问题。 – Siddharth 2013-04-22 08:20:33

3

由于EJP建议...

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