如何序列化对象,然后通过蓝牙发送它

use*_*208 3 android bluetooth

我正在制作《战舰》游戏,我想发送一个名为Ships的类的Array(其中包含诸如船名,大小,是否旋转以及是否有坐标的列表)。我已经用谷歌搜索并查看了堆栈溢出,我基本上需要序列化该数组,但这就是我遇到的问题。我需要使用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");
            }
        }

    }
Run Code Online (Sandbox Code Playgroud)

和我的经理:

        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;
Run Code Online (Sandbox Code Playgroud)

Tom*_*Tom 5

在上面的代码中,您从连接的套接字获取输入/输出流。

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

具体执行方式取决于您要流式传输的数据类型。在这种情况下,您有一个可序列化的对象要发送,因此您将把您的流包装在一个过滤器中,该过滤器将使该流适合与对象一起使用:ObjectOutputStream / ObjectInputStream ...

ObjectOutputStream oos = new ObjectOutputStream( mmOutStream );
for (Ship ship: ships)
  oos.writeObject( ship );
Run Code Online (Sandbox Code Playgroud)

此代码遍历Ships数组,将每条Ship写入流(并因此写入Bluetooth套接字)。

接收方是相同的,但又有一个复杂之处:您不一定知道何时停止或阅读什么。有多种解决方案,还有一些专门针对此问题。Android开发人员指南的“蓝牙”页面具有此示例代码:http : //developer.android.com/guide/topics/connectivity/bluetooth.html