2013-03-20 72 views
3

我正在制作一个战列舰游戏,我想发送一个名为Ships的类的数组(其中包含诸如船名,大小,旋转或不旋转以及数组列表坐标)。我GOOGLE了这个,看着堆栈溢出,我基本上需要序列化数组,但这是我卡住的地方。我需要使用ObjectOutputStream,但是如何将它嵌入到下面的代码中(取自android dev站点)。注意我已经将ship类实现为可序列化的。在此先感谢如何序列化一个对象,然后通过蓝牙发送它

public class ConnectedThread extends Thread { 
     private final BluetoothSocket mmSocket; 
     private final InputStream mmInStream; 
     private final OutputStream mmOutStream; 

     public ConnectedThread(BluetoothSocket socket) { 
      Log.d(TAG, "connectedthread started"); 
      // mHandler.obtainMessage(TEST).sendToTarget(); 
      mmSocket = socket; 
      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) { 
       Log.e(TAG, "temp sockets not created"); 
      } 

      mmInStream = tmpIn; 
      mmOutStream = tmpOut; 

     } 

     public void run() { 
      Log.i(TAG, "Begin mConnectedThread"); 
      byte[] buffer = new byte[1024]; // buffer store for the stream 
      int bytes; // bytes returned from read() 

      // Keep listening to the InputStream until an exception occurs 
      while (true) { 
       try { 
        // Read from the InputStream 
        bytes = mmInStream.read(buffer); 
        // Send the obtained bytes to the UI activity 
        Log.i(TAG, "reaaaad msg"); 
        mHandler.obtainMessage(SetUpGame.MESSAGE_READ2, bytes, -1, buffer).sendToTarget(); 
       } catch (IOException e) { 
        Log.e(TAG, "disconnectd"); 
        break; 
       } 
      } 
     } 

     /* 
     * Call this from the main activity to send data to the remote 
     * device 
     */ 
     public void write(byte[] buffer) { 

      try { 

       mmOutStream.write(buffer); 
       Log.i(TAG, "writeeee msg"); 
       mHandler.obtainMessage(SetUpGame.MESSAGE_WRITE, -1,-1, buffer).sendToTarget(); 
      } catch (IOException e) { 
       Log.e(TAG, "Exception during write"); 
      } 
     } 

     /* Call this from the main activity to shutdown the connection */ 
     public void cancel() { 
      try { 
       mmSocket.close(); 
      } catch (IOException e) { 
       Log.e(TAG, "close of connect socket failed"); 
      } 
     } 

    } 

和我的处理程序:

 final Handler mHandler = new Handler() { 
     @Override 
     public void handleMessage(android.os.Message msg) { 

      switch (msg.what) { 
      case MESSAGE_READ2: 

       byte[] readBuf = (byte[]) msg.obj; 
       String readMessage = new String(readBuf, 0, msg.arg1); 
       break; 

      case MESSAGE_WRITE: 
       byte[] writeBuf = (byte[]) msg.obj; 
       String writeMessage = new String(writeBuf); 
       //Toast.makeText(getApplicationContext(),"Me:" + writeMessage, Toast.LENGTH_SHORT).show(); 
       break; 
+0

我开发了一个Android的蓝牙系统来控制汽车中的东西......如果它未出现,我会在明天发布Github链接已经回答了,我需要找到代码 – Codeman 2013-03-20 01:06:39

回答

4

在上面你从所连接的插座输入/输出流的代码。

现在,您可以使用这些流向/从套接字流式传输数据。

你究竟做得如何取决于你想要传输的数据的类型。在这种情况下,你有一个序列化对象发送,所以你会在适应流与对象使用的过滤器换你流:

ObjectOutputStream oos = new ObjectOutputStream(mmOutStream); 
for (Ship ship: ships) 
    oos.writeObject(ship); 

此代码遍历船舶的阵列的ObjectOutputStream/ObjectInputStream的... ,将每艘船都写入流中(并因此写入蓝牙插槽)。

接收端是一样的,还有一个额外的复杂因素:你不一定知道何时停止或读什么。有各种各样的方案来处理这个问题,并且有专门处理这个问题的SO问题。 Android开发人员指南的蓝牙页面上有这样的示例代码: http://developer.android.com/guide/topics/connectivity/bluetooth.html

+0

哦,好的谢谢。我如何在处理程序的另一边处理它? – user1953208 2013-03-20 02:05:20

+0

@ user1953208我添加了关于接收答案的内容。另外,我建议你看一下SDK中的蓝牙聊天示例。 – Tom 2013-03-20 14:45:17

+0

你能提供一些关于接收对象的更多信息吗?它是'''Ship ship = objectInputStream.readObject();'''?这是一个阻止电话吗? – snapfractalpop 2015-03-09 22:34:50